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
/// # #![allow(unused)]
/// use std::ptr;
/// unsafe {
/// let x = &*core::ptr::null::<i32>();
/// let x = core::ptr::addr_of!(*std::ptr::null::<i32>());
/// let x = *core::ptr::null::<i32>();
/// let x = &*ptr::null::<i32>();
/// let x = ptr::addr_of!(*ptr::null::<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 is_null_ptr(cx, expr_deref) {
cx.struct_span_lint(DEREF_NULLPTR, expr.span, |lint| {
let mut err =
lint.build("Dereferencing a null pointer causes undefined behavior");
err.span_label(expr.span, "a null pointer is dereferenced");
let mut err = lint.build("dereferencing a null pointer");
err.span_label(
expr.span,
"this code causes undefined behavior when executed",

View File

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