Commit 7adb42ee authored by Richard Eric Gavaletz's avatar Richard Eric Gavaletz Committed by Robert Griesemer

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
parent 5dc8c4db
......@@ -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
......
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment