8000 builder/mobyexporter: Add missing nil check · moby/moby@c1d4587 · GitHub
[go: up one dir, main page]

Skip to content

Commit c1d4587

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 <pawel.gronowski@docker.com> (cherry picked from commit 642242a) Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
1 parent d642804 commit c1d4587

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

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+
A42E },
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)
0