Skip to content

Commit 4496fd5

Browse files
authored
Merge pull request github#13271 from jketema/invalid-pointer-deref-fp-2
C++: Add `cpp/invalid-pointer-deref` FP test case
2 parents 92b408f + 9a467f9 commit 4496fd5

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,24 @@ 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: ... = ... |
668+
| test.cpp:347:14:347:27 | new[] | test.cpp:348:15:348:16 | xs |
669+
| test.cpp:348:15:348:16 | xs | test.cpp:350:16:350:19 | ... ++ |
670+
| test.cpp:348:15:348:16 | xs | test.cpp:350:16:350:19 | ... ++ |
671+
| test.cpp:350:16:350:19 | ... ++ | test.cpp:350:15:350:19 | Load: * ... |
672+
| test.cpp:350:16:350:19 | ... ++ | test.cpp:350:16:350:19 | ... ++ |
673+
| test.cpp:350:16:350:19 | ... ++ | test.cpp:350:16:350:19 | ... ++ |
657674
subpaths
658675
#select
659676
| 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 +696,6 @@ subpaths
679696
| 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 |
680697
| 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 |
681698
| 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 | ... + ... | ... + ... |
699+
| 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 |
700+
| 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 |
701+
| test.cpp:350:15:350:19 | Load: * ... | test.cpp:347:14:347:27 | new[] | test.cpp:350:15:350:19 | Load: * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:347:14:347:27 | new[] | new[] | test.cpp:348:20:348:23 | size | size |

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

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,43 @@ 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+
}
344+
}
345+
346+
void test24(unsigned size) {
347+
char *xs = new char[size];
348+
char *end = xs + size;
349+
if (xs < end) {
350+
int val = *xs++; // GOOD [FALSE POSITIVE]
351+
}
324352
}

0 commit comments

Comments
 (0)