Skip to content

Commit

Permalink
Fix race in mutate (#1627)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonjohnsonjr committed Apr 6, 2023
1 parent 6ac92e8 commit aee00b1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/v1/mutate/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"encoding/json"
"errors"
"sync"

v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/partial"
Expand All @@ -38,6 +39,8 @@ type image struct {
diffIDMap map[v1.Hash]v1.Layer
digestMap map[v1.Hash]v1.Layer
subject *v1.Descriptor

sync.Mutex
}

var _ v1.Image = (*image)(nil)
Expand All @@ -50,6 +53,9 @@ func (i *image) MediaType() (types.MediaType, error) {
}

func (i *image) compute() error {
i.Lock()
defer i.Unlock()

// Don't re-compute if already computed.
if i.computed {
return nil
Expand Down
6 changes: 6 additions & 0 deletions pkg/v1/mutate/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package mutate
import (
"encoding/json"
"fmt"
"sync"

"github.com/google/go-containerregistry/pkg/logs"
v1 "github.com/google/go-containerregistry/pkg/v1"
Expand Down Expand Up @@ -71,6 +72,8 @@ type index struct {
indexMap map[v1.Hash]v1.ImageIndex
layerMap map[v1.Hash]v1.Layer
subject *v1.Descriptor

sync.Mutex
}

var _ v1.ImageIndex = (*index)(nil)
Expand All @@ -85,6 +88,9 @@ func (i *index) MediaType() (types.MediaType, error) {
func (i *index) Size() (int64, error) { return partial.Size(i) }

func (i *index) compute() error {
i.Lock()
defer i.Unlock()

// Don't re-compute if already computed.
if i.computed {
return nil
Expand Down

0 comments on commit aee00b1

Please sign in to comment.