-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new lint that warns for pointers to stack memory
This adds a new lint with level `Warn` to check for code like: ```rust fn foo() -> *const i32 { let x = 42; &x } ``` and produce a warning like: ```text error: returning a pointer to stack memory associated with a local variable --> <source>:12:5 | LL| &x | ^^ ```
- Loading branch information
Showing
6 changed files
with
198 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// test the deref_nullptr lint | ||
|
||
#![deny(return_local_variable_ptr)] | ||
|
||
fn foo() -> *const u32 { | ||
let empty = 42u32; | ||
return &empty as *const _; | ||
} | ||
|
||
fn bar() -> *const u32 { | ||
let empty = 42u32; | ||
&empty as *const _ | ||
} | ||
|
||
fn baz() -> *const u32 { | ||
let empty = 42u32; | ||
return ∅ | ||
} | ||
|
||
fn faa() -> *const u32 { | ||
let empty = 42u32; | ||
&empty | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
error: returning a pointer to stack memory associated with a local variable | ||
--> $DIR/lint-return-local-variable-ptr.rs:7:12 | ||
| | ||
LL | return &empty as *const _; | ||
| ^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/lint-return-local-variable-ptr.rs:3:9 | ||
| | ||
LL | #![deny(return_local_variable_ptr)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: returning a pointer to stack memory associated with a local variable | ||
--> $DIR/lint-return-local-variable-ptr.rs:12:5 | ||
| | ||
LL | &empty as *const _ | ||
| ^^^^^^^^^^^^^^^^^^ | ||
|
||
error: returning a pointer to stack memory associated with a local variable | ||
--> $DIR/lint-return-local-variable-ptr.rs:17:12 | ||
| | ||
LL | return ∅ | ||
| ^^^^^^ | ||
|
||
error: returning a pointer to stack memory associated with a local variable | ||
--> $DIR/lint-return-local-variable-ptr.rs:22:5 | ||
| | ||
LL | &empty | ||
| ^^^^^^ | ||
|
||
error: aborting due to 4 previous errors | ||
|