Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Go: Add query for redundant calls to recover#147

Merged
max-schaefer merged 3 commits intogithub:masterfrom
owen-mc:redundant-recover
May 19, 2020
Merged

Go: Add query for redundant calls to recover#147
max-schaefer merged 3 commits intogithub:masterfrom
owen-mc:redundant-recover

Conversation

@owen-mc
Copy link
Copy Markdown
Contributor

@owen-mc owen-mc commented May 18, 2020

It's very easy to think you are using recover correctly when you aren't. The two patterns I've found are (a) deferring a call to recover directly, (b) not deferring the call to recover and (c) deferring a function which calls a function which calls recover - the panic doesn't propagate down the stack, only back up it. This query finds direct calls to recover and calls to recover in functions which are never called in a defer statement (but which are called at least once). I haven't done a perfomance comparison.

@owen-mc owen-mc requested a review from shati-patel May 18, 2020 15:21
Copy link
Copy Markdown
Contributor

@max-schaefer max-schaefer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall, I just have some minor stylistic comments and suggestions. We won't need a performance evaluation for this query, since it is very simple and syntactic. Can I just confirm that you have reviewed results on LGTM.com and are satisfied that most of them are true positives?

</p>
<sample src="RedundantRecover1.go" />
<p>
This problem can be deferring the call to the function which calls<code>recover</code>:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't parse this sentence. Is there a "fixed by" missing somewhere?

f = recoverCall.getEnclosingCallable() and
(
isDeferred(recoverCall) and
msg = "Deferred calls to 'recover' have no effect"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency:

Suggested change
msg = "Deferred calls to 'recover' have no effect"
msg = "Deferred calls to 'recover' have no effect."

not isDeferred(f.getACall()) and
msg = "This call to 'recover' has no effect because $@ is never called using a defer statement."
)
select recoverCall, msg, f, f.getName()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not all functions have a name, so f.getName() may be undefined, causing the alert to be suppressed. I'd suggest instead using "the enclosing function":

Suggested change
select recoverCall, msg, f, f.getName()
select recoverCall, msg, f, "the enclosing function"

@@ -0,0 +1,3 @@
| RedundantRecover1.go:6:5:6:13 | call to recover | This call to 'recover' has no effect because $@ is never called using a defer statement. | RedundantRecover1.go:5:1:9:1 | function declaration | callRecover1 |
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| RedundantRecover1.go:6:5:6:13 | call to recover | This call to 'recover' has no effect because $@ is never called using a defer statement. | RedundantRecover1.go:5:1:9:1 | function declaration | callRecover1 |
| RedundantRecover1.go:6:5:6:13 | call to recover | This call to 'recover' has no effect because $@ is never called using a defer statement. | RedundantRecover1.go:5:1:9:1 | function declaration | the enclosing function |

@@ -0,0 +1,3 @@
| RedundantRecover1.go:6:5:6:13 | call to recover | This call to 'recover' has no effect because $@ is never called using a defer statement. | RedundantRecover1.go:5:1:9:1 | function declaration | callRecover1 |
| RedundantRecover2.go:4:8:4:16 | call to recover | Deferred calls to 'recover' have no effect | RedundantRecover2.go:3:1:6:1 | function declaration | fun2 |
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| RedundantRecover2.go:4:8:4:16 | call to recover | Deferred calls to 'recover' have no effect | RedundantRecover2.go:3:1:6:1 | function declaration | fun2 |
| RedundantRecover2.go:4:8:4:16 | call to recover | Deferred calls to 'recover' have no effect. | RedundantRecover2.go:3:1:6:1 | function declaration | fun2 |

@@ -0,0 +1,3 @@
| RedundantRecover1.go:6:5:6:13 | call to recover | This call to 'recover' has no effect because $@ is never called using a defer statement. | RedundantRecover1.go:5:1:9:1 | function declaration | callRecover1 |
| RedundantRecover2.go:4:8:4:16 | call to recover | Deferred calls to 'recover' have no effect | RedundantRecover2.go:3:1:6:1 | function declaration | fun2 |
| tst.go:8:5:8:13 | call to recover | This call to 'recover' has no effect because $@ is never called using a defer statement. | tst.go:5:1:11:1 | function declaration | callRecover3 |
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| tst.go:8:5:8:13 | call to recover | This call to 'recover' has no effect because $@ is never called using a defer statement. | tst.go:5:1:11:1 | function declaration | callRecover3 |
| tst.go:8:5:8:13 | call to recover | This call to 'recover' has no effect because $@ is never called using a defer statement. | tst.go:5:1:11:1 | function declaration | the enclosing function |

@shati-patel
Copy link
Copy Markdown
Contributor

I think the docs look good, but would it also be possible to see an HTML preview of the query help as a sanity check? (I'm not sure where those previews usually come from 👀 )

@max-schaefer
Copy link
Copy Markdown
Contributor

HTML preview is on its way (you're too fast 🙂 ).

@owen-mc
Copy link
Copy Markdown
Contributor Author

owen-mc commented May 18, 2020

I've added a commit to fix address your comments, @max-schaefer . (Let me know if I should squash it.)

Yes, I've seen the results on LGTM.com and they seemed to be almost entirely true positives. I put the precision as 'high' because I wasn't sure what the bar for 'very high' is.

@max-schaefer
Copy link
Copy Markdown
Contributor

Copy link
Copy Markdown
Contributor

@shati-patel shati-patel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the query help looks good 😊 I've just added a small suggestion to the change note.

Comment thread change-notes/2020-05-18-redundant-recover.md Outdated
Co-authored-by: Shati Patel <42641846+shati-patel@users.noreply.github.com>
@max-schaefer max-schaefer merged commit 6d93f48 into github:master May 19, 2020
@owen-mc owen-mc deleted the redundant-recover branch May 19, 2020 08:27
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants