Skip to content

Commit a9aa671

Browse files
committed
C++: Add test cases for HeuristicAllocationExpr in queries.
1 parent 10ca2da commit a9aa671

File tree

6 files changed

+79
-3
lines changed

6 files changed

+79
-3
lines changed

cpp/ql/test/query-tests/Critical/OverflowCalculated/tests3.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// tests1.cpp
1+
// tests3.cpp
22

33
typedef unsigned int size_t;
44

@@ -66,3 +66,21 @@ void test3c()
6666

6767
delete buffer;
6868
}
69+
70+
// --- custom allocators ---
71+
72+
void *MyMalloc1(size_t size) { return std::malloc(size); }
73+
void *MyMalloc2(size_t size);
74+
75+
void tests4()
76+
{
77+
const char *str4 = "1234";
78+
char *buffer1 = 0;
79+
char *buffer2 = 0;
80+
81+
buffer1 = (char *)MyMalloc1(strlen(str4)); // BAD [NOT DETECTED]
82+
strcpy(buffer1, str4);
83+
84+
buffer2 = (char *)MyMalloc2(strlen(str4)); // BAD [NOT DETECTED]
85+
strcpy(buffer2, str4);
86+
}

cpp/ql/test/query-tests/Critical/SizeCheck/test.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,14 @@ void test_union() {
5858
MyUnion *a = malloc(sizeof(MyUnion)); // GOOD
5959
MyUnion *b = malloc(sizeof(MyStruct)); // BAD (too small)
6060
}
61+
62+
// --- custom allocators ---
63+
64+
void *MyMalloc1(size_t size) { return malloc(size); }
65+
void *MyMalloc2(size_t size);
66+
67+
void customAllocatorTests()
68+
{
69+
float *fptr1 = MyMalloc1(3); // BAD (too small) [NOT DETECTED]
70+
float *fptr2 = MyMalloc2(3); // BAD (too small) [NOT DETECTED]
71+
}

cpp/ql/test/query-tests/Critical/SizeCheck/test2.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,13 @@ void good1(void) {
4343
free(dptr);
4444
}
4545

46-
47-
46+
// --- custom allocators ---
47+
48+
void *MyMalloc1(size_t size) { return malloc(size); }
49+
void *MyMalloc2(size_t size);
50+
51+
void customAllocatorTests()
52+
{
53+
double *dptr1 = MyMalloc1(33); // BAD -- Not a multiple of sizeof(double) [NOT DETECTED]
54+
double *dptr2 = MyMalloc2(33); // BAD -- Not a multiple of sizeof(double) [NOT DETECTED]
55+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,21 @@ void bad4(char *str) {
7272
strcpy(buffer, str);
7373
free(buffer);
7474
}
75+
76+
// --- custom allocators ---
77+
78+
void *MyMalloc1(size_t size) { return malloc(size); }
79+
void *MyMalloc2(size_t size);
80+
81+
void customAllocatorTests(char *str)
82+
{
83+
{
84+
char *buffer1 = (char *)MyMalloc1(strlen(str)); // BAD (no room for `\0` terminator) [NOT DETECTED]
85+
strcpy(buffer1, str);
86+
}
87+
88+
{
89+
char *buffer2 = (char *)MyMalloc2(strlen(str)); // BAD (no room for `\0` terminator) [NOT DETECTED]
90+
strcpy(buffer2, str);
91+
}
92+
}

cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/TaintedAllocationSize/TaintedAllocationSize.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ edges
2525
| test.cpp:259:20:259:25 | call to getenv | test.cpp:263:11:263:29 | ... * ... |
2626
| test.cpp:289:17:289:20 | size [post update] | test.cpp:291:11:291:28 | ... * ... |
2727
| test.cpp:305:18:305:21 | size [post update] | test.cpp:308:10:308:27 | ... * ... |
28+
| test.cpp:348:24:348:27 | size | test.cpp:348:46:348:49 | size |
29+
| test.cpp:353:18:353:23 | call to getenv | test.cpp:355:35:355:38 | size |
30+
| test.cpp:355:35:355:38 | size | test.cpp:348:24:348:27 | size |
2831
nodes
2932
| test.cpp:39:27:39:30 | argv | semmle.label | argv |
3033
| test.cpp:43:38:43:44 | tainted | semmle.label | tainted |
@@ -58,6 +61,10 @@ nodes
5861
| test.cpp:291:11:291:28 | ... * ... | semmle.label | ... * ... |
5962
| test.cpp:305:18:305:21 | size [post update] | semmle.label | size [post update] |
6063
| test.cpp:308:10:308:27 | ... * ... | semmle.label | ... * ... |
64+
| test.cpp:348:24:348:27 | size | semmle.label | size |
65+
| test.cpp:348:46:348:49 | size | semmle.label | size |
66+
| test.cpp:353:18:353:23 | call to getenv | semmle.label | call to getenv |
67+
| test.cpp:355:35:355:38 | size | semmle.label | size |
6168
subpaths
6269
#select
6370
| test.cpp:43:31:43:36 | call to malloc | test.cpp:39:27:39:30 | argv | test.cpp:43:38:43:44 | tainted | This allocation size is derived from $@ and might overflow. | test.cpp:39:27:39:30 | argv | user input (a command-line argument) |
@@ -76,3 +83,4 @@ subpaths
7683
| test.cpp:263:4:263:9 | call to malloc | test.cpp:259:20:259:25 | call to getenv | test.cpp:263:11:263:29 | ... * ... | This allocation size is derived from $@ and might overflow. | test.cpp:259:20:259:25 | call to getenv | user input (an environment variable) |
7784
| test.cpp:291:4:291:9 | call to malloc | test.cpp:251:18:251:23 | call to getenv | test.cpp:291:11:291:28 | ... * ... | This allocation size is derived from $@ and might overflow. | test.cpp:251:18:251:23 | call to getenv | user input (an environment variable) |
7885
| test.cpp:308:3:308:8 | call to malloc | test.cpp:251:18:251:23 | call to getenv | test.cpp:308:10:308:27 | ... * ... | This allocation size is derived from $@ and might overflow. | test.cpp:251:18:251:23 | call to getenv | user input (an environment variable) |
86+
| test.cpp:348:39:348:44 | call to malloc | test.cpp:353:18:353:23 | call to getenv | test.cpp:348:46:348:49 | size | This allocation size is derived from $@ and might overflow. | test.cpp:353:18:353:23 | call to getenv | user input (an environment variable) |

cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/TaintedAllocationSize/test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,16 @@ void equality_barrier() {
342342
int* a = (int*)malloc(size1 * sizeof(int)); // GOOD
343343
}
344344
}
345+
346+
// --- custom allocators ---
347+
348+
void *MyMalloc1(size_t size) { return malloc(size); } // [detected here]
349+
void *MyMalloc2(size_t size);
350+
351+
void customAllocatorTests()
352+
{
353+
int size = atoi(getenv("USER"));
354+
355+
char *chars1 = (char *)MyMalloc1(size); // BAD [detected above]
356+
char *chars2 = (char *)MyMalloc2(size); // BAD [NOT DETECTED]
357+
}

0 commit comments

Comments
 (0)