Skip to content

Commit a31e983

Browse files
committed
C++: Also allow single statement blocks in cpp/guarded-free
1 parent c86bbbb commit a31e983

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

cpp/ql/src/experimental/Best Practices/GuardedFree.ql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ from GuardCondition gc, FreeCall fc, Variable v, BasicBlock bb
2222
where
2323
gc.ensuresEq(v.getAnAccess(), 0, bb, false) and
2424
fc.getArgument(0) = v.getAnAccess() and
25-
bb = fc.getEnclosingStmt() and
25+
bb = fc.getBasicBlock() and
26+
(
27+
bb = fc.getEnclosingStmt()
28+
or
29+
strictcount(bb.(BlockStmt).getAStmt()) = 1
30+
) and
2631
strictcount(BasicBlock bb2 | gc.ensuresEq(_, 0, bb2, _) | bb2) = 1
2732
select gc, "unnecessary NULL check before call to $@", fc, "free"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
| test.cpp:5:7:5:7 | x | unnecessary NULL check before call to $@ | test.cpp:6:5:6:8 | call to free | free |
2+
| test.cpp:10:7:10:7 | x | unnecessary NULL check before call to $@ | test.cpp:11:5:11:8 | call to free | free |
23
| test.cpp:31:7:31:24 | ... \|\| ... | unnecessary NULL check before call to $@ | test.cpp:35:3:35:6 | call to free | free |
4+
| test.cpp:42:7:42:7 | x | unnecessary NULL check before call to $@ | test.cpp:43:5:43:8 | call to free | free |
5+
| test.cpp:49:7:49:7 | x | unnecessary NULL check before call to $@ | test.cpp:50:5:50:8 | call to free | free |
6+
| test.cpp:75:7:75:7 | x | unnecessary NULL check before call to $@ | test.cpp:76:5:76:14 | call to free | free |
7+
| test.cpp:81:7:81:7 | x | unnecessary NULL check before call to $@ | test.cpp:85:5:85:8 | call to free | free |
38
| test.cpp:94:12:94:12 | x | unnecessary NULL check before call to $@ | test.cpp:94:3:94:13 | call to free | free |
49
| test.cpp:106:7:106:18 | ... != ... | unnecessary NULL check before call to $@ | test.cpp:107:5:107:8 | call to free | free |
510
| test.cpp:113:7:113:18 | ... != ... | unnecessary NULL check before call to $@ | test.cpp:114:17:114:20 | call to free | free |

cpp/ql/test/experimental/query-tests/Best Practices/GuardedFree/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ bool test8(char *x) {
7272
#endif
7373

7474
void test9(char *x) {
75-
if (x) { // GOOD: macro may make free behave unexpectedly when compiled differently
75+
if (x) { // GOOD [FALSE POSITIVE]: macro may make free behave unexpectedly when compiled differently
7676
my_free(x);
7777
}
7878
}
7979

8080
void test10(char *x) {
81-
if (x) { // GOOD: #ifdef may make free behave unexpectedly when compiled differently
81+
if (x) { // GOOD [FALSE POSITIVE]: #ifdef may make free behave unexpectedly when compiled differently
8282
#ifdef FOO
8383
free(x - 1);
8484
#else

0 commit comments

Comments
 (0)