From c5226d94fab4e261fe2407262d9b5177326d4062 Mon Sep 17 00:00:00 2001 From: Kato Kazuyoshi Date: Thu, 10 Apr 2014 22:07:29 +0900 Subject: [PATCH] Avoid "invalid memory address or nil pointer dereference" panic libcontainer.GetNamespace returns nil on FreeBSD because libcontainer.namespaceList is empty. In this case, Namespaces#Get should return nil instead of being panic. Docker-DCO-1.1-Signed-off-by: Kato Kazuyoshi (github: kzys) --- pkg/libcontainer/types.go | 2 +- pkg/libcontainer/types_test.go | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/libcontainer/types.go b/pkg/libcontainer/types.go index ffeb55a022..d4818c3ffe 100644 --- a/pkg/libcontainer/types.go +++ b/pkg/libcontainer/types.go @@ -68,7 +68,7 @@ func (n Namespaces) Contains(ns string) bool { func (n Namespaces) Get(ns string) *Namespace { for _, nsp := range n { - if nsp.Key == ns { + if nsp != nil && nsp.Key == ns { return nsp } } diff --git a/pkg/libcontainer/types_test.go b/pkg/libcontainer/types_test.go index 9735937b76..dd31298fdf 100644 --- a/pkg/libcontainer/types_test.go +++ b/pkg/libcontainer/types_test.go @@ -18,6 +18,15 @@ func TestNamespacesContains(t *testing.T) { if !ns.Contains("NEWPID") { t.Fatal("namespaces should contain NEWPID but does not") } + + withNil := Namespaces{ + GetNamespace("UNDEFINED"), // this element will be nil + GetNamespace("NEWPID"), + } + + if !withNil.Contains("NEWPID") { + t.Fatal("namespaces should contain NEWPID but does not") + } } func TestCapabilitiesContains(t *testing.T) {