Skip to content

Commit fc9919a

Browse files
committed
C++: Add a test that exercise the 'last field' check.
1 parent c2db5f4 commit fc9919a

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-843/TypeConfusion.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edges
77
| test.cpp:143:14:143:19 | new | test.cpp:145:28:145:68 | s1_2 | provenance | |
88
| test.cpp:153:9:153:15 | new | test.cpp:159:14:159:33 | a | provenance | |
99
| test.cpp:168:9:168:15 | new | test.cpp:171:14:171:33 | a | provenance | |
10+
| test.cpp:187:15:187:24 | new | test.cpp:189:25:189:45 | u64 | provenance | |
1011
nodes
1112
| test.cpp:27:13:27:18 | new | semmle.label | new |
1213
| test.cpp:28:25:28:55 | p | semmle.label | p |
@@ -24,6 +25,8 @@ nodes
2425
| test.cpp:159:14:159:33 | a | semmle.label | a |
2526
| test.cpp:168:9:168:15 | new | semmle.label | new |
2627
| test.cpp:171:14:171:33 | a | semmle.label | a |
28+
| test.cpp:187:15:187:24 | new | semmle.label | new |
29+
| test.cpp:189:25:189:45 | u64 | semmle.label | u64 |
2730
subpaths
2831
#select
2932
| test.cpp:28:25:28:55 | p | test.cpp:27:13:27:18 | new | test.cpp:28:25:28:55 | p | Conversion from $@ to $@ is invalid. | test.cpp:1:8:1:9 | S1 | S1 | test.cpp:11:8:11:21 | Not_S1_wrapper | Not_S1_wrapper |
@@ -32,3 +35,4 @@ subpaths
3235
| test.cpp:128:24:128:59 | s2 | test.cpp:127:12:127:17 | new | test.cpp:128:24:128:59 | s2 | Conversion from $@ to $@ is invalid. | test.cpp:102:8:102:9 | S2 | S2 | test.cpp:119:8:119:20 | Not_S2_prefix | Not_S2_prefix |
3336
| test.cpp:145:28:145:68 | s1_2 | test.cpp:143:14:143:19 | new | test.cpp:145:28:145:68 | s1_2 | Conversion from $@ to $@ is invalid. | test.cpp:1:8:1:9 | S1 | S1 | test.cpp:131:8:131:23 | HasSomeBitFields | HasSomeBitFields |
3437
| test.cpp:159:14:159:33 | a | test.cpp:153:9:153:15 | new | test.cpp:159:14:159:33 | a | Conversion from $@ to $@ is invalid. | test.cpp:60:8:60:10 | Dog | Dog | test.cpp:55:8:55:10 | Cat | Cat |
38+
| test.cpp:189:25:189:45 | u64 | test.cpp:187:15:187:24 | new | test.cpp:189:25:189:45 | u64 | Conversion from $@ to $@ is invalid. | test.cpp:175:8:175:13 | UInt64 | UInt64 | test.cpp:184:8:184:22 | UInt8_with_more | UInt8_with_more |

cpp/ql/test/query-tests/Security/CWE/CWE-843/test.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,22 @@ void test14(bool b) {
171171
Cat* d = static_cast<Cat*>(a); // BAD [NOT DETECTED]
172172
}
173173
}
174+
175+
struct UInt64 { unsigned long u64; };
176+
struct UInt8 { unsigned char u8; };
177+
178+
void test14() {
179+
void* u64 = new UInt64;
180+
// ...
181+
UInt8* u8 = (UInt8*)u64; // GOOD
182+
}
183+
184+
struct UInt8_with_more { UInt8 u8; void* p; };
185+
186+
void test15() {
187+
void* u64 = new UInt64;
188+
// ...
189+
UInt8_with_more* u8 = (UInt8_with_more*)u64; // BAD
190+
}
191+
192+
// semmle-extractor-options: --gcc -std=c++11

0 commit comments

Comments
 (0)