Skip to content

Commit d9d6960

Browse files
author
Robert Marsh
authored
Merge pull request #2985 from MathiasVP/bounded-uncontrolled-alloc-size
C++: Add testcase for cpp/uncontrolled-allocation-size
2 parents 80ec5c1 + 3973a50 commit d9d6960

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
| test.cpp:49:17:49:30 | new[] | This allocation size is derived from $@ and might overflow | test.cpp:39:21:39:24 | argv | user input (argv) |
77
| test.cpp:52:21:52:27 | call to realloc | This allocation size is derived from $@ and might overflow | test.cpp:39:21:39:24 | argv | user input (argv) |
88
| test.cpp:52:35:52:60 | ... * ... | This allocation size is derived from $@ and might overflow | test.cpp:39:21:39:24 | argv | user input (argv) |
9+
| test.cpp:127:17:127:22 | call to malloc | This allocation size is derived from $@ and might overflow | test.cpp:123:25:123:30 | call to getenv | user input (getenv) |

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,24 @@ void processFile()
105105
fclose(f);
106106
}
107107
}
108+
109+
char *getenv(const char *name);
110+
111+
#define MAX_SIZE 500
112+
113+
int bounded(int x, int limit) {
114+
int result = x;
115+
if (x <= 0)
116+
result = 1;
117+
else if (x > limit)
118+
result = limit;
119+
return result;
120+
}
121+
122+
void open_file_bounded () {
123+
int size = size = atoi(getenv("USER"));
124+
int bounded_size = bounded(size, MAX_SIZE);
125+
126+
int* a = (int*)malloc(bounded_size); // GOOD
127+
int* b = (int*)malloc(size); // BAD
128+
}

0 commit comments

Comments
 (0)