Skip to content

Commit 8594c7a

Browse files
committed
Rust: Add test for rust/access-after-lifetime-ended FP involving generic calls.
1 parent e52f819 commit 8594c7a

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

rust/ql/test/query-tests/security/CWE-825/AccessAfterLifetime.expected

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
| lifetime.rs:667:14:667:17 | ref1 | lifetime.rs:655:11:655:25 | &raw const str2 | lifetime.rs:667:14:667:17 | ref1 | Access of a pointer to $@ after its lifetime has ended. | lifetime.rs:651:7:651:10 | str2 | str2 |
2323
| lifetime.rs:789:12:789:13 | p1 | lifetime.rs:781:9:781:19 | &my_local10 | lifetime.rs:789:12:789:13 | p1 | Access of a pointer to $@ after its lifetime has ended. | lifetime.rs:779:6:779:15 | my_local10 | my_local10 |
2424
| lifetime.rs:808:23:808:25 | ptr | lifetime.rs:798:9:798:12 | &val | lifetime.rs:808:23:808:25 | ptr | Access of a pointer to $@ after its lifetime has ended. | lifetime.rs:796:6:796:8 | val | val |
25+
| lifetime.rs:843:12:843:14 | ptr | lifetime.rs:851:12:851:23 | &local_value | lifetime.rs:843:12:843:14 | ptr | Access of a pointer to $@ after its lifetime has ended. | lifetime.rs:850:6:850:16 | local_value | local_value |
2526
| main.rs:64:23:64:24 | p2 | main.rs:44:26:44:28 | &b2 | main.rs:64:23:64:24 | p2 | Access of a pointer to $@ after its lifetime has ended. | main.rs:43:13:43:14 | b2 | b2 |
2627
edges
2728
| deallocation.rs:242:6:242:7 | p1 | deallocation.rs:245:14:245:15 | p1 | provenance | |
@@ -194,6 +195,10 @@ edges
194195
| lifetime.rs:798:9:798:12 | &val | lifetime.rs:798:2:798:12 | return ... | provenance | |
195196
| lifetime.rs:802:6:802:8 | ptr | lifetime.rs:808:23:808:25 | ptr | provenance | |
196197
| lifetime.rs:802:12:802:24 | get_pointer(...) | lifetime.rs:802:6:802:8 | ptr | provenance | |
198+
| lifetime.rs:841:13:841:27 | ...: ... | lifetime.rs:843:12:843:14 | ptr | provenance | |
199+
| lifetime.rs:851:6:851:8 | ptr | lifetime.rs:853:20:853:22 | ptr | provenance | |
200+
| lifetime.rs:851:12:851:23 | &local_value | lifetime.rs:851:6:851:8 | ptr | provenance | |
201+
| lifetime.rs:853:20:853:22 | ptr | lifetime.rs:841:13:841:27 | ...: ... | provenance | |
197202
| main.rs:18:9:18:10 | p1 [&ref] | main.rs:21:19:21:20 | p1 | provenance | |
198203
| main.rs:18:9:18:10 | p1 [&ref] | main.rs:29:19:29:20 | p1 | provenance | |
199204
| main.rs:18:14:18:29 | ...::as_ptr(...) [&ref] | main.rs:18:9:18:10 | p1 [&ref] | provenance | |
@@ -409,6 +414,11 @@ nodes
409414
| lifetime.rs:802:6:802:8 | ptr | semmle.label | ptr |
410415
| lifetime.rs:802:12:802:24 | get_pointer(...) | semmle.label | get_pointer(...) |
411416
| lifetime.rs:808:23:808:25 | ptr | semmle.label | ptr |
417+
| lifetime.rs:841:13:841:27 | ...: ... | semmle.label | ...: ... |
418+
| lifetime.rs:843:12:843:14 | ptr | semmle.label | ptr |
419+
| lifetime.rs:851:6:851:8 | ptr | semmle.label | ptr |
420+
| lifetime.rs:851:12:851:23 | &local_value | semmle.label | &local_value |
421+
| lifetime.rs:853:20:853:22 | ptr | semmle.label | ptr |
412422
| main.rs:18:9:18:10 | p1 [&ref] | semmle.label | p1 [&ref] |
413423
| main.rs:18:14:18:29 | ...::as_ptr(...) [&ref] | semmle.label | ...::as_ptr(...) [&ref] |
414424
| main.rs:18:26:18:28 | &b1 | semmle.label | &b1 |

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,3 +827,33 @@ pub fn test_lifetimes_example_good() {
827827

828828
println!(" val = {dereferenced_ptr}");
829829
}
830+
831+
// --- generic calls ---
832+
833+
trait Processor {
834+
fn process(ptr: *const i64) -> i64;
835+
}
836+
837+
struct MyProcessor {
838+
}
839+
840+
impl Processor for MyProcessor {
841+
fn process(ptr: *const i64) -> i64 {
842+
unsafe {
843+
return *ptr; // $ SPURIOUS: Alert[rust/access-after-lifetime-ended]=local_value
844+
}
845+
}
846+
}
847+
848+
fn generic_caller<T: Processor>() -> i64
849+
{
850+
let local_value: i64 = 10;
851+
let ptr = &local_value as *const i64; // $ Source[rust/access-after-lifetime-ended]=local_value
852+
853+
return T::process(ptr);
854+
}
855+
856+
pub fn test_generic() {
857+
let result = generic_caller::<MyProcessor>();
858+
println!(" result = {result}");
859+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,7 @@ fn main() {
209209

210210
println!("test_lifetimes_example_good:");
211211
test_lifetimes_example_good();
212+
213+
println!("test_generic:");
214+
test_generic();
212215
}

0 commit comments

Comments
 (0)