Skip to content

Commit 75a50a1

Browse files
committed
C++: Understand formatting function varargs as needing null termination.
1 parent de8d84d commit 75a50a1

3 files changed

Lines changed: 11 additions & 1 deletion

File tree

cpp/ql/src/Security/CWE/CWE-131/NoSpaceForZeroTerminator.ql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ predicate terminationProblem(AllocationExpr malloc, string msg) {
3232
or
3333
// flows into likely null terminated string argument (such as `strcpy`, `strcat`)
3434
af.hasArrayWithUnknownSize(arg)
35+
or
36+
// flows into string argument to a formatting function (such as `printf`)
37+
exists(int n, FormatLiteral fl |
38+
fc.getArgument(arg) = fc.(FormattingFunctionCall).getConversionArgument(n) and
39+
fl = fc.(FormattingFunctionCall).getFormat() and
40+
fl.getConversionType(n) instanceof PointerType and // `%s`, `%ws` etc
41+
not fl.getConversionType(n) instanceof VoidPointerType and // exclude: `%p`
42+
not fl.hasPrecision(n) // exclude: `%.*s`
43+
)
3544
)
3645
) and
3746
msg = "This allocation does not include space to null-terminate the string."

cpp/ql/test/query-tests/Security/CWE/CWE-131/semmle/NoSpaceForZeroTerminator/NoSpaceForZeroTerminator.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
| test.c:49:20:49:25 | call to malloc | This allocation does not include space to null-terminate the string. |
66
| test.cpp:24:35:24:40 | call to malloc | This allocation does not include space to null-terminate the string. |
77
| test.cpp:45:28:45:33 | call to malloc | This allocation does not include space to null-terminate the string. |
8+
| test.cpp:55:28:55:33 | call to malloc | This allocation does not include space to null-terminate the string. |
89
| test.cpp:63:28:63:33 | call to malloc | This allocation does not include space to null-terminate the string. |
910
| test.cpp:71:28:71:33 | call to malloc | This allocation does not include space to null-terminate the string. |
1011
| test.cpp:79:28:79:33 | call to malloc | This allocation does not include space to null-terminate the string. |

cpp/ql/test/query-tests/Security/CWE/CWE-131/semmle/NoSpaceForZeroTerminator/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void decode(char *dest, char *src);
5151
void wdecode(wchar_t *dest, wchar_t *src);
5252

5353
void bad4(char *str) {
54-
// BAD -- zero-termination proved by wprintf (as parameter) [NOT DETECTED]
54+
// BAD -- zero-termination proved by wprintf (as parameter)
5555
char *buffer = (char *)malloc(strlen(str));
5656
decode(buffer, str);
5757
wprintf(L"%s", buffer);

0 commit comments

Comments
 (0)