Skip to content

Commit af378df

Browse files
committed
C++: Add cpp/invalid-pointer-deref FP test case
1 parent eea972b commit af378df

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-193/pointer-deref/InvalidPointerDeref.expected

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,18 @@ edges
653653
| test.cpp:304:15:304:26 | new[] | test.cpp:308:5:308:6 | xs |
654654
| test.cpp:308:5:308:6 | xs | test.cpp:308:5:308:11 | access to array |
655655
| test.cpp:308:5:308:11 | access to array | test.cpp:308:5:308:29 | Store: ... = ... |
656-
| test.cpp:313:16:313:29 | new[] | test.cpp:314:17:314:18 | xs |
656+
| test.cpp:313:14:313:27 | new[] | test.cpp:314:15:314:16 | xs |
657+
| test.cpp:325:14:325:27 | new[] | test.cpp:326:15:326:16 | xs |
658+
| test.cpp:326:15:326:16 | xs | test.cpp:326:15:326:23 | ... + ... |
659+
| test.cpp:326:15:326:16 | xs | test.cpp:326:15:326:23 | ... + ... |
660+
| test.cpp:326:15:326:16 | xs | test.cpp:338:8:338:15 | * ... |
661+
| test.cpp:326:15:326:16 | xs | test.cpp:341:8:341:17 | * ... |
662+
| test.cpp:326:15:326:23 | ... + ... | test.cpp:342:8:342:17 | * ... |
663+
| test.cpp:326:15:326:23 | ... + ... | test.cpp:342:8:342:17 | * ... |
664+
| test.cpp:338:8:338:15 | * ... | test.cpp:342:8:342:17 | * ... |
665+
| test.cpp:341:8:341:17 | * ... | test.cpp:342:8:342:17 | * ... |
666+
| test.cpp:342:8:342:17 | * ... | test.cpp:333:5:333:21 | Store: ... = ... |
667+
| test.cpp:342:8:342:17 | * ... | test.cpp:341:5:341:21 | Store: ... = ... |
657668
subpaths
658669
#select
659670
| test.cpp:6:14:6:15 | Load: * ... | test.cpp:4:15:4:20 | call to malloc | test.cpp:6:14:6:15 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:4:15:4:20 | call to malloc | call to malloc | test.cpp:5:19:5:22 | size | size |
@@ -679,3 +690,5 @@ subpaths
679690
| test.cpp:264:13:264:14 | Load: * ... | test.cpp:260:13:260:24 | new[] | test.cpp:264:13:264:14 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:260:13:260:24 | new[] | new[] | test.cpp:261:19:261:21 | len | len |
680691
| test.cpp:274:5:274:10 | Store: ... = ... | test.cpp:270:13:270:24 | new[] | test.cpp:274:5:274:10 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:270:13:270:24 | new[] | new[] | test.cpp:271:19:271:21 | len | len |
681692
| test.cpp:308:5:308:29 | Store: ... = ... | test.cpp:304:15:304:26 | new[] | test.cpp:308:5:308:29 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:304:15:304:26 | new[] | new[] | test.cpp:308:8:308:10 | ... + ... | ... + ... |
693+
| test.cpp:333:5:333:21 | Store: ... = ... | test.cpp:325:14:325:27 | new[] | test.cpp:333:5:333:21 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:325:14:325:27 | new[] | new[] | test.cpp:326:20:326:23 | size | size |
694+
| test.cpp:341:5:341:21 | Store: ... = ... | test.cpp:325:14:325:27 | new[] | test.cpp:341:5:341:21 | Store: ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:325:14:325:27 | new[] | new[] | test.cpp:326:20:326:23 | size | size |

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-193/pointer-deref/test.cpp

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,35 @@ void test21() {
310310
}
311311

312312
void test22(unsigned size, int val) {
313-
char *xs = new char[size];
314-
char *end = xs + size; // GOOD
315-
char **current = &end;
316-
do
317-
{
318-
if( *current - xs < 1 ) // GOOD
319-
return;
320-
*--(*current) = 0; // GOOD
321-
val >>= 8;
322-
}
323-
while( val > 0 );
313+
char *xs = new char[size];
314+
char *end = xs + size; // GOOD
315+
char **current = &end;
316+
do {
317+
if (*current - xs < 1) // GOOD
318+
return;
319+
*--(*current) = 0; // GOOD
320+
val >>= 8;
321+
} while (val > 0);
322+
}
323+
324+
void test23(unsigned size, int val) {
325+
char *xs = new char[size];
326+
char *end = xs + size;
327+
char **current = &end;
328+
329+
if (val < 1) {
330+
if(*current - xs < 1)
331+
return;
332+
333+
*--(*current) = 0; // GOOD [FALSE POSITIVE]
334+
return;
335+
}
336+
337+
if (val < 2) {
338+
if(*current - xs < 2)
339+
return;
340+
341+
*--(*current) = 0; // GOOD [FALSE POSITIVE]
342+
*--(*current) = 0; // GOOD
343+
}
324344
}

0 commit comments

Comments
 (0)