From 62f9984e05d49ae19d26cca44ca9d0ee35f49d02 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) --- types.go | 2 +- types_test.go | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/types.go b/types.go index ffeb55a0..d4818c3f 100644 --- a/types.go +++ b/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/types_test.go b/types_test.go index 9735937b..dd31298f 100644 --- a/types_test.go +++ b/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) {