Skip to content

Commit bb4fdfe

Browse files
author
zhouhao
committed
image: avoid panic on nil fields when converting
The following error occurs when the create command is executed: panic: runtime error: invalid memory address or nil pointer dereference [recovered] panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x48ef43] goroutine 19 [running]: panic(0x808060, 0xc420010130) /home/zhouhao/go/src/runtime/panic.go:500 +0x1ae testing.tRunner.func1(0xc4200932c0) /home/zhouhao/go/src/testing/testing.go:579 +0x474 panic(0x808060, 0xc420010130) /home/zhouhao/go/src/runtime/panic.go:458 +0x271 ... Because there is no non-empty check on the optional fields in config.go. Signed-off-by: zhouhao <[email protected]>
1 parent b686775 commit bb4fdfe

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

image/config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,11 @@ func (c *config) runtimeSpec(rootfs string) (*specs.Spec, error) {
7373
var s specs.Spec
7474
s.Version = specs.Version
7575
// we should at least apply the default spec, otherwise this is totally useless
76-
s.Process.Terminal = true
76+
s.Root = &specs.Root{}
7777
s.Root.Path = rootfs
78+
79+
s.Process = &specs.Process{}
80+
s.Process.Terminal = true
7881
s.Process.Cwd = "/"
7982
if c.Config.WorkingDir != "" {
8083
s.Process.Cwd = c.Config.WorkingDir

image/image_test.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,37 @@ type imageLayout struct {
187187
tarList []tarContent
188188
}
189189

190-
func TestValidateLayout(t *testing.T) {
190+
func TestImageLayout(t *testing.T) {
191191
root, err := ioutil.TempDir("", "oci-test")
192192
if err != nil {
193193
t.Fatal(err)
194194
}
195195
defer os.RemoveAll(root)
196196

197+
dest1, err := ioutil.TempDir("", "dest1")
198+
if err != nil {
199+
t.Fatal(err)
200+
}
201+
defer os.RemoveAll(dest1)
202+
203+
dest2, err := ioutil.TempDir("", "dest2")
204+
if err != nil {
205+
t.Fatal(err)
206+
}
207+
defer os.RemoveAll(dest2)
208+
209+
dest3, err := ioutil.TempDir("", "dest3")
210+
if err != nil {
211+
t.Fatal(err)
212+
}
213+
defer os.RemoveAll(dest3)
214+
215+
dest4, err := ioutil.TempDir("", "dest4")
216+
if err != nil {
217+
t.Fatal(err)
218+
}
219+
defer os.RemoveAll(dest4)
220+
197221
il := imageLayout{
198222
rootDir: root,
199223
layout: layoutStr,
@@ -217,6 +241,23 @@ func TestValidateLayout(t *testing.T) {
217241
if err != nil {
218242
t.Fatal(err)
219243
}
244+
245+
err = UnpackLayout(root, dest1, refTag[0], "")
246+
if err != nil {
247+
t.Fatal(err)
248+
}
249+
err = UnpackLayout(root, dest2, refTag[1], "linux:amd64")
250+
if err != nil {
251+
t.Fatal(err)
252+
}
253+
err = CreateRuntimeBundleLayout(root, dest3, refTag[0], "rootfs", "")
254+
if err != nil {
255+
t.Fatal(err)
256+
}
257+
err = CreateRuntimeBundleLayout(root, dest4, refTag[1], "rootfs", "linux:amd64")
258+
if err != nil {
259+
t.Fatal(err)
260+
}
220261
}
221262

222263
func createImageLayoutBundle(il imageLayout) error {

0 commit comments

Comments
 (0)