Prechádzať zdrojové kódy

Merge pull request #5143 from kzys/ns-nil

Avoid "invalid memory address or nil pointer dereference" panic
Victor Vieux 11 rokov pred
rodič
commit
b8c10d8af2
2 zmenil súbory, kde vykonal 10 pridanie a 1 odobranie
  1. 1 1
      pkg/libcontainer/types.go
  2. 9 0
      pkg/libcontainer/types_test.go

+ 1 - 1
pkg/libcontainer/types.go

@@ -68,7 +68,7 @@ func (n Namespaces) Contains(ns string) bool {
 
 
 func (n Namespaces) Get(ns string) *Namespace {
 func (n Namespaces) Get(ns string) *Namespace {
 	for _, nsp := range n {
 	for _, nsp := range n {
-		if nsp.Key == ns {
+		if nsp != nil && nsp.Key == ns {
 			return nsp
 			return nsp
 		}
 		}
 	}
 	}

+ 9 - 0
pkg/libcontainer/types_test.go

@@ -18,6 +18,15 @@ func TestNamespacesContains(t *testing.T) {
 	if !ns.Contains("NEWPID") {
 	if !ns.Contains("NEWPID") {
 		t.Fatal("namespaces should contain NEWPID but does not")
 		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) {
 func TestCapabilitiesContains(t *testing.T) {