Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cpp/ql/src/Critical/UseAfterFree.ql
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private predicate externalCallNeverDereferences(FormattingFunctionCall call, int

predicate isUse0(DataFlow::Node n, Expr e) {
e = n.asExpr() and
not isFree(_, e, _) and
not isFree(n, _, _) and
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.

This makes me a bit nervous. because isFree looks a (un)converted expressions explicitly. Was the change just made to bind n, or is there a deeper reason? If the former, can't we just drop the node argument of isUse0 completely?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think the change was leftover from an earlier attempt which I accidentality left in. I have now changed it to not have a DataFlow::Node argumebnt at all and call asExpr at call sites.

(
e = any(PointerDereferenceExpr pde).getOperand()
or
Expand All @@ -43,7 +43,7 @@ predicate isUse0(DataFlow::Node n, Expr e) {
or
// Assume any function without a body will dereference the pointer
exists(int i, Call call, Function f |
n.asExpr() = call.getArgument(i) and
e = call.getArgument(i) and
f = call.getTarget() and
not f.hasEntryPoint() and
// Exclude known functions we know won't dereference the pointer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
| test.cpp:128:15:128:16 | v4 |
| test.cpp:185:10:185:12 | cpy |
| test.cpp:199:10:199:12 | cpy |
| test.cpp:205:7:205:11 | ... = ... |
| test_free.cpp:11:10:11:10 | a |
| test_free.cpp:14:10:14:10 | a |
| test_free.cpp:16:10:16:10 | a |
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
| test.cpp:203:12:203:17 | call to malloc | This memory allocation may not be released at $@. | test.cpp:206:1:206:1 | return ... | this exit point |
| test_free.cpp:36:22:36:35 | ... = ... | This memory allocation may not be released at $@. | test_free.cpp:38:1:38:1 | return ... | this exit point |
6 changes: 6 additions & 0 deletions cpp/ql/test/query-tests/Critical/MemoryFreed/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,9 @@ void test_strndupa_dealloc() {
char *cpy = strndupa(msg, 4);
free(cpy); // BAD [NOT DETECTED]
}

void test_free_malloc() {
void *a = malloc(10);
void *b;
free(b = a);
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 think this needs some kind of // GOOD or // BAD comment. However, it's not clear to me to which query being tested here the comment applies.

}