Skip to content

Commit a6f1f8d

Browse files
committed
C++: Add testcases demonstrating FPs from real code.
1 parent 6a11aa7 commit a6f1f8d

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/uncontrolled/ArithmeticUncontrolled.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ edges
77
| test.c:81:14:81:17 | call to rand | test.c:83:9:83:9 | r |
88
| test.c:81:23:81:26 | call to rand | test.c:83:9:83:9 | r |
99
| test.c:99:14:99:19 | call to rand | test.c:100:5:100:5 | r |
10+
| test.c:115:12:115:15 | call to rand | test.c:116:3:116:4 | r1 |
11+
| test.c:118:13:118:16 | call to rand | test.c:119:3:119:4 | r2 |
1012
| test.cpp:8:9:8:12 | Store | test.cpp:24:11:24:18 | call to get_rand |
1113
| test.cpp:8:9:8:12 | call to rand | test.cpp:8:9:8:12 | Store |
1214
| test.cpp:13:2:13:15 | Chi [[]] | test.cpp:30:13:30:14 | get_rand2 output argument [[]] |
@@ -33,6 +35,10 @@ nodes
3335
| test.c:83:9:83:9 | r | semmle.label | r |
3436
| test.c:99:14:99:19 | call to rand | semmle.label | call to rand |
3537
| test.c:100:5:100:5 | r | semmle.label | r |
38+
| test.c:115:12:115:15 | call to rand | semmle.label | call to rand |
39+
| test.c:116:3:116:4 | r1 | semmle.label | r1 |
40+
| test.c:118:13:118:16 | call to rand | semmle.label | call to rand |
41+
| test.c:119:3:119:4 | r2 | semmle.label | r2 |
3642
| test.cpp:8:9:8:12 | Store | semmle.label | Store |
3743
| test.cpp:8:9:8:12 | call to rand | semmle.label | call to rand |
3844
| test.cpp:13:2:13:15 | Chi [[]] | semmle.label | Chi [[]] |
@@ -56,6 +62,8 @@ nodes
5662
| test.c:83:9:83:9 | r | test.c:81:14:81:17 | call to rand | test.c:83:9:83:9 | r | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.c:81:14:81:17 | call to rand | Uncontrolled value |
5763
| test.c:83:9:83:9 | r | test.c:81:23:81:26 | call to rand | test.c:83:9:83:9 | r | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.c:81:23:81:26 | call to rand | Uncontrolled value |
5864
| test.c:100:5:100:5 | r | test.c:99:14:99:19 | call to rand | test.c:100:5:100:5 | r | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test.c:99:14:99:19 | call to rand | Uncontrolled value |
65+
| test.c:116:3:116:4 | r1 | test.c:115:12:115:15 | call to rand | test.c:116:3:116:4 | r1 | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:115:12:115:15 | call to rand | Uncontrolled value |
66+
| test.c:119:3:119:4 | r2 | test.c:118:13:118:16 | call to rand | test.c:119:3:119:4 | r2 | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:118:13:118:16 | call to rand | Uncontrolled value |
5967
| test.cpp:25:7:25:7 | r | test.cpp:8:9:8:12 | call to rand | test.cpp:25:7:25:7 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.cpp:8:9:8:12 | call to rand | Uncontrolled value |
6068
| test.cpp:31:7:31:7 | r | test.cpp:13:10:13:13 | call to rand | test.cpp:31:7:31:7 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.cpp:13:10:13:13 | call to rand | Uncontrolled value |
6169
| test.cpp:37:7:37:7 | r | test.cpp:18:9:18:12 | call to rand | test.cpp:37:7:37:7 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.cpp:18:9:18:12 | call to rand | Uncontrolled value |

cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/uncontrolled/test.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,13 @@ void randomTester() {
109109

110110
void add_100(int r) {
111111
r += 100; // GOOD
112-
}
112+
}
113+
114+
void randomTester2(int bound, int min, int max) {
115+
int r1 = rand() % bound;
116+
r1 += 100; // GOOD [FALSE POSITIVE] (`bound` may possibly be MAX_INT in which case this could
117+
// still overflow, but it's most likely fine)
118+
119+
int r2 = (rand() % (max - min + 1)) + min;
120+
r2 += 100; // GOOD [FALSE POSITIVE] (This is a common way to clamp the random value between [min, max])
121+
}

0 commit comments

Comments
 (0)