Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

container/list: zero List value breaks after reset #39014

Open
gobwas opened this issue May 12, 2020 · 8 comments
Open

container/list: zero List value breaks after reset #39014

gobwas opened this issue May 12, 2020 · 8 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@gobwas
Copy link

gobwas commented May 12, 2020

What version of Go are you using (go version)?

$ go version
go version go1.14.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOHOSTARCH="amd64"
GOHOSTOS="darwin"

What did you do?

var l list.List
el := l.PushBack(42)
l = list.List{} // Reset the list.
l.Remove(el)
fmt.Println(l.Len())

What did you expect to see?

0

What did you see instead?

-1
@lsytj0413
Copy link

Because the another assign l = list.List{} Never change the address of l, you can print the memory address of l with:

	fmt.Printf("%p\n", &l)
	el := l.PushBack(42)
	l = list.List{} // Reset the list.
	fmt.Printf("%p\n", &l)

@lsytj0413
Copy link

Maybe list.List should disable copy with

type noCopy struct{}

@gobwas
Copy link
Author

gobwas commented May 12, 2020

@lsytj0413 Yes, I know why it happens, thank you. Anyway, the comment about List tells that The zero value for List is an empty list ready to use..

@lsytj0413
Copy link

@lsytj0413 Yes, I know why it happens, thank you :) Anyway, the comment about List tells that The zero value for List is an empty list ready to use..

It's Zero Value when you reassign it,but after you called Remove it's length change to -1(removed one element from an empty list).

@gopherbot
Copy link

Change https://golang.org/cl/233326 mentions this issue: container/list: check the list state before mutation

@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 18, 2020
@cagedmantis cagedmantis added this to the Backlog milestone May 18, 2020
@cagedmantis
Copy link
Contributor

/cc @griesemer

@cagedmantis
Copy link
Contributor

I'm confirming that the code above reproduces. Resetting the list does not change the pointer to the list. The element still has the pointer to the original list. The Remove() function checks if the pointer to the list is the same as the pointer to list in the element.

@josharian
Copy link
Contributor

Resetting the list does not change the pointer to the list.

Seems like that could also cause memory leaks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants