-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Cpp: new experimental query cpp/guarded-free #16331
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d7c784e
Initial commit of experimental query cpp/guarded-free.
mario-campos 3195f0c
Use more specific `hasGlobalName()` for stdlib function free(3)
mario-campos c480431
C++: simplify cpp/guarded-free
mario-campos 5a7a1dc
C++: forgot to import semmle.code.cpp.controlflow.Guards
mario-campos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Initial commit of experimental query cpp/guarded-free.
- Loading branch information
commit d7c784ef2f31bb39cc9418d984bd42c03bb1fb25
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
void test() | ||
{ | ||
char *foo = malloc(100); | ||
|
||
// BAD | ||
if (foo) | ||
free(foo); | ||
|
||
// GOOD | ||
free(foo); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!DOCTYPE qhelp SYSTEM "qhelp.dtd"> | ||
<qhelp> | ||
<overview> | ||
<p>The <code>free</code> function, which deallocates heap memory, may accept a NULL pointer and take no action. Therefore, it is unnecessary to check its argument for the value of NULL before a function call to <code>free</code>. As such, these guards may hinder performance and readability.</p> | ||
</overview> | ||
<recommendation> | ||
<p>A function call to <code>free</code> should not depend upon the value of its argument. Delete the <code>if</code> condition preceeding a function call to <code>free</code> when its only purpose is to check the value of the pointer to be freed.</p> | ||
</recommendation> | ||
<example> | ||
<sample src = "GuardedFree.cpp" /> | ||
</example> | ||
<references> | ||
<li> | ||
The Open Group Base Specifications Issue 7, 2018 Edition: | ||
<a href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/free.html">free - free allocated memory</a> | ||
</li> | ||
</references> | ||
</qhelp> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that re
8000
veals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* @name Guarded Free | ||
* @description NULL-condition guards before function calls to the memory-deallocation | ||
* function free(3) are unnecessary, because passing NULL to free(3) is a no-op. | ||
* @kind problem | ||
* @problem.severity recommendation | ||
* @precision very-high | ||
* @id cpp/guarded-free | ||
* @tags maintainability | ||
* readability | ||
* experimental | ||
*/ | ||
|
||
import cpp | ||
|
||
class FreeCall extends FunctionCall { | ||
FreeCall() { this.getTarget().hasName("free") } | ||
} | ||
|
||
from IfStmt stmt, FreeCall fc, Variable v | ||
where | ||
stmt.getThen() = fc.getEnclosingStmt() and | ||
( | ||
stmt.getCondition() = v.getAnAccess() and | ||
fc.getArgument(0) = v.getAnAccess() | ||
or | ||
exists(PointerDereferenceExpr cond, PointerDereferenceExpr arg | | ||
fc.getArgument(0) = arg and | ||
stmt.getCondition() = cond and | ||
cond.getOperand+() = v.getAnAccess() and | ||
arg.getOperand+() = v.getAnAccess() | ||
) | ||
or | ||
exists(ArrayExpr cond, ArrayExpr arg | | ||
fc.getArgument(0) = arg and | ||
stmt.getCondition() = cond and | ||
cond.getArrayBase+() = v.getAnAccess() and | ||
arg.getArrayBase+() = v.getAnAccess() | ||
) | ||
or | ||
exists(NEExpr eq | | ||
fc.getArgument(0) = v.getAnAccess() and | ||
stmt.getCondition() = eq and | ||
eq.getAnOperand() = v.getAnAccess() and | ||
eq.getAnOperand().getValue() = "0" | ||
) | ||
) | ||
select stmt, "unnecessary NULL check before call to $@", fc, "free" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.