Skip to content

add lint deref_nullptr detecting when a null ptr is dereferenced #83948

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

Merged
merged 7 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/test/ui/lint/lint-deref-nullptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,37 @@

#![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 = *core::ptr::null::<i32>();
let ub = *ptr::null::<i32>();
//~^ ERROR dereferencing a null pointer
let ub = *ptr::null_mut::<i32>();
//~^ ERROR dereferencing a null pointer
let ub = *core::ptr::null_mut::<i32>();
let ub = *(ptr::null::<i16>() as *const i32);
//~^ ERROR dereferencing a null pointer
let ub = *(core::ptr::null::<i16>() as *const i32);
let ub = *(ptr::null::<i16>() as *mut i32 as *mut usize as *const u8);
//~^ ERROR dereferencing a null pointer
let ub = *(core::ptr::null::<i16>() as *mut i32 as *mut usize as *const u8);
let ub = &*ptr::null::<i32>();
//~^ ERROR dereferencing a null pointer
let ub = &*core::ptr::null::<i32>();
ptr::addr_of!(*ptr::null::<i32>());
//~^ ERROR dereferencing a null pointer
core::ptr::addr_of!(*core::ptr::null::<i32>());
ptr::addr_of_mut!(*ptr::null_mut::<i32>());
//~^ ERROR dereferencing a null pointer
std::ptr::addr_of_mut!(*core::ptr::null_mut::<i32>());
let ub = *ptr::null::<i32>();
//~^ ERROR dereferencing a null pointer
let ub = *std::ptr::null::<i32>();
let ub = *ptr::null_mut::<i32>();
//~^ ERROR dereferencing a null pointer
let ub = *std::ptr::null_mut::<i32>();
let offset = ptr::addr_of!((*ptr::null::<Struct>()).field);
//~^ ERROR dereferencing a null pointer
}
}
Expand Down
64 changes: 35 additions & 29 deletions src/test/ui/lint/lint-deref-nullptr.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: dereferencing a null pointer
--> $DIR/lint-deref-nullptr.rs:9:18
--> $DIR/lint-deref-nullptr.rs:15:18
|
LL | let ub = *(0 as *const i32);
| ^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
Expand All @@ -11,58 +11,64 @@ LL | #![deny(deref_nullptr)]
| ^^^^^^^^^^^^^

error: dereferencing a null pointer
--> $DIR/lint-deref-nullptr.rs:11:18
--> $DIR/lint-deref-nullptr.rs:17:18
|
LL | let ub = *core::ptr::null::<i32>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
LL | let ub = *ptr::null::<i32>();
| ^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed

error: dereferencing a null pointer
--> $DIR/lint-deref-nullptr.rs:13:18
--> $DIR/lint-deref-nullptr.rs:19:18
|
LL | let ub = *core::ptr::null_mut::<i32>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
LL | let ub = *ptr::null_mut::<i32>();
| ^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed

error: dereferencing a null pointer
--> $DIR/lint-deref-nullptr.rs:15:18
--> $DIR/lint-deref-nullptr.rs:21:18
|
LL | let ub = *(core::ptr::null::<i16>() as *const i32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
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:17:18
--> $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 = *(core::ptr::null::<i16>() as *mut i32 as *mut usize as *const u8);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
LL | let ub = &*ptr::null::<i32>();
| ^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed

error: dereferencing a null pointer
--> $DIR/lint-deref-nullptr.rs:19:19
--> $DIR/lint-deref-nullptr.rs:27:23
|
LL | let ub = &*core::ptr::null::<i32>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
LL | ptr::addr_of!(*ptr::null::<i32>());
| ^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed

error: dereferencing a null pointer
--> $DIR/lint-deref-nullptr.rs:21:29
--> $DIR/lint-deref-nullptr.rs:29:27
|
LL | core::ptr::addr_of!(*core::ptr::null::<i32>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
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:23:32
--> $DIR/lint-deref-nullptr.rs:31:18
|
LL | std::ptr::addr_of_mut!(*core::ptr::null_mut::<i32>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
LL | let ub = *ptr::null::<i32>();
| ^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed

error: dereferencing a null pointer
--> $DIR/lint-deref-nullptr.rs:25:18
--> $DIR/lint-deref-nullptr.rs:33:18
|
LL | let ub = *std::ptr::null::<i32>();
| ^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
LL | let ub = *ptr::null_mut::<i32>();
| ^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed

error: dereferencing a null pointer
--> $DIR/lint-deref-nullptr.rs:27:18
--> $DIR/lint-deref-nullptr.rs:35:36
|
LL | let ub = *std::ptr::null_mut::<i32>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
LL | let offset = ptr::addr_of!((*ptr::null::<Struct>()).field);
| ^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed

error: aborting due to 10 previous errors
error: aborting due to 11 previous errors