Skip to content

Commit 10755ec

Browse files
committed
C++: Add testcase with bounded randomness source.
1 parent f97b8ad commit 10755ec

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ edges
3636
| test.cpp:36:13:36:13 | Chi | test.cpp:37:7:37:7 | r |
3737
| test.cpp:36:13:36:13 | Chi | test.cpp:37:7:37:7 | r |
3838
| test.cpp:36:13:36:13 | get_rand3 output argument [array content] | test.cpp:36:13:36:13 | Chi |
39+
| test.cpp:45:11:45:14 | call to rand | test.cpp:46:3:46:3 | r |
40+
| test.cpp:45:11:45:14 | call to rand | test.cpp:46:3:46:3 | r |
41+
| test.cpp:45:11:45:14 | call to rand | test.cpp:46:3:46:3 | r |
42+
| test.cpp:45:11:45:14 | call to rand | test.cpp:46:3:46:3 | r |
43+
| test.cpp:48:24:48:27 | call to rand | test.cpp:49:2:49:11 | unsigned_r |
44+
| test.cpp:48:24:48:27 | call to rand | test.cpp:49:2:49:11 | unsigned_r |
45+
| test.cpp:48:24:48:27 | call to rand | test.cpp:49:2:49:11 | unsigned_r |
46+
| test.cpp:48:24:48:27 | call to rand | test.cpp:49:2:49:11 | unsigned_r |
3947
nodes
4048
| test.c:18:13:18:16 | call to rand | semmle.label | call to rand |
4149
| test.c:18:13:18:16 | call to rand | semmle.label | call to rand |
@@ -87,6 +95,16 @@ nodes
8795
| test.cpp:37:7:37:7 | r | semmle.label | r |
8896
| test.cpp:37:7:37:7 | r | semmle.label | r |
8997
| test.cpp:37:7:37:7 | r | semmle.label | r |
98+
| test.cpp:45:11:45:14 | call to rand | semmle.label | call to rand |
99+
| test.cpp:45:11:45:14 | call to rand | semmle.label | call to rand |
100+
| test.cpp:46:3:46:3 | r | semmle.label | r |
101+
| test.cpp:46:3:46:3 | r | semmle.label | r |
102+
| test.cpp:46:3:46:3 | r | semmle.label | r |
103+
| test.cpp:48:24:48:27 | call to rand | semmle.label | call to rand |
104+
| test.cpp:48:24:48:27 | call to rand | semmle.label | call to rand |
105+
| test.cpp:49:2:49:11 | unsigned_r | semmle.label | unsigned_r |
106+
| test.cpp:49:2:49:11 | unsigned_r | semmle.label | unsigned_r |
107+
| test.cpp:49:2:49:11 | unsigned_r | semmle.label | unsigned_r |
90108
#select
91109
| test.c:21:17:21:17 | r | test.c:18:13:18:16 | call to rand | test.c:21:17:21:17 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:18:13:18:16 | call to rand | Uncontrolled value |
92110
| test.c:35:5:35:5 | r | test.c:34:13:34:18 | call to rand | test.c:35:5:35:5 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.c:34:13:34:18 | call to rand | Uncontrolled value |
@@ -96,3 +114,5 @@ nodes
96114
| 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 |
97115
| 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 |
98116
| 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 |
117+
| test.cpp:46:3:46:3 | r | test.cpp:45:11:45:14 | call to rand | test.cpp:46:3:46:3 | r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.cpp:45:11:45:14 | call to rand | Uncontrolled value |
118+
| test.cpp:49:2:49:11 | unsigned_r | test.cpp:48:24:48:27 | call to rand | test.cpp:49:2:49:11 | unsigned_r | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test.cpp:48:24:48:27 | call to rand | Uncontrolled value |

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,14 @@ void randomTester2()
3737
r = r + 100; // BAD
3838
}
3939
}
40+
41+
int rand(int min, int max);
42+
unsigned rand(int max);
43+
44+
void test_with_bounded_randomness() {
45+
int r = rand(0, 10);
46+
r++; // GOOD [FALSE POSITIVE]
47+
48+
unsigned unsigned_r = rand(10);
49+
unsigned_r++; // GOOD [FALSE POSITIVE]
50+
}

0 commit comments

Comments
 (0)