Skip to content

Commit 5831c44

Browse files
committed
Rust: Add test cases for another situation I came across.
1 parent 7ecba71 commit 5831c44

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

rust/ql/test/query-tests/security/CWE-825/deallocation.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,43 @@ pub fn test_qhelp_tests() {
198198
std::alloc::dealloc(ptr, layout);
199199
}
200200
}
201+
202+
pub fn test_vec_reserve() {
203+
let mut vec1 = Vec::<u16>::new();
204+
vec1.push(100);
205+
let ptr1 = &raw mut vec1[0];
206+
207+
unsafe {
208+
let v1 = *ptr1;
209+
println!(" v1 = {}", v1);
210+
}
211+
212+
vec1.reserve(1000); // $ MISSING: Source=reserve
213+
// (may invalidate the pointer)
214+
215+
unsafe {
216+
let v2 = *ptr1; // $ MISSING: Alert[rust/access-invalid-pointer]=reserve
217+
println!(" v2 = {}", v2); // corrupt in practice
218+
}
219+
220+
// -
221+
222+
let mut vec2 = Vec::<u16>::new();
223+
vec2.push(200);
224+
let ptr2 = &raw mut vec2[0];
225+
226+
unsafe {
227+
let v3 = *ptr2;
228+
println!(" v3 = {}", v3);
229+
}
230+
231+
for _i in 0..1000 {
232+
vec2.push(0); // $ MISSING: Source=push
233+
// (may invalidate the pointer)
234+
}
235+
236+
unsafe {
237+
let v4 = *ptr2; // $ MISSING: Alert[rust/access-invalid-pointer]=push
238+
println!(" v4 = {}", v4); // corrupt in practice
239+
}
240+
}

rust/ql/test/query-tests/security/CWE-825/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ fn main() {
126126
println!("test_qhelp_tests:");
127127
test_qhelp_tests();
128128

129+
println!("test_vec_reserve:");
130+
test_vec_reserve();
131+
129132
// ---
130133

131134
println!("test_local_dangling:");

0 commit comments

Comments
 (0)