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: -N flag cause return address of structure on stack instead of address of newly allocated one. #8719

Closed
gopherbot opened this issue Sep 13, 2014 · 4 comments
Milestone

Comments

@gopherbot
Copy link

by daniel.crettol:

package main

import "fmt"

type A struct {
    i int
}

func new1() *A {
    return &A{10}
}

func new2() *A {
    p := A{}
    p.i = 11
    return &p
}

func dummy(p *A) {
}

func new3() *A {
    p := A{}
    p.i = 12
    dummy(&p)
    return &p
}

func main() {
    a1 := new1()
    fmt.Printf("a1=%p %+v\n", a1, a1)

    a2 := new2()
    fmt.Printf("a2=%p %+v\n", a2, a2)

    a3 := new3()
    fmt.Printf("a3=%p %+v\n", a3, a3)
}

$ go build -gcflags '-N' bugtst.go && ./bugtst
a1=0xc208000150 &{i:10}
a2=0x7f31b606ddc0 &{i:11}
a3=0xc2080001e0 &{i:12}

$ go build -gcflags '' bugtst.go && ./bugtst
a1=0xc208000150 &{i:10}
a2=0xc2080001a8 &{i:11}
a3=0xc2080001e8 &{i:12}

$ go version
go version go1.3.1 linux/amd64

I had this pattern in a program and the struct generated by the new2 function was
corrupted after returned.

It seems to me that the compiler does not generate a copy of the structure when using
the new2 function but does it when we call the new1 and new2 function.

As the -N -L flags seems to be required to debug with gcc, it's really annoying.

Happy debugging :)
@ianlancetaylor
Copy link
Contributor

Comment 1:

Labels changed: added repo-main, release-go1.4.

@josharian
Copy link
Contributor

Comment 2:

Possible duplicate of issue #8585.

@davecheney
Copy link
Contributor

Comment 3:

This may be related to issue #8703. I have asked the OP to report if they were using -N

@josharian
Copy link
Contributor

Comment 4:

Status changed to Duplicate.

Merged into issue #8585.

@rsc rsc added this to the Go1.4 milestone Apr 14, 2015
@rsc rsc removed the release-go1.4 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 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

5 participants