Skip to content

Commit 8d0cfb4

Browse files
committed
C++: Merge tests from 'cpp/access-memory-location-after-end-buffer-strncat' into the tests from 'cpp/unsafe-strncat'.
1 parent 5300dd2 commit 8d0cfb4

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
| test.c:24:2:24:8 | call to strncat | Potentially unsafe call to strncat. |
2+
| test.c:46:3:46:9 | call to strncat | Potentially unsafe call to strncat. |
3+
| test.c:68:3:68:9 | call to strncat | Potentially unsafe call to strncat. |

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,31 @@ void bad1(char *s) {
3939
strncat(buf, ".", 1); // BAD [NOT DETECTED] -- Need to check if any space is left
4040
}
4141

42+
43+
void strncat_test1(char *s) {
44+
char buf[80];
45+
strncat(buf, s, sizeof(buf) - strlen(buf) - 1); // GOOD
46+
strncat(buf, s, sizeof(buf) - strlen(buf)); // BAD
47+
}
48+
49+
void* malloc(size_t);
50+
51+
void strncat_test2(char *s) {
52+
int len = 80;
53+
char* buf = (char *)malloc(len);
54+
strncat(buf, s, len - strlen(buf) - 1); // GOOD
55+
strncat(buf, s, len - strlen(buf)); // BAD [NOT DETECTED]
56+
}
57+
58+
struct buffers
59+
{
60+
char array[50];
61+
char* pointer;
62+
};
63+
64+
void strncat_test3(char* s, struct buffers* buffers) {
65+
unsigned len_array = strlen(buffers->array);
66+
unsigned max_size = sizeof(buffers->array);
67+
unsigned free_size = max_size - len_array;
68+
strncat(buffers->array, s, free_size); // BAD
69+
}

0 commit comments

Comments
 (0)