Skip to content

Commit d6b8f25

Browse files
committed
C++: Add more tests.
1 parent 9745073 commit d6b8f25

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

cpp/ql/test/query-tests/Likely Bugs/Memory Management/UsingExpiredStackAddress/UsingExpiredStackAddress.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ edges
6464
| test.cpp:201:5:201:17 | EnterFunction: maybe_deref_p | test.cpp:201:5:201:17 | VariableAddress: maybe_deref_p |
6565
| test.cpp:210:3:210:9 | Call: call to escape1 | test.cpp:201:5:201:17 | EnterFunction: maybe_deref_p |
6666
| test.cpp:210:3:210:9 | Call: call to escape1 | test.cpp:201:5:201:17 | VariableAddress: maybe_deref_p |
67+
| test.cpp:234:3:234:13 | Store: ... = ... | test.cpp:238:3:238:9 | Call: call to escape2 |
68+
| test.cpp:238:3:238:9 | Call: call to escape2 | test.cpp:239:17:239:17 | Load: p |
69+
| test.cpp:263:3:263:13 | Store: ... = ... | test.cpp:267:3:267:9 | Call: call to escape3 |
70+
| test.cpp:267:3:267:9 | Call: call to escape3 | test.cpp:268:17:268:17 | Load: p |
6771
#select
6872
| test.cpp:15:16:15:16 | Load: p | test.cpp:10:3:10:13 | Store: ... = ... | test.cpp:15:16:15:16 | Load: p | Stack variable $@ escapes $@ and is used after it has expired. | test.cpp:9:7:9:7 | x | x | test.cpp:10:3:10:13 | Store: ... = ... | here |
6973
| test.cpp:24:16:24:16 | Load: p | test.cpp:10:3:10:13 | Store: ... = ... | test.cpp:24:16:24:16 | Load: p | Stack variable $@ escapes $@ and is used after it has expired. | test.cpp:9:7:9:7 | x | x | test.cpp:10:3:10:13 | Store: ... = ... | here |
@@ -90,3 +94,5 @@ edges
9094
| test.cpp:180:14:180:19 | Load: * ... | test.cpp:154:3:154:22 | Store: ... = ... | test.cpp:180:14:180:19 | Load: * ... | Stack variable $@ escapes $@ and is used after it has expired. | test.cpp:133:7:133:8 | b2 | b2 | test.cpp:154:3:154:22 | Store: ... = ... | here |
9195
| test.cpp:181:13:181:20 | Load: access to array | test.cpp:155:3:155:21 | Store: ... = ... | test.cpp:181:13:181:20 | Load: access to array | Stack variable $@ escapes $@ and is used after it has expired. | test.cpp:134:7:134:8 | b3 | b3 | test.cpp:155:3:155:21 | Store: ... = ... | here |
9296
| test.cpp:182:14:182:19 | Load: * ... | test.cpp:156:3:156:25 | Store: ... = ... | test.cpp:182:14:182:19 | Load: * ... | Stack variable $@ escapes $@ and is used after it has expired. | test.cpp:134:7:134:8 | b3 | b3 | test.cpp:156:3:156:25 | Store: ... = ... | here |
97+
| test.cpp:239:17:239:17 | Load: p | test.cpp:234:3:234:13 | Store: ... = ... | test.cpp:239:17:239:17 | Load: p | Stack variable $@ escapes $@ and is used after it has expired. | test.cpp:232:7:232:7 | x | x | test.cpp:234:3:234:13 | Store: ... = ... | here |
98+
| test.cpp:268:17:268:17 | Load: p | test.cpp:263:3:263:13 | Store: ... = ... | test.cpp:268:17:268:17 | Load: p | Stack variable $@ escapes $@ and is used after it has expired. | test.cpp:260:7:260:7 | x | x | test.cpp:263:3:263:13 | Store: ... = ... | here |

cpp/ql/test/query-tests/Likely Bugs/Memory Management/UsingExpiredStackAddress/test.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,61 @@ int maybe_deref_p(bool b) {
209209
int field_indirect_maybe_bad(bool b) {
210210
escape1();
211211
return maybe_deref_p(b);
212+
}
213+
214+
// These next tests cover subsequent stores to the same address in the same basic block.
215+
216+
static struct S100 s102;
217+
218+
void not_escape1() {
219+
int x;
220+
s102.p = &x;
221+
s102.p = nullptr;
222+
}
223+
224+
void calls_not_escape1() {
225+
not_escape1();
226+
int x = *s102.p; // GOOD
227+
}
228+
229+
static struct S100 s103;
230+
231+
void escape2() {
232+
int x;
233+
s103.p = nullptr;
234+
s103.p = &x;
235+
}
236+
237+
void calls_escape2() {
238+
escape2();
239+
int x = *s103.p; // BAD
240+
}
241+
242+
bool unknown();
243+
static struct S100 s104;
244+
245+
void not_escape2() {
246+
int x;
247+
s104.p = &x;
248+
if(unknown()) { }
249+
s104.p = nullptr;
250+
}
251+
252+
void calls_not_escape2() {
253+
not_escape2();
254+
int x = *s104.p; // GOOD
255+
}
256+
257+
static struct S100 s105;
258+
259+
void escape3() {
260+
int x;
261+
s105.p = nullptr;
262+
if(unknown()) { }
263+
s105.p = &x;
264+
}
265+
266+
void calls_escape3() {
267+
escape3();
268+
int x = *s105.p; // BAD
212269
}

0 commit comments

Comments
 (0)