Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c9fe91

Browse files
committed
CPP: Add proof of zero-termination to tests.
1 parent 77c869f commit 3c9fe91

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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. |

cpp/ql/test/query-tests/Security/CWE/CWE-131/semmle/NoSpaceForZeroTerminator/test.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@
77
typedef unsigned long size_t;
88
void *malloc(size_t size);
99
void free(void *ptr);
10+
char *strcpy(char *s1, const char *s2);
1011

1112
//// Test code /////
1213

1314
void 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

1921
void 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

3337
void 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

4854
void 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

5562
void 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

6169
void 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

cpp/ql/test/query-tests/Security/CWE/CWE-131/semmle/NoSpaceForZeroTerminator/test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,32 @@ typedef unsigned long size_t;
1010
void *malloc(size_t size);
1111
void free(void *ptr);
1212
size_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

1622
void 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

2229
void 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

2836
void 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
}

0 commit comments

Comments
 (0)