Skip to content

Commit 642242a

Browse files
committed
builder/mobyexporter: Add missing nil check
Add a nil check to handle a case where the image config JSON would deserialize into a nil map. Signed-off-by: Paweł Gronowski <[email protected]>
1 parent 076c976 commit 642242a

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

builder/builder-next/exporter/mobyexporter/writer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ func patchImageConfig(dt []byte, dps []digest.Digest, history []ocispec.History,
4545
return nil, errors.Wrap(err, "failed to parse image config for patch")
4646
}
4747

48+
if m == nil {
49+
return nil, errors.New("null image config")
50+
}
51+
4852
var rootFS ocispec.RootFS
4953
rootFS.Type = "layers"
5054
rootFS.DiffIDs = append(rootFS.DiffIDs, dps...)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package mobyexporter
2+
3+
import (
4+
"testing"
5+
6+
"gotest.tools/v3/assert"
7+
)
8+
9+
func TestPatchImageConfig(t *testing.T) {
10+
for _, tc := range []struct {
11+
name string
12+
cfgJSON string
13+
err string
14+
}{
15+
{
16+
name: "empty",
17+
cfgJSON: "{}",
18+
},
19+
{
20+
name: "history only",
21+
cfgJSON: `{"history": []}`,
22+
},
23+
{
24+
name: "rootfs only",
25+
cfgJSON: `{"rootfs": {}}`,
26+
},
27+
{
28+
name: "null",
29+
cfgJSON: "null",
30+
err: "null image config",
31+
},
32+
} {
33+
t.Run(tc.name, func(t *testing.T) {
34+
_, err := patchImageConfig([]byte(tc.cfgJSON), nil, nil, nil)
35+
if tc.err == "" {
36+
assert.NilError(t, err)
37+
} else {
38+
assert.ErrorContains(t, err, tc.err)
39+
}
40+
})
41+
}
42+
}

0 commit comments

Comments
 (0)