container/list: unexpected panic if Next/Prev called outside of list.

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
This commit is contained in:
Richard Eric Gavaletz 2013-09-09 15:41:36 -07:00 committed by Robert Griesemer
parent 5dc8c4dbfb
commit 7adb42eee4
2 changed files with 20 additions and 2 deletions

View 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

View 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)