Skip to content

Commit 2938ad5

Browse files
committed
C++: Add testcase demonstrating the fix from a8c57ec.
1 parent 43bbd4f commit 2938ad5

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-416/semmle/tests/UseAfterFree.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
| test.cpp:170:6:170:9 | data | Memory pointed to by 'data' may have been previously freed $@ | test.cpp:165:2:165:5 | call to free | here |
88
| test.cpp:193:6:193:9 | data | Memory pointed to by 'data' may have been previously freed $@ | test.cpp:191:3:191:6 | call to free | here |
99
| test.cpp:201:6:201:6 | x | Memory pointed to by 'x' may have been previously freed $@ | test.cpp:200:2:200:9 | delete | here |
10+
| test.cpp:242:14:242:17 | data | Memory pointed to by 'data' may have been previously freed $@ | test.cpp:243:11:243:14 | call to free | here |

cpp/ql/test/query-tests/Security/CWE/CWE-416/semmle/tests/test.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,21 @@ void test17(int n, bool b) {
234234
use(data); // GOOD
235235
}
236236
}
237+
238+
void test18(int* array) {
239+
char* data = (char*)malloc(10 * sizeof(char));
240+
for (int i = 0; i < 4; ++i) {
241+
int b = array[i];
242+
if(b) use(data); // BAD
243+
if(!b) free(data);
244+
}
245+
}
246+
247+
void test19(int* array) {
248+
char* data = (char*)malloc(10 * sizeof(char));
249+
int b = array[0];
250+
for (int i = 0; i < 4; ++i) {
251+
if(b) use(data); // GOOD
252+
if(!b) free(data);
253+
}
254+
}

0 commit comments

Comments
 (0)