Skip to content

Commit d58bc16

Browse files
committed
validate/CheckRoot: fix panic on empty spec
Fix the following panic: > panic: runtime error: invalid memory address or nil pointer dereference [recovered] > panic: runtime error: invalid memory address or nil pointer dereference > ... > github.com/opencontainers/runtime-tools/validate.(*Validator).CheckRoot(0xc00049b748) > /home/kir/go/src/github.com/opencontainers/runtime-tools/validate/validate.go:192 +0x278 This is caused by empty Windows field, which is a spec violation, but it should not result in a nil pointer dereference Add a test case (which fails before the fix). Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 83399e7 commit d58bc16

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

validate/validate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ func (v *Validator) CheckJSONSchema() (errs error) {
170170
func (v *Validator) CheckRoot() (errs error) {
171171
logrus.Debugf("check root")
172172

173-
if v.platform == "windows" && v.spec.Windows != nil {
174-
if v.spec.Windows.HyperV != nil {
173+
if v.platform == "windows" {
174+
if v.spec.Windows != nil && v.spec.Windows.HyperV != nil {
175175
if v.spec.Root != nil {
176176
errs = multierror.Append(errs,
177177
specerror.NewError(specerror.RootOnHyperVNotSet, fmt.Errorf("for Hyper-V containers, Root must not be set"), rspec.Version))
@@ -182,7 +182,7 @@ func (v *Validator) CheckRoot() (errs error) {
182182
specerror.NewError(specerror.RootOnWindowsRequired, fmt.Errorf("on Windows, for Windows Server Containers, Root is REQUIRED"), rspec.Version))
183183
return
184184
}
185-
} else if v.platform != "windows" && v.spec.Root == nil {
185+
} else if v.spec.Root == nil {
186186
errs = multierror.Append(errs,
187187
specerror.NewError(specerror.RootOnNonWindowsRequired, fmt.Errorf("on all other platforms, Root is REQUIRED"), rspec.Version))
188188
return

validate/validate_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ func TestCheckRoot(t *testing.T) {
289289
{rspec.Spec{Windows: &rspec.Windows{HyperV: &rspec.WindowsHyperV{}}, Root: nil}, "windows", specerror.NonError},
290290
{rspec.Spec{Windows: &rspec.Windows{}, Root: &rspec.Root{Path: filepath.Join(tmpBundle, "rootfs")}}, "windows", specerror.RootPathOnWindowsGUID},
291291
{rspec.Spec{Windows: &rspec.Windows{}, Root: &rspec.Root{Path: "\\\\?\\Volume{ec84d99e-3f02-11e7-ac6c-00155d7682cf}\\"}}, "windows", specerror.NonError},
292+
{rspec.Spec{}, "windows", specerror.RootOnWindowsRequired},
292293
{rspec.Spec{Windows: &rspec.Windows{}, Root: nil}, "windows", specerror.RootOnWindowsRequired},
293294
{rspec.Spec{Root: nil}, "linux", specerror.RootOnNonWindowsRequired},
294295
{rspec.Spec{Root: &rspec.Root{Path: "maverick-rootfs"}}, "linux", specerror.RootPathOnPosixConvention},

0 commit comments

Comments
 (0)