Skip to content

Commit 5f5a87e

Browse files
committed
C++: Add false positive.
1 parent 66d13dc commit 5f5a87e

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ edges
228228
| test.cpp:732:16:732:26 | ... + ... | test.cpp:732:16:732:26 | ... + ... |
229229
| test.cpp:732:16:732:26 | ... + ... | test.cpp:733:5:733:12 | ... = ... |
230230
| test.cpp:732:16:732:26 | ... + ... | test.cpp:733:5:733:12 | ... = ... |
231+
| test.cpp:754:18:754:31 | new[] | test.cpp:767:16:767:29 | access to array |
232+
| test.cpp:754:18:754:31 | new[] | test.cpp:767:16:767:29 | access to array |
233+
| test.cpp:754:18:754:31 | new[] | test.cpp:772:16:772:29 | access to array |
234+
| test.cpp:754:18:754:31 | new[] | test.cpp:772:16:772:29 | access to array |
235+
| test.cpp:781:14:781:27 | new[] | test.cpp:786:18:786:27 | access to array |
231236
nodes
232237
| test.cpp:4:15:4:20 | call to malloc | semmle.label | call to malloc |
233238
| test.cpp:5:15:5:22 | ... + ... | semmle.label | ... + ... |
@@ -382,6 +387,13 @@ nodes
382387
| test.cpp:732:16:732:26 | ... + ... | semmle.label | ... + ... |
383388
| test.cpp:732:16:732:26 | ... + ... | semmle.label | ... + ... |
384389
| test.cpp:733:5:733:12 | ... = ... | semmle.label | ... = ... |
390+
| test.cpp:754:18:754:31 | new[] | semmle.label | new[] |
391+
| test.cpp:767:16:767:29 | access to array | semmle.label | access to array |
392+
| test.cpp:767:16:767:29 | access to array | semmle.label | access to array |
393+
| test.cpp:772:16:772:29 | access to array | semmle.label | access to array |
394+
| test.cpp:772:16:772:29 | access to array | semmle.label | access to array |
395+
| test.cpp:781:14:781:27 | new[] | semmle.label | new[] |
396+
| test.cpp:786:18:786:27 | access to array | semmle.label | access to array |
385397
subpaths
386398
#select
387399
| test.cpp:6:14:6:15 | * ... | test.cpp:4:15:4:20 | call to malloc | test.cpp:6:14:6:15 | * ... | 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 |
@@ -417,3 +429,8 @@ subpaths
417429
| test.cpp:701:15:701:16 | * ... | test.cpp:695:13:695:26 | new[] | test.cpp:701:15:701:16 | * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:695:13:695:26 | new[] | new[] | test.cpp:696:19:696:22 | size | size |
418430
| test.cpp:706:12:706:13 | * ... | test.cpp:711:13:711:26 | new[] | test.cpp:706:12:706:13 | * ... | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:711:13:711:26 | new[] | new[] | test.cpp:712:19:712:22 | size | size |
419431
| test.cpp:733:5:733:12 | ... = ... | test.cpp:730:12:730:28 | new[] | test.cpp:733:5:733:12 | ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:730:12:730:28 | new[] | new[] | test.cpp:732:21:732:25 | ... + ... | ... + ... |
432+
| test.cpp:767:16:767:29 | access to array | test.cpp:754:18:754:31 | new[] | test.cpp:767:16:767:29 | access to array | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:754:18:754:31 | new[] | new[] | test.cpp:767:22:767:28 | ... + ... | ... + ... |
433+
| test.cpp:767:16:767:29 | access to array | test.cpp:754:18:754:31 | new[] | test.cpp:767:16:767:29 | access to array | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:754:18:754:31 | new[] | new[] | test.cpp:772:22:772:28 | ... + ... | ... + ... |
434+
| test.cpp:772:16:772:29 | access to array | test.cpp:754:18:754:31 | new[] | test.cpp:772:16:772:29 | access to array | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:754:18:754:31 | new[] | new[] | test.cpp:767:22:767:28 | ... + ... | ... + ... |
435+
| test.cpp:772:16:772:29 | access to array | test.cpp:754:18:754:31 | new[] | test.cpp:772:16:772:29 | access to array | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:754:18:754:31 | new[] | new[] | test.cpp:772:22:772:28 | ... + ... | ... + ... |
436+
| test.cpp:786:18:786:27 | access to array | test.cpp:781:14:781:27 | new[] | test.cpp:786:18:786:27 | access to array | This read might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:781:14:781:27 | new[] | new[] | test.cpp:786:20:786:26 | ... + ... | ... + ... |

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,4 +741,50 @@ void test37(unsigned long n)
741741
{
742742
p[n - i] = 0; // GOOD
743743
}
744+
}
745+
746+
unsigned get(char);
747+
void exit(int);
748+
749+
void error(const char * msg) {
750+
exit(1);
751+
}
752+
753+
void test38(unsigned size) {
754+
char * alloc = new char[size];
755+
756+
unsigned pos = 0;
757+
while (pos < size) {
758+
char kind = alloc[pos];
759+
unsigned n = get(alloc[pos]);
760+
if (pos + n >= size) {
761+
error("");
762+
}
763+
switch (kind) {
764+
case '0':
765+
if (n != 1)
766+
error("");
767+
char x = alloc[pos + 1]; // $ alloc=L754 deref=L767 // GOOD [FALSE POSITIVE]
768+
break;
769+
case '1':
770+
if (n != 2)
771+
error("");
772+
char a = alloc[pos + 1]; // $ alloc=L754 deref=L772 // GOOD [FALSE POSITIVE]
773+
char b = alloc[pos + 2];
774+
break;
775+
}
776+
pos += 1 + n;
777+
}
778+
}
779+
780+
void test38_simple(unsigned size, unsigned pos, unsigned numParams) {
781+
char * p = new char[size];
782+
783+
if (pos < size) {
784+
if (pos + numParams < size) {
785+
if (numParams == 1) {
786+
char x = p[pos + 1]; // $ alloc=L781 deref=L786 // GOOD [FALSE POSITIVE]
787+
}
788+
}
789+
}
744790
}

0 commit comments

Comments
 (0)