Skip to content

Commit 26ed560

Browse files
committed
C++: Add new test cases.
1 parent 263e51f commit 26ed560

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@
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) |
99
| test.cpp:127:17:127:22 | call to malloc | This allocation size is derived from $@ and might overflow | test.cpp:123:18:123:23 | call to getenv | user input (getenv) |
1010
| test.cpp:127:24:127:41 | ... * ... | This allocation size is derived from $@ and might overflow | test.cpp:123:18:123:23 | call to getenv | user input (getenv) |
11+
| test.cpp:134:3:134:8 | call to malloc | This allocation size is derived from $@ and might overflow | test.cpp:132:19:132:24 | call to getenv | user input (getenv) |
12+
| test.cpp:134:10:134:27 | ... * ... | This allocation size is derived from $@ and might overflow | test.cpp:132:19:132:24 | call to getenv | user input (getenv) |
13+
| test.cpp:142:4:142:9 | call to malloc | This allocation size is derived from $@ and might overflow | test.cpp:138:19:138:24 | call to getenv | user input (getenv) |
14+
| test.cpp:142:11:142:28 | ... * ... | This allocation size is derived from $@ and might overflow | test.cpp:138:19:138:24 | call to getenv | user input (getenv) |
15+
| test.cpp:169:4:169:9 | call to malloc | This allocation size is derived from $@ and might overflow | test.cpp:165:19:165:24 | call to getenv | user input (getenv) |
16+
| test.cpp:169:11:169:28 | ... * ... | This allocation size is derived from $@ and might overflow | test.cpp:165:19:165:24 | call to getenv | user input (getenv) |

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,67 @@ void open_file_bounded () {
126126
int* a = (int*)malloc(bounded_size * sizeof(int)); // GOOD
127127
int* b = (int*)malloc(size * sizeof(int)); // BAD
128128
}
129+
130+
void more_bounded_tests() {
131+
{
132+
int size = atoi(getenv("USER"));
133+
134+
malloc(size * sizeof(int)); // BAD
135+
}
136+
137+
{
138+
int size = atoi(getenv("USER"));
139+
140+
if (size > 0)
141+
{
142+
malloc(size * sizeof(int)); // BAD
143+
}
144+
}
145+
146+
{
147+
int size = atoi(getenv("USER"));
148+
149+
if (size < 100)
150+
{
151+
malloc(size * sizeof(int)); // BAD [NOT DETECTED]
152+
}
153+
}
154+
155+
{
156+
int size = atoi(getenv("USER"));
157+
158+
if ((size > 0) && (size < 100))
159+
{
160+
malloc(size * sizeof(int)); // GOOD
161+
}
162+
}
163+
164+
{
165+
int size = atoi(getenv("USER"));
166+
167+
if ((100 > size) && (0 < size))
168+
{
169+
malloc(size * sizeof(int)); // GOOD [FALSE POSITIVE]
170+
}
171+
}
172+
173+
{
174+
int size = atoi(getenv("USER"));
175+
176+
malloc(size * sizeof(int)); // BAD [NOT DETECTED]
177+
178+
if ((size > 0) && (size < 100))
179+
{
180+
// ...
181+
}
182+
}
183+
184+
{
185+
int size = atoi(getenv("USER"));
186+
187+
if (size > 100)
188+
{
189+
malloc(size * sizeof(int)); // BAD [NOT DETECTED]
190+
}
191+
}
192+
}

0 commit comments

Comments
 (0)