1
Fork 0

changes based on review

This commit is contained in:
Aliénore Bouttefeux 2021-04-09 16:13:04 +02:00
parent 0531ed0b62
commit 79666c8857
3 changed files with 34 additions and 66 deletions

View file

@ -2972,10 +2972,10 @@ declare_lint! {
/// ///
/// ```rust,no_run /// ```rust,no_run
/// # #![allow(unused)] /// # #![allow(unused)]
/// use std::ptr;
/// unsafe { /// unsafe {
/// let x = &*core::ptr::null::<i32>(); /// let x = &*ptr::null::<i32>();
/// let x = core::ptr::addr_of!(*std::ptr::null::<i32>()); /// let x = ptr::addr_of!(*ptr::null::<i32>());
/// let x = *core::ptr::null::<i32>();
/// let x = *(0 as *const i32); /// let x = *(0 as *const i32);
/// } /// }
/// ``` /// ```
@ -3036,9 +3036,7 @@ impl<'tcx> LateLintPass<'tcx> for DerefNullPtr {
if let rustc_hir::UnOp::Deref = un_op { if let rustc_hir::UnOp::Deref = un_op {
if is_null_ptr(cx, expr_deref) { if is_null_ptr(cx, expr_deref) {
cx.struct_span_lint(DEREF_NULLPTR, expr.span, |lint| { cx.struct_span_lint(DEREF_NULLPTR, expr.span, |lint| {
let mut err = let mut err = lint.build("dereferencing a null pointer");
lint.build("Dereferencing a null pointer causes undefined behavior");
err.span_label(expr.span, "a null pointer is dereferenced");
err.span_label( err.span_label(
expr.span, expr.span,
"this code causes undefined behavior when executed", "this code causes undefined behavior when executed",

View file

@ -7,25 +7,25 @@ fn f() {
let a = 1; let a = 1;
let ub = *(a as *const i32); let ub = *(a as *const i32);
let ub = *(0 as *const i32); let ub = *(0 as *const i32);
//~^ ERROR Dereferencing a null pointer causes undefined behavior //~^ ERROR dereferencing a null pointer
let ub = *core::ptr::null::<i32>(); let ub = *core::ptr::null::<i32>();
//~^ ERROR Dereferencing a null pointer causes undefined behavior //~^ ERROR dereferencing a null pointer
let ub = *core::ptr::null_mut::<i32>(); let ub = *core::ptr::null_mut::<i32>();
//~^ ERROR Dereferencing a null pointer causes undefined behavior //~^ ERROR dereferencing a null pointer
let ub = *(core::ptr::null::<i16>() as *const i32); let ub = *(core::ptr::null::<i16>() as *const i32);
//~^ ERROR Dereferencing a null pointer causes undefined behavior //~^ ERROR dereferencing a null pointer
let ub = *(core::ptr::null::<i16>() as *mut i32 as *mut usize as *const u8); let ub = *(core::ptr::null::<i16>() as *mut i32 as *mut usize as *const u8);
//~^ ERROR Dereferencing a null pointer causes undefined behavior //~^ ERROR dereferencing a null pointer
let ub = &*core::ptr::null::<i32>(); let ub = &*core::ptr::null::<i32>();
//~^ ERROR Dereferencing a null pointer causes undefined behavior //~^ ERROR dereferencing a null pointer
core::ptr::addr_of!(*core::ptr::null::<i32>()); core::ptr::addr_of!(*core::ptr::null::<i32>());
//~^ ERROR Dereferencing a null pointer causes undefined behavior //~^ ERROR dereferencing a null pointer
std::ptr::addr_of_mut!(*core::ptr::null_mut::<i32>()); std::ptr::addr_of_mut!(*core::ptr::null_mut::<i32>());
//~^ ERROR Dereferencing a null pointer causes undefined behavior //~^ ERROR dereferencing a null pointer
let ub = *std::ptr::null::<i32>(); let ub = *std::ptr::null::<i32>();
//~^ ERROR Dereferencing a null pointer causes undefined behavior //~^ ERROR dereferencing a null pointer
let ub = *std::ptr::null_mut::<i32>(); let ub = *std::ptr::null_mut::<i32>();
//~^ ERROR Dereferencing a null pointer causes undefined behavior //~^ ERROR dereferencing a null pointer
} }
} }

View file

@ -1,11 +1,8 @@
error: Dereferencing a null pointer causes undefined behavior error: dereferencing a null pointer
--> $DIR/lint-deref-nullptr.rs:9:18 --> $DIR/lint-deref-nullptr.rs:9:18
| |
LL | let ub = *(0 as *const i32); LL | let ub = *(0 as *const i32);
| ^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
| |
| a null pointer is dereferenced
| this code causes undefined behavior when executed
| |
note: the lint level is defined here note: the lint level is defined here
--> $DIR/lint-deref-nullptr.rs:3:9 --> $DIR/lint-deref-nullptr.rs:3:9
@ -13,86 +10,59 @@ note: the lint level is defined here
LL | #![deny(deref_nullptr)] LL | #![deny(deref_nullptr)]
| ^^^^^^^^^^^^^ | ^^^^^^^^^^^^^
error: Dereferencing a null pointer causes undefined behavior error: dereferencing a null pointer
--> $DIR/lint-deref-nullptr.rs:11:18 --> $DIR/lint-deref-nullptr.rs:11:18
| |
LL | let ub = *core::ptr::null::<i32>(); LL | let ub = *core::ptr::null::<i32>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
| |
| a null pointer is dereferenced
| this code causes undefined behavior when executed
error: Dereferencing a null pointer causes undefined behavior error: dereferencing a null pointer
--> $DIR/lint-deref-nullptr.rs:13:18 --> $DIR/lint-deref-nullptr.rs:13:18
| |
LL | let ub = *core::ptr::null_mut::<i32>(); LL | let ub = *core::ptr::null_mut::<i32>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
| |
| a null pointer is dereferenced
| this code causes undefined behavior when executed
error: Dereferencing a null pointer causes undefined behavior error: dereferencing a null pointer
--> $DIR/lint-deref-nullptr.rs:15:18 --> $DIR/lint-deref-nullptr.rs:15:18
| |
LL | let ub = *(core::ptr::null::<i16>() as *const i32); LL | let ub = *(core::ptr::null::<i16>() as *const i32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
| |
| a null pointer is dereferenced
| this code causes undefined behavior when executed
error: Dereferencing a null pointer causes undefined behavior error: dereferencing a null pointer
--> $DIR/lint-deref-nullptr.rs:17:18 --> $DIR/lint-deref-nullptr.rs:17:18
| |
LL | let ub = *(core::ptr::null::<i16>() as *mut i32 as *mut usize as *const u8); LL | let ub = *(core::ptr::null::<i16>() as *mut i32 as *mut usize as *const u8);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
| |
| a null pointer is dereferenced
| this code causes undefined behavior when executed
error: Dereferencing a null pointer causes undefined behavior error: dereferencing a null pointer
--> $DIR/lint-deref-nullptr.rs:19:19 --> $DIR/lint-deref-nullptr.rs:19:19
| |
LL | let ub = &*core::ptr::null::<i32>(); LL | let ub = &*core::ptr::null::<i32>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
| |
| a null pointer is dereferenced
| this code causes undefined behavior when executed
error: Dereferencing a null pointer causes undefined behavior error: dereferencing a null pointer
--> $DIR/lint-deref-nullptr.rs:21:29 --> $DIR/lint-deref-nullptr.rs:21:29
| |
LL | core::ptr::addr_of!(*core::ptr::null::<i32>()); LL | core::ptr::addr_of!(*core::ptr::null::<i32>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
| |
| a null pointer is dereferenced
| this code causes undefined behavior when executed
error: Dereferencing a null pointer causes undefined behavior error: dereferencing a null pointer
--> $DIR/lint-deref-nullptr.rs:23:32 --> $DIR/lint-deref-nullptr.rs:23:32
| |
LL | std::ptr::addr_of_mut!(*core::ptr::null_mut::<i32>()); LL | std::ptr::addr_of_mut!(*core::ptr::null_mut::<i32>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
| |
| a null pointer is dereferenced
| this code causes undefined behavior when executed
error: Dereferencing a null pointer causes undefined behavior error: dereferencing a null pointer
--> $DIR/lint-deref-nullptr.rs:25:18 --> $DIR/lint-deref-nullptr.rs:25:18
| |
LL | let ub = *std::ptr::null::<i32>(); LL | let ub = *std::ptr::null::<i32>();
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
| |
| a null pointer is dereferenced
| this code causes undefined behavior when executed
error: Dereferencing a null pointer causes undefined behavior error: dereferencing a null pointer
--> $DIR/lint-deref-nullptr.rs:27:18 --> $DIR/lint-deref-nullptr.rs:27:18
| |
LL | let ub = *std::ptr::null_mut::<i32>(); LL | let ub = *std::ptr::null_mut::<i32>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
| |
| a null pointer is dereferenced
| this code causes undefined behavior when executed
error: aborting due to 10 previous errors error: aborting due to 10 previous errors