Skip to content

Commit 91a8b9f

Browse files
committed
C++: Add suggested test (and a good variant).
1 parent 8debae1 commit 91a8b9f

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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
@@ -23,3 +23,4 @@
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 |

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 [NOT DETECTED]
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)