Skip to content

Commit 5379b25

Browse files
committed
C++: Add tests.
1 parent f17c06a commit 5379b25

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-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
@@ -27,3 +27,4 @@
2727
| test.cpp:454:18:454:23 | buffer | Variable $@ may not be null terminated. | test.cpp:452:8:452:13 | buffer | buffer |
2828
| test.cpp:513:10:513:15 | buffer | Variable $@ may not be null terminated. | test.cpp:511:8:511:13 | buffer | buffer |
2929
| test.cpp:519:16:519:21 | buffer | Variable $@ may not be null terminated. | test.cpp:517:8:517:13 | buffer | buffer |
30+
| test.cpp:558:10:558:16 | buffer2 | Variable $@ may not be null terminated. | test.cpp:553:8:553:14 | buffer2 | buffer2 |

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,3 +536,51 @@ void test_printf(char *str)
536536
}
537537
}
538538

539+
void test_reassignment()
540+
{
541+
{
542+
char buffer1[1024];
543+
char buffer2[1024];
544+
char *buffer_ptr = buffer1;
545+
546+
buffer_ptr = buffer2;
547+
strcpy(buffer_ptr, "content"); // null terminates buffer2
548+
strdup(buffer2); // GOOD
549+
}
550+
551+
{
552+
char buffer1[1024];
553+
char buffer2[1024];
554+
char *buffer_ptr = buffer1;
555+
556+
strcpy(buffer_ptr, "content"); // null terminates buffer1
557+
buffer_ptr = buffer2;
558+
strdup(buffer2); // BAD
559+
}
560+
561+
{
562+
char buffer1[1024];
563+
char buffer2[1024];
564+
char *buffer_ptr = buffer1;
565+
566+
while (cond())
567+
{
568+
buffer_ptr = buffer2;
569+
strcpy(buffer_ptr, "content"); // null terminates buffer2
570+
strdup(buffer2); // GOOD
571+
}
572+
}
573+
574+
{
575+
char buffer1[1024];
576+
char buffer2[1024];
577+
char *buffer_ptr = buffer1;
578+
579+
while (cond())
580+
{
581+
strcpy(buffer_ptr, "content"); // null terminates buffer1 or buffer2
582+
buffer_ptr = buffer2;
583+
strdup(buffer2); // BAD [NOT DETECTED]
584+
}
585+
}
586+
}

0 commit comments

Comments
 (0)