Skip to content

Commit b581723

Browse files
committed
C++: Ignore complex guards and the comma operator
1 parent 176acab commit b581723

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,7 @@ where
4040
strictcount(bb.(BlockStmt).getAStmt()) = 1
4141
) and
4242
strictcount(BasicBlock bb2 | gc.ensuresEq(_, 0, bb2, _) | bb2) = 1 and
43-
not isAffectedByMacro(fc, bb)
43+
not isAffectedByMacro(fc, bb) and
44+
not (gc instanceof BinaryOperation and not gc instanceof ComparisonOperation) and
45+
not exists(CommaExpr c | c.getAChild*() = fc)
4446
select gc, "unnecessary NULL check before call to $@", fc, "free"
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
| test.cpp:5:7:5:7 | x | unnecessary NULL check before call to $@ | test.cpp:6:5:6:8 | call to free | free |
22
| test.cpp:10:7:10:7 | x | unnecessary NULL check before call to $@ | test.cpp:11:5:11:8 | call to free | free |
3-
| test.cpp:31:7:31:24 | ... \|\| ... | unnecessary NULL check before call to $@ | test.cpp:35:3:35:6 | call to free | free |
43
| test.cpp:42:7:42:7 | x | unnecessary NULL check before call to $@ | test.cpp:43:5:43:8 | call to free | free |
54
| test.cpp:49:7:49:7 | x | unnecessary NULL check before call to $@ | test.cpp:50:5:50:8 | call to free | free |
65
| test.cpp:106:7:106:18 | ... != ... | unnecessary NULL check before call to $@ | test.cpp:107:5:107:8 | call to free | free |
7-
| 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
@@ -28,7 +28,7 @@ void test3(int *x, bool b) {
2828
}
2929

3030
bool test4(char *x, char *y) {
31-
if (!x || strcmp(x, y)) { // GOOD [FALSE POSITIVE]: x is being accessed in the guard and return value depends on x
31+
if (!x || strcmp(x, y)) { // GOOD: x is being accessed in the guard and return value depends on x
3232
free(x);
3333
return true;
3434
}
@@ -110,6 +110,6 @@ void test13(char *x) {
110110
void inspect(char *x);
111111

112112
void test14(char *x) {
113-
if (x != nullptr) // GOOD [FALSE POSITIVE]: x might be accessed in the first operand of the comma operator
113+
if (x != nullptr) // GOOD: x might be accessed in the first operand of the comma operator
114114
inspect(x), free(x);
115115
}

0 commit comments

Comments
 (0)