Skip to content

Commit 3fb0241

Browse files
committed
C++: Add a FP caused by bad range analysis for subtraction.
1 parent 44b734e commit 3fb0241

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ 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:739:12:739:21 | new[] | test.cpp:742:5:742:16 | ... = ... |
231232
nodes
232233
| test.cpp:4:15:4:20 | call to malloc | semmle.label | call to malloc |
233234
| test.cpp:5:15:5:22 | ... + ... | semmle.label | ... + ... |
@@ -382,6 +383,8 @@ nodes
382383
| test.cpp:732:16:732:26 | ... + ... | semmle.label | ... + ... |
383384
| test.cpp:732:16:732:26 | ... + ... | semmle.label | ... + ... |
384385
| test.cpp:733:5:733:12 | ... = ... | semmle.label | ... = ... |
386+
| test.cpp:739:12:739:21 | new[] | semmle.label | new[] |
387+
| test.cpp:742:5:742:16 | ... = ... | semmle.label | ... = ... |
385388
subpaths
386389
#select
387390
| 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 +420,4 @@ subpaths
417420
| 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 |
418421
| 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 |
419422
| 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 | ... + ... | ... + ... |
423+
| test.cpp:742:5:742:16 | ... = ... | test.cpp:739:12:739:21 | new[] | test.cpp:742:5:742:16 | ... = ... | This write might be out of bounds, as the pointer might be equal to $@ + $@. | test.cpp:739:12:739:21 | new[] | new[] | test.cpp:742:7:742:11 | ... - ... | ... - ... |

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,3 +733,12 @@ void test36(unsigned size, unsigned n) {
733733
*end = 0; // $ deref=L733 // BAD
734734
}
735735
}
736+
737+
void test37(unsigned long n)
738+
{
739+
int *p = new int[n];
740+
for (unsigned long i = n; i != 0u; i--)
741+
{
742+
p[n - i] = 0; // $ alloc=L739 deref=L742 // GOOD [FALSE POSITIVE]
743+
}
744+
}

cpp/ql/test/library-tests/ir/range-analysis/test.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,25 @@ void gotoLoop(bool b1, bool b2)
9595
}
9696
}
9797
}
98+
99+
void test_sub(int x, int y, int n) {
100+
if(x > 0 && x < 500) {
101+
if(y > 0 && y < 10) {
102+
range(x - y); // $ range="<=InitializeParameter: x-1" range=<=498
103+
}
104+
105+
if(n > 0 && n < 100) {
106+
for (int i = 0; i < n; i++)
107+
{
108+
range(n - i); // $ range=<=99 range="<=InitializeParameter: n | Store: n+0"
109+
range(i - n); // $ range="<=InitializeParameter: n | Store: n-2" range=<=97 range="<=Phi: i-1"
110+
}
111+
112+
for (int i = n; i != 0; i--)
113+
{
114+
range(n - i); // $ SPURIOUS: overflow=+
115+
range(i - n); // $ range="<=Phi: i-1" SPURIOUS: overflow=-
116+
}
117+
}
118+
}
119+
}

0 commit comments

Comments
 (0)