Skip to content

Commit 7e7dfe2

Browse files
committed
C++: Understand format arguments.
1 parent 91a8b9f commit 7e7dfe2

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ predicate variableMustBeNullTerminated(VariableAccess va) {
9393
fc.getArgument(i) = va
9494
)
9595
or
96+
// String argument to a formatting function (such as `printf`)
97+
exists(int n, FormatLiteral fl |
98+
fc.(FormattingFunctionCall).getConversionArgument(n) = va and
99+
fl = fc.(FormattingFunctionCall).getFormat() and
100+
fl.getConversionType(n) instanceof PointerType and // `%s`, `%ws` etc
101+
not fl.getConversionType(n) instanceof VoidPointerType and // exclude: `%p`
102+
not fl.hasPrecision(n) // exclude: `%.*s`
103+
)
104+
or
96105
// Call to a wrapper function that requires null termination
97106
// (not itself adding a null terminator)
98107
exists(Function wrapper, int i, Parameter p, VariableAccess use |

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
| test.cpp:392:17:392:22 | buffer | Variable $@ may not be null terminated. | test.cpp:390:8:390:13 | buffer | buffer |
2525
| test.cpp:398:18:398:23 | buffer | Variable $@ may not be null terminated. | test.cpp:396:8:396:13 | buffer | buffer |
2626
| test.cpp:444:10:444:15 | buffer | Variable $@ may not be null terminated. | test.cpp:442:8:442:13 | buffer | buffer |
27+
| test.cpp:450:16:450:21 | buffer | Variable $@ may not be null terminated. | test.cpp:448:8:448: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
@@ -447,7 +447,7 @@ void test_printf(char *str)
447447
{
448448
char buffer[1024];
449449

450-
printf("%s", buffer); // BAD [NOT DETECTED]
450+
printf("%s", buffer); // BAD
451451
}
452452

453453
{

0 commit comments

Comments
 (0)