Skip to content

Commit b0836a6

Browse files
authored
Merge pull request github#6757 from geoffw0/impropnulltest2
C++: Small improvement to cpp/improper-null-termination
2 parents 06b36f7 + 7e7dfe2 commit b0836a6

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@
2323
| test.cpp:365:19:365:25 | buffer2 | Variable $@ may not be null terminated. | test.cpp:363:8:363:14 | buffer2 | buffer2 |
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 |
26+
| 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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,36 @@ void test_read_fread(int read_src, FILE *s)
433433
strlen(buffer); // GOOD
434434
}
435435
}
436+
437+
int printf(const char *format, ...);
438+
439+
void test_printf(char *str)
440+
{
441+
{
442+
char buffer[1024];
443+
444+
printf(buffer, ""); // BAD
445+
}
446+
447+
{
448+
char buffer[1024];
449+
450+
printf("%s", buffer); // BAD
451+
}
452+
453+
{
454+
size_t len = strlen(str);
455+
char *copied_str = (char *)malloc(len);
456+
457+
memcpy(copied_str, str, len);
458+
printf("%s", copied_str); // BAD [NOT DETECTED]
459+
}
460+
461+
{
462+
size_t len = strlen(str);
463+
char *copied_str = (char *)malloc(len + 1);
464+
465+
memcpy(copied_str, str, len + 1);
466+
printf("%s", copied_str); // GOOD
467+
}
468+
}

0 commit comments

Comments
 (0)