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

defer + named-return-value false negative #89

Open
chrchang opened this issue May 24, 2024 · 2 comments
Open

defer + named-return-value false negative #89

chrchang opened this issue May 24, 2024 · 2 comments

Comments

@chrchang
Copy link

chrchang commented May 24, 2024

package main

import (
        "errors"
        "fmt"
)

func decorateErr(errPtr *error) {
        *errPtr = fmt.Errorf("blah blah blah %v", *errPtr)
}

func sampleFunc() (err error) {
        defer decorateErr(&err)
        err = errors.New("this is an ineffective assignment")
        return nil
}

func main() {
        err := sampleFunc()
        fmt.Printf("%v", err)
}

The ineffective assignment is recognized if I remove "defer decorateErr(&err)".

@gordonklaus
Copy link
Owner

Thanks for the report.

The reason this ineffective assignment is not recognized is actually not due to the defer but to the &err. Whenever the tool sees &x, it says "x escapes" and gives up on tracking it, because it's a more complicated problem to track where pointers flow through a program. As far as the tool is concerned, decorateErr(&err) (as seen from the outside) might possibly use *err instead of just assigning to it.

Until the tool is completely rewritten to use a full pointer analysis (if ever), it's beyond its scope to recognize this case.

@77Misha
Copy link

77Misha commented Nov 22, 2024

Thanks for the report.

The reason this ineffective assignment is not recognized is actually not due to the defer but to the &err. Whenever the tool sees &x, it says "x escapes" and gives up on tracking it, because it's a more complicated problem to track where pointers flow through a program. As far as the tool is concerned, decorateErr(&err) (as seen from the outside) might possibly use *err instead of just assigning to it.

Until the tool is completely rewritten to use a full pointer analysis (if ever), it's beyond its scope to recognize this case.

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

No branches or pull requests

3 participants