golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.69k stars 17.62k forks source link

container/list: lsit.Remove allows removal of elements not in the list, leading to negative length #70022

Open ktalg opened 4 hours ago

ktalg commented 4 hours ago

Go version

go version go1.22.5 linux/amd64

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='amd64'

GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'

GOOS='linux'

GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'

GOVCS=''
GOVERSION='go1.22.5'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'

GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1426153306=/tmp/go-build -gno-record-gcc-switches'

What did you do?

https://go.dev/play/p/OK0kMoF1XR9

func main() {
    l := list.New()
    l.PushBack(1)
    front := l.Front()
    l.Init()
    fmt.Println("list len:", l.Len(), "front is nil:", l.Front() == nil)
    l.Remove(front)
    fmt.Println("list len:", l.Len(), "front is nil:", l.Front() == nil)
}

What did you see happen?

list len: 0 front is nil: true list len: -1 front is nil: false

What did you expect to see?

I expected that after reinitializing the list with l.Init(), attempting to remove an element that was previously in the list would either do nothing or result in a runtime error, and the list length would remain at 0.

gabyhelp commented 4 hours ago

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)