Skip to content

Commit 51176bd

Browse files
committed
C++: Add Geoffrey's testcases.
1 parent f2cbbab commit 51176bd

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-193/constant-size/ConstantSizeArrayOffByOne.expected

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,102 @@
11
edges
2+
| test.cpp:34:10:34:12 | buf | test.cpp:34:5:34:24 | access to array |
23
| test.cpp:35:10:35:12 | buf | test.cpp:35:5:35:22 | access to array |
34
| test.cpp:36:10:36:12 | buf | test.cpp:36:5:36:24 | access to array |
5+
| test.cpp:39:14:39:16 | buf | test.cpp:39:9:39:19 | access to array |
46
| test.cpp:43:14:43:16 | buf | test.cpp:43:9:43:19 | access to array |
7+
| test.cpp:48:10:48:12 | buf | test.cpp:48:5:48:24 | access to array |
58
| test.cpp:49:10:49:12 | buf | test.cpp:49:5:49:22 | access to array |
69
| test.cpp:50:10:50:12 | buf | test.cpp:50:5:50:24 | access to array |
10+
| test.cpp:53:14:53:16 | buf | test.cpp:53:9:53:19 | access to array |
711
| test.cpp:57:14:57:16 | buf | test.cpp:57:9:57:19 | access to array |
812
| test.cpp:61:14:61:16 | buf | test.cpp:61:9:61:19 | access to array |
13+
| test.cpp:70:33:70:33 | p | test.cpp:71:5:71:17 | access to array |
914
| test.cpp:70:33:70:33 | p | test.cpp:72:5:72:15 | access to array |
15+
| test.cpp:76:26:76:46 | & ... | test.cpp:66:32:66:32 | p |
16+
| test.cpp:76:32:76:34 | buf | test.cpp:76:26:76:46 | & ... |
1017
| test.cpp:77:26:77:44 | & ... | test.cpp:66:32:66:32 | p |
1118
| test.cpp:77:32:77:34 | buf | test.cpp:77:26:77:44 | & ... |
1219
| test.cpp:79:27:79:34 | buf | test.cpp:70:33:70:33 | p |
1320
| test.cpp:79:32:79:34 | buf | test.cpp:79:27:79:34 | buf |
1421
| test.cpp:85:34:85:36 | buf | test.cpp:87:5:87:31 | access to array |
1522
| test.cpp:85:34:85:36 | buf | test.cpp:88:5:88:27 | access to array |
23+
| test.cpp:96:13:96:15 | arr | test.cpp:96:13:96:18 | access to array |
24+
| test.cpp:111:17:111:19 | arr | test.cpp:111:17:111:22 | access to array |
25+
| test.cpp:111:17:111:19 | arr | test.cpp:115:35:115:40 | access to array |
26+
| test.cpp:111:17:111:19 | arr | test.cpp:119:17:119:22 | access to array |
27+
| test.cpp:115:35:115:37 | arr | test.cpp:111:17:111:22 | access to array |
28+
| test.cpp:115:35:115:37 | arr | test.cpp:115:35:115:40 | access to array |
29+
| test.cpp:115:35:115:37 | arr | test.cpp:119:17:119:22 | access to array |
30+
| test.cpp:119:17:119:19 | arr | test.cpp:111:17:111:22 | access to array |
31+
| test.cpp:119:17:119:19 | arr | test.cpp:115:35:115:40 | access to array |
32+
| test.cpp:119:17:119:19 | arr | test.cpp:119:17:119:22 | access to array |
1633
| test.cpp:128:9:128:11 | arr | test.cpp:128:9:128:14 | access to array |
1734
| test.cpp:134:25:134:27 | arr | test.cpp:136:9:136:16 | ... += ... |
1835
| test.cpp:136:9:136:16 | ... += ... | test.cpp:138:13:138:15 | arr |
1936
| test.cpp:143:18:143:21 | asdf | test.cpp:134:25:134:27 | arr |
2037
| test.cpp:143:18:143:21 | asdf | test.cpp:143:18:143:21 | asdf |
38+
| test.cpp:148:23:148:28 | buffer | test.cpp:150:5:150:11 | access to array |
39+
| test.cpp:148:23:148:28 | buffer | test.cpp:151:5:151:11 | access to array |
40+
| test.cpp:159:25:159:29 | array | test.cpp:161:5:161:10 | access to array |
41+
| test.cpp:159:25:159:29 | array | test.cpp:162:5:162:10 | access to array |
2142
nodes
43+
| test.cpp:34:5:34:24 | access to array | semmle.label | access to array |
44+
| test.cpp:34:10:34:12 | buf | semmle.label | buf |
2245
| test.cpp:35:5:35:22 | access to array | semmle.label | access to array |
2346
| test.cpp:35:10:35:12 | buf | semmle.label | buf |
2447
| test.cpp:36:5:36:24 | access to array | semmle.label | access to array |
2548
| test.cpp:36:10:36:12 | buf | semmle.label | buf |
49+
| test.cpp:39:9:39:19 | access to array | semmle.label | access to array |
50+
| test.cpp:39:14:39:16 | buf | semmle.label | buf |
2651
| test.cpp:43:9:43:19 | access to array | semmle.label | access to array |
2752
| test.cpp:43:14:43:16 | buf | semmle.label | buf |
53+
| test.cpp:48:5:48:24 | access to array | semmle.label | access to array |
54+
| test.cpp:48:10:48:12 | buf | semmle.label | buf |
2855
| test.cpp:49:5:49:22 | access to array | semmle.label | access to array |
2956
| test.cpp:49:10:49:12 | buf | semmle.label | buf |
3057
| test.cpp:50:5:50:24 | access to array | semmle.label | access to array |
3158
| test.cpp:50:10:50:12 | buf | semmle.label | buf |
59+
| test.cpp:53:9:53:19 | access to array | semmle.label | access to array |
60+
| test.cpp:53:14:53:16 | buf | semmle.label | buf |
3261
| test.cpp:57:9:57:19 | access to array | semmle.label | access to array |
3362
| test.cpp:57:14:57:16 | buf | semmle.label | buf |
3463
| test.cpp:61:9:61:19 | access to array | semmle.label | access to array |
3564
| test.cpp:61:14:61:16 | buf | semmle.label | buf |
3665
| test.cpp:66:32:66:32 | p | semmle.label | p |
66+
| test.cpp:66:32:66:32 | p | semmle.label | p |
3767
| test.cpp:70:33:70:33 | p | semmle.label | p |
68+
| test.cpp:71:5:71:17 | access to array | semmle.label | access to array |
3869
| test.cpp:72:5:72:15 | access to array | semmle.label | access to array |
70+
| test.cpp:76:26:76:46 | & ... | semmle.label | & ... |
71+
| test.cpp:76:32:76:34 | buf | semmle.label | buf |
3972
| test.cpp:77:26:77:44 | & ... | semmle.label | & ... |
4073
| test.cpp:77:32:77:34 | buf | semmle.label | buf |
4174
| test.cpp:79:27:79:34 | buf | semmle.label | buf |
4275
| test.cpp:79:32:79:34 | buf | semmle.label | buf |
4376
| test.cpp:85:34:85:36 | buf | semmle.label | buf |
4477
| test.cpp:87:5:87:31 | access to array | semmle.label | access to array |
4578
| test.cpp:88:5:88:27 | access to array | semmle.label | access to array |
79+
| test.cpp:96:13:96:15 | arr | semmle.label | arr |
80+
| test.cpp:96:13:96:18 | access to array | semmle.label | access to array |
81+
| test.cpp:111:17:111:19 | arr | semmle.label | arr |
82+
| test.cpp:111:17:111:22 | access to array | semmle.label | access to array |
83+
| test.cpp:115:35:115:37 | arr | semmle.label | arr |
84+
| test.cpp:115:35:115:40 | access to array | semmle.label | access to array |
85+
| test.cpp:119:17:119:19 | arr | semmle.label | arr |
86+
| test.cpp:119:17:119:22 | access to array | semmle.label | access to array |
4687
| test.cpp:128:9:128:11 | arr | semmle.label | arr |
4788
| test.cpp:128:9:128:14 | access to array | semmle.label | access to array |
4889
| test.cpp:134:25:134:27 | arr | semmle.label | arr |
4990
| test.cpp:136:9:136:16 | ... += ... | semmle.label | ... += ... |
5091
| test.cpp:138:13:138:15 | arr | semmle.label | arr |
5192
| test.cpp:143:18:143:21 | asdf | semmle.label | asdf |
5293
| test.cpp:143:18:143:21 | asdf | semmle.label | asdf |
94+
| test.cpp:148:23:148:28 | buffer | semmle.label | buffer |
95+
| test.cpp:150:5:150:11 | access to array | semmle.label | access to array |
96+
| test.cpp:151:5:151:11 | access to array | semmle.label | access to array |
97+
| test.cpp:159:25:159:29 | array | semmle.label | array |
98+
| test.cpp:161:5:161:10 | access to array | semmle.label | access to array |
99+
| test.cpp:162:5:162:10 | access to array | semmle.label | access to array |
53100
subpaths
54101
#select
55102
| test.cpp:35:5:35:22 | PointerAdd: access to array | test.cpp:35:10:35:12 | buf | test.cpp:35:5:35:22 | access to array | This pointer arithmetic may have an off-by-1 error allowing it to overrun $@ at this $@. | test.cpp:15:9:15:11 | buf | buf | test.cpp:35:5:35:26 | Store: ... = ... | write |
@@ -64,3 +111,5 @@ subpaths
64111
| test.cpp:88:5:88:27 | PointerAdd: access to array | test.cpp:85:34:85:36 | buf | test.cpp:88:5:88:27 | access to array | This pointer arithmetic may have an off-by-1 error allowing it to overrun $@ at this $@. | test.cpp:15:9:15:11 | buf | buf | test.cpp:88:5:88:31 | Store: ... = ... | write |
65112
| test.cpp:128:9:128:14 | PointerAdd: access to array | test.cpp:128:9:128:11 | arr | test.cpp:128:9:128:14 | access to array | This pointer arithmetic may have an off-by-1 error allowing it to overrun $@ at this $@. | test.cpp:125:11:125:13 | arr | arr | test.cpp:128:9:128:18 | Store: ... = ... | write |
66113
| test.cpp:136:9:136:16 | PointerAdd: ... += ... | test.cpp:143:18:143:21 | asdf | test.cpp:138:13:138:15 | arr | This pointer arithmetic may have an off-by-2 error allowing it to overrun $@ at this $@. | test.cpp:142:10:142:13 | asdf | asdf | test.cpp:138:12:138:15 | Load: * ... | read |
114+
| test.cpp:151:5:151:11 | PointerAdd: access to array | test.cpp:148:23:148:28 | buffer | test.cpp:151:5:151:11 | access to array | This pointer arithmetic may have an off-by-1 error allowing it to overrun $@ at this $@. | test.cpp:147:19:147:24 | buffer | buffer | test.cpp:151:5:151:15 | Store: ... = ... | write |
115+
| test.cpp:162:5:162:10 | PointerAdd: access to array | test.cpp:159:25:159:29 | array | test.cpp:162:5:162:10 | access to array | This pointer arithmetic may have an off-by-1 error allowing it to overrun $@ at this $@. | test.cpp:158:10:158:14 | array | array | test.cpp:162:5:162:19 | Store: ... = ... | write |

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-193/constant-size/test.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,30 @@ void testStrncmp1() {
142142
char asdf[5];
143143
testStrncmp2(asdf);
144144
}
145+
146+
void pointer_size_larger_than_array_element_size() {
147+
unsigned char buffer[100]; // getByteSize() = 100
148+
int *ptr = (int *)buffer; // pai.getElementSize() will be sizeof(int) = 4 -> size = 25
149+
150+
ptr[24] = 0; // GOOD: writes bytes 96, 97, 98, 99
151+
ptr[25] = 0; // BAD: writes bytes 100, 101, 102, 103
152+
}
153+
154+
struct vec2 { int x, y; };
155+
struct vec3 { int x, y, z; };
156+
157+
void pointer_size_smaller_than_array_element_size_but_does_not_divide_it() {
158+
vec3 array[3]; // getByteSize() = 9 * sizeof(int)
159+
vec2 *ptr = (vec2 *)array; // pai.getElementSize() will be 2 * sizeof(int) -> size = 4
160+
161+
ptr[3] = vec2{}; // GOOD: writes ints 6, 7
162+
ptr[4] = vec2{}; // BAD: writes ints 8, 9
163+
}
164+
165+
void pointer_size_larger_than_array_element_size_and_does_not_divide_it() {
166+
vec2 array[2]; // getByteSize() = 4 * sizeof(int) = 4 * 4 = 16
167+
vec3 *ptr = (vec3 *)array; // pai.getElementSize() will be 3 * sizeof(int) -> size = 1
168+
169+
ptr[0] = vec3{}; // GOOD: writes ints 0, 1, 2
170+
ptr[1] = vec3{}; // BAD: writes ints 3, 4, 5 [NOT DETECTED]
171+
}

0 commit comments

Comments
 (0)