-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Rust: Exclude self parameter accesses from rust/access-after-lifetime-ended #21155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
06c34fd
3792adf
bed8106
d88e71a
91ab686
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * Fixed false positives from the `rust/access-after-lifetime-ended` query, involving pointers to fields of `self`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -857,3 +857,50 @@ pub fn test_generic() { | |
| let result = generic_caller::<MyProcessor>(); | ||
| println!(" result = {result}"); | ||
| } | ||
|
|
||
| // --- self --- | ||
|
|
||
| struct MyObjectWithGetters { | ||
| value: i64 | ||
| } | ||
|
|
||
| impl MyObjectWithGetters { | ||
| pub fn new(_value: i64) -> Self { | ||
| Self { | ||
| value: _value | ||
| } | ||
| } | ||
|
|
||
| pub unsafe fn get_ptr1(&self) -> *const i64 { | ||
| &raw const self.value // $ MISSING: Source[rust/access-after-lifetime-ended]=self_value | ||
| // (the returned pointer is valid as long as the containing object is) | ||
| } | ||
|
|
||
| pub unsafe fn get_ptr2(self) -> *const i64 { | ||
| &raw const self.value // $ MISSING: Source[rust/access-after-lifetime-ended]=self_value | ||
| // (the returned pointer is valid as long as the containing object is) | ||
| } | ||
| } | ||
|
|
||
| pub fn test_get_self() { | ||
| let ptr1: *const i64; | ||
| let ptr2: *const i64; | ||
|
|
||
| unsafe { | ||
| let obj = MyObjectWithGetters::new(1111); | ||
| ptr1 = obj.get_ptr1(); | ||
| ptr2 = obj.get_ptr2(); | ||
|
|
||
| let v1 = *ptr1; // GOOD | ||
| let v2 = *ptr2; // GOOD | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one is in fact bad. I tried inserting
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's interesting ... then I think the first step to fixing this PR is going to be correcting and expanding the test cases. |
||
| println!(" v1 = {}", v1); | ||
| println!(" v2 = {}", v2); | ||
| } | ||
|
|
||
| use_the_stack(); | ||
|
|
||
| let v3 = unsafe { *ptr1 }; // $ MISSING: Alert[rust/access-after-lifetime-ended]=self_value | ||
| let v4 = unsafe { *ptr2 }; // $ MISSING: Alert[rust/access-after-lifetime-ended]=self_value | ||
| println!(" v3 = {} (!)", v3); | ||
| println!(" v4 = {} (!)", v4); // corrupt in practice | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -215,4 +215,7 @@ fn main() { | |
|
|
||
| println!("test_generic:"); | ||
| test_generic(); | ||
|
|
||
| println!("test_get_self:"); | ||
| test_get_self(); | ||
| } | ||