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 · May be fixed by #39015
Open

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

gobwas opened this issue May 12, 2020 · 8 comments · May be fixed by #39015

Comments

@gobwas
Copy link

@gobwas 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

@lsytj0413 lsytj0413 commented May 12, 2020

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)

Loading

@lsytj0413
Copy link

@lsytj0413 lsytj0413 commented May 12, 2020

Maybe list.List should disable copy with

type noCopy struct{}

Loading

@gobwas
Copy link
Author

@gobwas 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..

Loading

@lsytj0413
Copy link

@lsytj0413 lsytj0413 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..

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

Loading

@gobwas gobwas linked a pull request that will close this issue May 12, 2020
@gopherbot
Copy link

@gopherbot gopherbot commented May 12, 2020

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

Loading

@cagedmantis cagedmantis added this to the Backlog milestone May 18, 2020
@cagedmantis
Copy link
Contributor

@cagedmantis cagedmantis commented May 18, 2020

Loading

@cagedmantis
Copy link
Contributor

@cagedmantis cagedmantis commented May 18, 2020

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.

Loading

@josharian
Copy link
Contributor

@josharian josharian commented May 18, 2020

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

Seems like that could also cause memory leaks.

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

5 participants