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

cmd/compile: escape analysis says shadowed variable that's copied gets leaked #12389

Closed
dsnet opened this issue Aug 28, 2015 · 1 comment
Closed

Comments

@dsnet
Copy link
Member

dsnet commented Aug 28, 2015

Using go1.5

Compile the following package:

package foo

func foo(str string) string {
    str = string([]byte(str)) // Make copy
    return str
}

func bar(str string) string {
    str2 := string([]byte(str)) // Make copy
    return str2
}

I currently see (unrelated lines removed):

$ go build -gcflags=-m foo.go
./main.go:3: leaking param: str to result ~r1 level=0
./main.go:8: bar str does not escape

I expect to see (unrelated lines removed):

$ go build -gcflags=-m foo.go
./main.go:3: foo str does not escape
./main.go:8: bar str does not escape
@dsnet dsnet changed the title cmd/compile: escape analysis says copied variable gets leaked cmd/compile: escape analysis says shadowed variable that's copied gets leaked Aug 28, 2015
@rsc
Copy link
Contributor

rsc commented Oct 23, 2015

The variable is not "shadowed". It is reassigned. Escape analysis works on variables. One value assigned to the variable str does indeed escape. The result is therefore correct.

You might argue a broader point, that we should use a much more sophisticated escape analysis that works on values instead of variables, but I am skeptical that there's much evidence for that.

In any event when you really care about escape analysis results there is a clear workaround, as you've shown.

If you did use a shadowed variable that would work too:

{
    str := string([]byte(str))
    return str
}

Russ

@rsc rsc closed this as completed Oct 23, 2015
@golang golang locked and limited conversation to collaborators Oct 24, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants