Skip to content

Commit ada3080

Browse files
committed
C++: Exclude results where identity-like functions obscure operations on a variable.
1 parent 11d7a0b commit ada3080

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

cpp/ql/lib/semmle/code/cpp/commons/NullTermination.qll

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,27 @@ predicate mayAddNullTerminator(Expr e, VariableAccess va) {
4545
ae.getRValue().getAChild*() = va
4646
)
4747
or
48-
// Function call: library function, varargs function, function
49-
// containing assembler code, or function where the relevant
50-
// parameter is potentially added a null terminator.
48+
// Function calls...
5149
exists(Call c, Function f, int i |
5250
e = c and
5351
f = c.getTarget() and
5452
not functionArgumentMustBeNullTerminated(f, i) and
5553
c.getAnArgumentSubExpr(i) = va
5654
|
55+
// library function
5756
not f.hasEntryPoint()
5857
or
58+
// function where the relevant parameter is potentially added a null terminator
5959
mayAddNullTerminator(_, f.getParameter(i).getAnAccess())
6060
or
61+
// varargs function
6162
f.isVarargs() and i >= f.getNumberOfParameters()
6263
or
64+
// function containing assembler code
6365
exists(AsmStmt s | s.getEnclosingFunction() = f)
66+
or
67+
// function where the relevant parameter is returned (leaking it)
68+
exists(ReturnStmt rs | rs.getEnclosingFunction() = f and rs.getExpr().getAChild*() = f.getParameter(i).getAnAccess())
6469
)
6570
or
6671
// Call without target (e.g., function pointer call)

cpp/ql/test/query-tests/Likely Bugs/Memory Management/ImproperNullTermination/ImproperNullTermination.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
| test.cpp:285:10:285:15 | buffer | Variable $@ may not be null terminated. | test.cpp:282:8:282:13 | buffer | buffer |
1818
| test.cpp:302:10:302:16 | buffer2 | Variable $@ may not be null terminated. | test.cpp:297:8:297:14 | buffer2 | buffer2 |
1919
| test.cpp:314:10:314:15 | buffer | Variable $@ may not be null terminated. | test.cpp:310:8:310:13 | buffer | buffer |
20-
| test.cpp:328:10:328:15 | buffer | Variable $@ may not be null terminated. | test.cpp:325:8:325:13 | buffer | buffer |
2120
| test.cpp:336:18:336:23 | buffer | Variable $@ may not be null terminated. | test.cpp:335:8:335:13 | buffer | buffer |
2221
| test.cpp:355:11:355:16 | buffer | Variable $@ may not be null terminated. | test.cpp:350:8:350:13 | buffer | buffer |
2322
| test.cpp:364:11:364:16 | buffer | Variable $@ may not be null terminated. | test.cpp:359:8:359:13 | buffer | buffer |

cpp/ql/test/query-tests/Likely Bugs/Memory Management/ImproperNullTermination/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ void test_strcat()
325325
char buffer[1024];
326326

327327
clearBuffer(id(buffer), 1024);
328-
strcat(buffer, "content"); // GOOD [FALSE POSITIVE]
328+
strcat(buffer, "content"); // GOOD
329329
}
330330
}
331331

0 commit comments

Comments
 (0)