]> Cypherpunks repositories - gostls13.git/commitdiff
container/list: unexpected panic if Next/Prev called outside of list.
authorRichard Eric Gavaletz <gavaletz@gmail.com>
Mon, 9 Sep 2013 22:41:36 +0000 (15:41 -0700)
committerRobert Griesemer <gri@golang.org>
Mon, 9 Sep 2013 22:41:36 +0000 (15:41 -0700)
Before CL 7065067 calling Next on an element returned either the
next/prev element or nil was returned.  After the CL if an element
was not part of a list e.Next() and e.Prev() will panic.  This CL
returns to the documented behavior, that Next/Prev returns the
next/prev list element or nil.

Fixes #6349.

R=golang-dev, gri
CC=golang-dev
https://golang.org/cl/13234051

src/pkg/container/list/list.go
src/pkg/container/list/list_test.go

index 34e6b9a50dfc7ddb4445fa39e2b2e72a511bcddd..ed2d15a45756a32d3346e6e931e24a26935afd35 100644 (file)
@@ -29,7 +29,7 @@ type Element struct {
 
 // Next returns the next list element or nil.
 func (e *Element) Next() *Element {
-       if p := e.next; p != &e.list.root {
+       if p := e.next; e.list != nil && p != &e.list.root {
                return p
        }
        return nil
@@ -37,7 +37,7 @@ func (e *Element) Next() *Element {
 
 // Prev returns the previous list element or nil.
 func (e *Element) Prev() *Element {
-       if p := e.prev; p != &e.list.root {
+       if p := e.prev; e.list != nil && p != &e.list.root {
                return p
        }
        return nil
index 33f060c7f5b510967b138c776a8667f3239efae1..ee52afe82b9aac13c4ad0d6cd10edf62009f0d20 100644 (file)
@@ -234,6 +234,24 @@ func TestIssue4103(t *testing.T) {
        }
 }
 
+func TestIssue6349(t *testing.T) {
+       l := New()
+       l.PushBack(1)
+       l.PushBack(2)
+
+       e := l.Front()
+       l.Remove(e)
+       if e.Value != 1 {
+               t.Errorf("e.value = %d, want 1", e.Value)
+       }
+       if e.Next() != nil {
+               t.Errorf("e.Next() != nil")
+       }
+       if e.Prev() != nil {
+               t.Errorf("e.Prev() != nil")
+       }
+}
+
 func TestMove(t *testing.T) {
        l := New()
        e1 := l.PushBack(1)