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/gc: returning a function parameter causes escape #3702

Closed
bradfitz opened this issue Jun 4, 2012 · 6 comments
Closed

cmd/gc: returning a function parameter causes escape #3702

bradfitz opened this issue Jun 4, 2012 · 6 comments
Milestone

Comments

@bradfitz
Copy link
Contributor

bradfitz commented Jun 4, 2012

The buf below shouldn't escape to the heap.

package main

func nothing() {
}

func f(b []byte) []byte { // leaking param: b
        b[0] = 'H'
        b[1] = 'i'
        nothing() // (force f to not inline.)
        return b[:2]
}

func main() {
        var buf [10]byte // moved to heap: buf
        hi := f(buf[:]) // buf escapes to heap
        for i, c := range hi {
                println(i, c)
        }
}

$ go tool 6g -s -m e.go
e.go:3: can inline nothing
e.go:9: inlining call to nothing
e.go:14: moved to heap: buf
e.go:6: leaking param: b
e.go:15: buf escapes to heap
@rsc
Copy link
Contributor

rsc commented Jun 4, 2012

Comment 1:

That is exactly what I'm working on. I am probably going to have to
change gears for a week or two, but it's at the top of my compiler
work list.

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented Sep 12, 2012

Comment 2:

http://golang.org/cl/6339053/ is supposed to fix this. lvd may look into why
it's broken, or if he doesn't get to it I will.

@rsc
Copy link
Contributor

rsc commented Sep 12, 2012

Comment 3:

Labels changed: added priority-later, removed priority-triage.

@rsc
Copy link
Contributor

rsc commented Sep 12, 2012

Comment 4:

Labels changed: added go1.1.

@lvdlvd
Copy link

lvdlvd commented Oct 29, 2012

@bradfitz
Copy link
Contributor Author

Comment 6:

Verified the example at top doesn't example.

Status changed to Fixed.

Attachments:

  1. e.go (731 bytes)

@rsc rsc added this to the Go1.1 milestone Apr 14, 2015
@rsc rsc removed the go1.1 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
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