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

control variable used on defer #80

Closed
amery opened this issue Mar 31, 2023 · 1 comment
Closed

control variable used on defer #80

amery opened this issue Mar 31, 2023 · 1 comment

Comments

@amery
Copy link

amery commented Mar 31, 2023

on some functions, to make error handling cleaner, I put a sentinel on defer to take care of undoing incomplete "transactions". it looks like this:

func doThings(...) error {
    var ok bool
    defer undoThingsUnless(ok, ...)
    // ...
    if  err != nil {
        return err
    }
    // ...
    if  err != nil {
        return err
    }
   
    ok = true
    return nil
}

actual code example: https://github.com/darvaza-proxy/darvaza/blob/master/shared/net/bind/listen.go#L114 or https://github.com/darvaza-proxy/darvaza/blob/master/shared/net/bind/bind.go#L165

I don't know the version, but I just noticed it on goreportcard. https://goreportcard.com/report/github.com/darvaza-proxy/darvaza/shared

@gordonklaus
Copy link
Owner

Hi @amery .

Your code doesn't work the way you think it does. Arguments to deferred function calls are evaluated at the defer statement. You are always passing false to undoThingsUnless. Try passing &ok instead.

When you went to open this issue, did you see an issue template called "False positive"? Did you ignore it, and why? (E.g., are you not familiar with the term "false positive"?) I ask because I created that issue template for the sake of reducing spurious reports like this one — and yet here we are. Can you think of a better name for the issue template? I'd appreciate your feedback :)

@gordonklaus gordonklaus closed this as not planned Won't fix, can't repro, duplicate, stale Mar 31, 2023
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

2 participants