forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#83948 - ABouttefeux:lint-nullprt-deref, r=Ral…
…fJung add lint deref_nullptr detecting when a null ptr is dereferenced fixes rust-lang#83856 changelog: add lint that detect code like ```rust unsafe { &*core::ptr::null::<i32>() }; unsafe { addr_of!(std::ptr::null::<i32>()) }; let x: i32 = unsafe {*core::ptr::null()}; let x: i32 = unsafe {*core::ptr::null_mut()}; unsafe {*(0 as *const i32)}; unsafe {*(core::ptr::null() as *const i32)}; ``` ``` warning: Dereferencing a null pointer causes undefined behavior --> src\main.rs:5:26 | 5 | let x: i32 = unsafe {*core::ptr::null()}; | ^^^^^^^^^^^^^^^^^^ | | | a null pointer is dereferenced | this code causes undefined behavior when executed | = note: `#[warn(deref_nullptr)]` on by default ``` Limitation: It does not detect code like ```rust const ZERO: usize = 0; unsafe {*(ZERO as *const i32)}; ``` or code where `0` is not directly a literal
- Loading branch information
Showing
7 changed files
with
201 additions
and
0 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 |
---|---|---|
|
@@ -3,6 +3,9 @@ | |
|
||
// pretty-expanded FIXME #23616 | ||
|
||
#![allow(deref_nullptr)] | ||
|
||
|
||
use std::env; | ||
|
||
pub 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,38 @@ | ||
// test the deref_nullptr lint | ||
|
||
#![deny(deref_nullptr)] | ||
|
||
use std::ptr; | ||
|
||
struct Struct { | ||
field: u8, | ||
} | ||
|
||
fn f() { | ||
unsafe { | ||
let a = 1; | ||
let ub = *(a as *const i32); | ||
let ub = *(0 as *const i32); | ||
//~^ ERROR dereferencing a null pointer | ||
let ub = *ptr::null::<i32>(); | ||
//~^ ERROR dereferencing a null pointer | ||
let ub = *ptr::null_mut::<i32>(); | ||
//~^ ERROR dereferencing a null pointer | ||
let ub = *(ptr::null::<i16>() as *const i32); | ||
//~^ ERROR dereferencing a null pointer | ||
let ub = *(ptr::null::<i16>() as *mut i32 as *mut usize as *const u8); | ||
//~^ ERROR dereferencing a null pointer | ||
let ub = &*ptr::null::<i32>(); | ||
//~^ ERROR dereferencing a null pointer | ||
let ub = &*ptr::null_mut::<i32>(); | ||
//~^ ERROR dereferencing a null pointer | ||
ptr::addr_of!(*ptr::null::<i32>()); | ||
//~^ ERROR dereferencing a null pointer | ||
ptr::addr_of_mut!(*ptr::null_mut::<i32>()); | ||
//~^ ERROR dereferencing a null pointer | ||
let offset = ptr::addr_of!((*ptr::null::<Struct>()).field); | ||
//~^ ERROR dereferencing a null pointer | ||
} | ||
} | ||
|
||
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,68 @@ | ||
error: dereferencing a null pointer | ||
--> $DIR/lint-deref-nullptr.rs:15:18 | ||
| | ||
LL | let ub = *(0 as *const i32); | ||
| ^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/lint-deref-nullptr.rs:3:9 | ||
| | ||
LL | #![deny(deref_nullptr)] | ||
| ^^^^^^^^^^^^^ | ||
|
||
error: dereferencing a null pointer | ||
--> $DIR/lint-deref-nullptr.rs:17:18 | ||
| | ||
LL | let ub = *ptr::null::<i32>(); | ||
| ^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed | ||
|
||
error: dereferencing a null pointer | ||
--> $DIR/lint-deref-nullptr.rs:19:18 | ||
| | ||
LL | let ub = *ptr::null_mut::<i32>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed | ||
|
||
error: dereferencing a null pointer | ||
--> $DIR/lint-deref-nullptr.rs:21:18 | ||
| | ||
LL | let ub = *(ptr::null::<i16>() as *const i32); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed | ||
|
||
error: dereferencing a null pointer | ||
--> $DIR/lint-deref-nullptr.rs:23:18 | ||
| | ||
LL | let ub = *(ptr::null::<i16>() as *mut i32 as *mut usize as *const u8); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed | ||
|
||
error: dereferencing a null pointer | ||
--> $DIR/lint-deref-nullptr.rs:25:19 | ||
| | ||
LL | let ub = &*ptr::null::<i32>(); | ||
| ^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed | ||
|
||
error: dereferencing a null pointer | ||
--> $DIR/lint-deref-nullptr.rs:27:19 | ||
| | ||
LL | let ub = &*ptr::null_mut::<i32>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed | ||
|
||
error: dereferencing a null pointer | ||
--> $DIR/lint-deref-nullptr.rs:29:23 | ||
| | ||
LL | ptr::addr_of!(*ptr::null::<i32>()); | ||
| ^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed | ||
|
||
error: dereferencing a null pointer | ||
--> $DIR/lint-deref-nullptr.rs:31:27 | ||
| | ||
LL | ptr::addr_of_mut!(*ptr::null_mut::<i32>()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed | ||
|
||
error: dereferencing a null pointer | ||
--> $DIR/lint-deref-nullptr.rs:33:36 | ||
| | ||
LL | let offset = ptr::addr_of!((*ptr::null::<Struct>()).field); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed | ||
|
||
error: aborting due to 10 previous errors | ||
|