File tree Expand file tree Collapse file tree
cpp/ql/test/query-tests/Security/CWE/CWE-131/semmle/NoSpaceForZeroTerminator Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- | test.c:15 :20:15 :25 | call to malloc | This allocation does not include space to null-terminate the string. |
2- | test.c:29 :20:29 :25 | call to malloc | This allocation does not include space to null-terminate the string. |
3- | test.c:44 :20:44 :25 | call to malloc | This allocation does not include space to null-terminate the string. |
4- | test.cpp:18 :35:18 :40 | call to malloc | This allocation does not include space to null-terminate the string. |
1+ | test.c:16 :20:16 :25 | call to malloc | This allocation does not include space to null-terminate the string. |
2+ | test.c:32 :20:32 :25 | call to malloc | This allocation does not include space to null-terminate the string. |
3+ | test.c:49 :20:49 :25 | call to malloc | This allocation does not include space to null-terminate the string. |
4+ | test.cpp:24 :35:24 :40 | call to malloc | This allocation does not include space to null-terminate the string. |
Original file line number Diff line number Diff line change 77typedef unsigned long size_t ;
88void * malloc (size_t size );
99void free (void * ptr );
10+ char * strcpy (char * s1 , const char * s2 );
1011
1112//// Test code /////
1213
1314void bad0 (char * str ) {
1415 // BAD -- Not allocating space for '\0' terminator
1516 char * buffer = malloc (strlen (str ));
17+ strcpy (buffer , str );
1618 free (buffer );
1719}
1820
1921void good0 (char * str ) {
2022 // GOOD -- Allocating extra byte for terminator
2123 char * buffer = malloc (strlen (str )+ 1 );
24+ strcpy (buffer , str );
2225 free (buffer );
2326}
2427
@@ -27,13 +30,15 @@ void bad1(char *str) {
2730 int len = strlen (str );
2831 // BAD -- Not allocating space for '\0' terminator
2932 char * buffer = malloc (len );
33+ strcpy (buffer , str );
3034 free (buffer );
3135}
3236
3337void good1 (char * str ) {
3438 int len = strlen (str );
3539 // GOOD -- Allocating extra byte for terminator
3640 char * buffer = malloc (len + 1 );
41+ strcpy (buffer , str );
3742 free (buffer );
3843}
3944
@@ -42,25 +47,29 @@ void bad2(char *str) {
4247 int len = strlen (str );
4348 // BAD -- Not allocating space for '\0' terminator
4449 char * buffer = malloc (len );
50+ strcpy (buffer , str );
4551 free (buffer );
4652}
4753
4854void good2 (char * str ) {
4955 int len = strlen (str )+ 1 ;
5056 // GOOD -- Allocating extra byte for terminator
5157 char * buffer = malloc (len );
58+ strcpy (buffer , str );
5259 free (buffer );
5360}
5461
5562void bad3 (char * str ) {
5663 // BAD -- Not allocating space for '\0' terminator [NOT DETECTED]
5764 char * buffer = malloc (strlen (str ) * sizeof (char ));
65+ strcpy (buffer , str );
5866 free (buffer );
5967}
6068
6169void good3 (char * str ) {
6270 // GOOD -- Allocating extra byte for terminator
6371 char * buffer = malloc ((strlen (str ) + 1 ) * sizeof (char ));
72+ strcpy (buffer , str );
6473 free (buffer );
6574}
6675
Original file line number Diff line number Diff line change @@ -10,23 +10,32 @@ typedef unsigned long size_t;
1010void *malloc (size_t size);
1111void free (void *ptr);
1212size_t wcslen (const wchar_t *s);
13+ wchar_t * wcscpy (wchar_t * s1, const wchar_t * s2);
14+
15+
16+
17+
18+
1319
1420// // Test code /////
1521
1622void bad1 (wchar_t *wstr) {
1723 // BAD -- Not allocating space for '\0' terminator
1824 wchar_t *wbuffer = (wchar_t *)malloc (wcslen (wstr));
25+ wcscpy (wbuffer, wstr);
1926 free (wbuffer);
2027}
2128
2229void bad2 (wchar_t *wstr) {
2330 // BAD -- Not allocating space for '\0' terminator [NOT DETECTED]
2431 wchar_t *wbuffer = (wchar_t *)malloc (wcslen (wstr) * sizeof (wchar_t ));
32+ wcscpy (wbuffer, wstr);
2533 free (wbuffer);
2634}
2735
2836void good1 (wchar_t *wstr) {
2937 // GOOD -- Allocating extra character for terminator
3038 wchar_t *wbuffer = (wchar_t *)malloc ((wcslen (wstr) + 1 ) * sizeof (wchar_t ));
39+ wcscpy (wbuffer, wstr);
3140 free (wbuffer);
3241}
You can’t perform that action at this time.
0 commit comments