Skip to content

Commit 3997b76

Browse files
committed
Auto merge of rust-lang#83948 - ABouttefeux:lint-nullprt-deref, r=RalfJung
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
2 parents c9be90a + 5e3ffa9 commit 3997b76

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

core/src/ptr/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
211211
#[stable(feature = "rust1", since = "1.0.0")]
212212
#[rustc_promotable]
213213
#[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")]
214+
#[rustc_diagnostic_item = "ptr_null"]
214215
pub const fn null<T>() -> *const T {
215216
0 as *const T
216217
}
@@ -229,6 +230,7 @@ pub const fn null<T>() -> *const T {
229230
#[stable(feature = "rust1", since = "1.0.0")]
230231
#[rustc_promotable]
231232
#[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")]
233+
#[rustc_diagnostic_item = "ptr_null_mut"]
232234
pub const fn null_mut<T>() -> *mut T {
233235
0 as *mut T
234236
}

0 commit comments

Comments
 (0)