Skip to content

container/list element.Next() panics and should return nil #6349

@gavaletz

Description

@gavaletz
Not that there is a good reason to call Next() on an element removed from a list, but
the element removed should still return nil for Next() to satisfy the API.

// remove removes e from its list, decrements l.len, and returns e.
func (l *List) remove(e *Element) *Element {
    e.prev.next = e.next
    e.next.prev = e.prev
    e.next = nil // avoid memory leaks
    e.prev = nil // avoid memory leaks
    e.list = nil
    l.len--
    return e
}

When e.next is nil then calling e.Next() will panic

// Next returns the next list element or nil.
func (e *Element) Next() *Element {
    if p := e.next; p != &e.list.root {
        return p
    }
    return nil
}

Here e.list is nil, and so e.list.root will panic.  Prior to this CL

https://golang.org/cl/6569072

e.list was not set to nil when the element was removed from a list.  There is a
discussion of the problem here:

https://groups.google.com/forum/#!topic/golang-nuts/HGCY7IanlvU

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions