Skip to content

Commit 9572b9d

Browse files
committed
C++: Add test where buffer initialized with literal is reassigned an allocation
1 parent c0dec21 commit 9572b9d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
| tests2.cpp:59:3:59:10 | call to snprintf | This 'call to snprintf' operation is limited to 13 bytes but the destination is only 0 bytes. |
2+
| tests2.cpp:59:3:59:10 | call to snprintf | This 'call to snprintf' operation is limited to 13 bytes but the destination is only 2 bytes. |
3+
| tests2.cpp:63:3:63:10 | call to snprintf | This 'call to snprintf' operation is limited to 13 bytes but the destination is only 0 bytes. |
4+
| tests2.cpp:63:3:63:10 | call to snprintf | This 'call to snprintf' operation is limited to 13 bytes but the destination is only 3 bytes. |
15
| tests.c:43:3:43:10 | call to snprintf | This 'call to snprintf' operation is limited to 111 bytes but the destination is only 110 bytes. |
26
| tests.c:46:3:46:10 | call to snprintf | This 'call to snprintf' operation is limited to 111 bytes but the destination is only 110 bytes. |

cpp/ql/test/query-tests/Security/CWE/CWE-120/semmle/tests/tests2.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ void *realloc(void *ptr, size_t size);
66
void *calloc(size_t nmemb, size_t size);
77
void free(void *ptr);
88
wchar_t *wcscpy(wchar_t *s1, const wchar_t *s2);
9+
int snprintf(char *s, size_t n, const char *format, ...);
910

1011
// --- Semmle tests ---
1112

@@ -46,3 +47,18 @@ void tests2() {
4647
wcscpy(buffer, L"12345678"); // BAD: buffer overflow
4748
delete [] buffer;
4849
}
50+
51+
char* dest1 = "a";
52+
char* dest2 = "abcdefghijklmnopqrstuvwxyz";
53+
54+
void test3() {
55+
const char src[] = "abcdefghijkl";
56+
dest1 = (char*)malloc(sizeof(src));
57+
if (!dest1)
58+
return;
59+
snprintf(dest1, sizeof(src), "%s", src); // GOOD [FALSE POSITIVE]
60+
dest2 = (char*)malloc(3);
61+
if (!dest2)
62+
return;
63+
snprintf(dest2, sizeof(src), "%s", src); // BAD: buffer overflow
64+
}

0 commit comments

Comments
 (0)