From 389100921ab840bc235b3487d8b519197dec77c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ali=C3=A9nore=20Bouttefeux?= Date: Tue, 6 Apr 2021 22:01:00 +0200 Subject: [PATCH 1/7] add lint deref_nullptr --- compiler/rustc_lint/src/builtin.rs | 89 ++++++++++++++++++++++++++++++ compiler/rustc_lint/src/lib.rs | 1 + compiler/rustc_span/src/symbol.rs | 2 + library/core/src/ptr/mod.rs | 2 + 4 files changed, 94 insertions(+) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 3f16bb9f442ee..ab82070ce7342 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -2961,3 +2961,92 @@ impl<'tcx> LateLintPass<'tcx> for ClashingExternDeclarations { } } } + +declare_lint! { + /// The `deref_nullptr` lint detects when an null pointer is dereferenced, + /// which causes [undefined behavior]. + /// + /// ### Example + /// + /// ```rust,no_run + /// let x: i32 = unsafe { + /// *ptr::null() + /// }; + /// ``` + /// ```rust,no_run + /// unsafe { + /// *(0 as *const i32); + /// } + /// ``` + /// + /// {{produces}} + /// + /// ### Explanation + /// + /// + /// Dereferencing a null pointer causes [undefined behavior] even as a place expression, + /// like `&*(0 as *const i32)` or `addr_of!(*(0 as *const i32))`. + /// + /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html + pub DEREF_NULLPTR, + Warn, + "detects when an null pointer is dereferenced" +} + +declare_lint_pass!(DerefNullPtr => [DEREF_NULLPTR]); + +impl<'tcx> LateLintPass<'tcx> for DerefNullPtr { + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr<'_>) { + /// test if expression is a null ptr + fn is_null_ptr(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool { + match &expr.kind { + rustc_hir::ExprKind::Cast(ref expr, ref ty) => { + if let rustc_hir::TyKind::Ptr(_) = ty.kind { + return is_zero(expr) || is_null_ptr(cx, expr); + } + } + // check for call to `core::ptr::null` or `core::ptr::null_mut` + rustc_hir::ExprKind::Call(ref path, _) => { + if let rustc_hir::ExprKind::Path(ref qpath) = path.kind { + if let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id() { + return cx.tcx.is_diagnostic_item(sym::ptr_null, def_id) + || cx.tcx.is_diagnostic_item(sym::ptr_null_mut, def_id); + } + } + } + _ => {} + } + false + } + + /// test if experssion is the literal `0` + fn is_zero(expr: &hir::Expr<'_>) -> bool { + match &expr.kind { + rustc_hir::ExprKind::Lit(ref lit) => { + if let LitKind::Int(a, _) = lit.node { + return a == 0; + } + } + _ => {} + } + false + } + + if let rustc_hir::ExprKind::Unary(ref un_op, ref expr_deref) = expr.kind { + 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"); + err.span_label( + expr.span, + "this code causes undefined behavior when executed", + ); + err.emit(); + }); + } + } + } + } +} diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs index e2724b52453a5..2f46969b021e6 100644 --- a/compiler/rustc_lint/src/lib.rs +++ b/compiler/rustc_lint/src/lib.rs @@ -206,6 +206,7 @@ macro_rules! late_lint_mod_passes { UnreachablePub: UnreachablePub, ExplicitOutlivesRequirements: ExplicitOutlivesRequirements, InvalidValue: InvalidValue, + DerefNullPtr: DerefNullPtr, ] ); }; diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 54fea5515946f..95a8bda94222c 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -900,6 +900,8 @@ symbols! { profiler_runtime, ptr_guaranteed_eq, ptr_guaranteed_ne, + ptr_null, + ptr_null_mut, ptr_offset_from, pub_macro_rules, pub_restricted, diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index 6e207156b55a3..ad8696ab9272d 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -211,6 +211,7 @@ pub unsafe fn drop_in_place(to_drop: *mut T) { #[stable(feature = "rust1", since = "1.0.0")] #[rustc_promotable] #[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")] +#[rustc_diagnostic_item = "ptr_null"] pub const fn null() -> *const T { 0 as *const T } @@ -229,6 +230,7 @@ pub const fn null() -> *const T { #[stable(feature = "rust1", since = "1.0.0")] #[rustc_promotable] #[rustc_const_stable(feature = "const_ptr_null", since = "1.24.0")] +#[rustc_diagnostic_item = "ptr_null_mut"] pub const fn null_mut() -> *mut T { 0 as *mut T } From c7bc41f97aeef3c4f562c0fd2835acd6d6c2b5b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ali=C3=A9nore=20Bouttefeux?= Date: Wed, 7 Apr 2021 19:23:17 +0200 Subject: [PATCH 2/7] add test --- compiler/rustc_lint/src/builtin.rs | 2 + src/test/ui/cleanup-shortcircuit.rs | 3 + src/test/ui/lint/lint-deref-nullptr.rs | 32 +++++++ src/test/ui/lint/lint-deref-nullptr.stderr | 98 ++++++++++++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 src/test/ui/lint/lint-deref-nullptr.rs create mode 100644 src/test/ui/lint/lint-deref-nullptr.stderr diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index ab82070ce7342..cb81c330b718b 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -1,3 +1,5 @@ +// ignore-tidy-filelength + //! Lints in the Rust compiler. //! //! This contains lints which can feasibly be implemented as their own diff --git a/src/test/ui/cleanup-shortcircuit.rs b/src/test/ui/cleanup-shortcircuit.rs index 4f5197a5ba9b8..fe867ce1fbd57 100644 --- a/src/test/ui/cleanup-shortcircuit.rs +++ b/src/test/ui/cleanup-shortcircuit.rs @@ -3,6 +3,9 @@ // pretty-expanded FIXME #23616 +#![allow(deref_nullptr)] + + use std::env; pub fn main() { diff --git a/src/test/ui/lint/lint-deref-nullptr.rs b/src/test/ui/lint/lint-deref-nullptr.rs new file mode 100644 index 0000000000000..7b10e711c27b8 --- /dev/null +++ b/src/test/ui/lint/lint-deref-nullptr.rs @@ -0,0 +1,32 @@ +// test the deref_nullptr lint + +#![deny(deref_nullptr)] + +fn f() { + unsafe { + let a = 1; + let ub = *(a as *const i32); + let ub = *(0 as *const i32); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + let ub = *core::ptr::null::(); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + let ub = *core::ptr::null_mut::(); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + let ub = *(core::ptr::null::() as *const i32); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + let ub = *(core::ptr::null::() as *mut i32 as *mut usize as *const u8); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + let ub = &*core::ptr::null::(); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + core::ptr::addr_of!(*core::ptr::null::()); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + std::ptr::addr_of_mut!(*core::ptr::null_mut::()); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + let ub = *std::ptr::null::(); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + let ub = *std::ptr::null_mut::(); + //~^ ERROR Dereferencing a null pointer causes undefined behavior + } +} + +fn main() {} diff --git a/src/test/ui/lint/lint-deref-nullptr.stderr b/src/test/ui/lint/lint-deref-nullptr.stderr new file mode 100644 index 0000000000000..4fc6c54e19707 --- /dev/null +++ b/src/test/ui/lint/lint-deref-nullptr.stderr @@ -0,0 +1,98 @@ +error: Dereferencing a null pointer causes undefined behavior + --> $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 + | +note: the lint level is defined here + --> $DIR/lint-deref-nullptr.rs:3:9 + | +LL | #![deny(deref_nullptr)] + | ^^^^^^^^^^^^^ + +error: Dereferencing a null pointer causes undefined behavior + --> $DIR/lint-deref-nullptr.rs:11:18 + | +LL | let ub = *core::ptr::null::(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | a null pointer is dereferenced + | this code causes undefined behavior when executed + +error: Dereferencing a null pointer causes undefined behavior + --> $DIR/lint-deref-nullptr.rs:13:18 + | +LL | let ub = *core::ptr::null_mut::(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | a null pointer is dereferenced + | this code causes undefined behavior when executed + +error: Dereferencing a null pointer causes undefined behavior + --> $DIR/lint-deref-nullptr.rs:15:18 + | +LL | let ub = *(core::ptr::null::() as *const i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | a null pointer is dereferenced + | this code causes undefined behavior when executed + +error: Dereferencing a null pointer causes undefined behavior + --> $DIR/lint-deref-nullptr.rs:17:18 + | +LL | let ub = *(core::ptr::null::() as *mut i32 as *mut usize as *const u8); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | a null pointer is dereferenced + | this code causes undefined behavior when executed + +error: Dereferencing a null pointer causes undefined behavior + --> $DIR/lint-deref-nullptr.rs:19:19 + | +LL | let ub = &*core::ptr::null::(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | a null pointer is dereferenced + | this code causes undefined behavior when executed + +error: Dereferencing a null pointer causes undefined behavior + --> $DIR/lint-deref-nullptr.rs:21:29 + | +LL | core::ptr::addr_of!(*core::ptr::null::()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | a null pointer is dereferenced + | this code causes undefined behavior when executed + +error: Dereferencing a null pointer causes undefined behavior + --> $DIR/lint-deref-nullptr.rs:23:32 + | +LL | std::ptr::addr_of_mut!(*core::ptr::null_mut::()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | a null pointer is dereferenced + | this code causes undefined behavior when executed + +error: Dereferencing a null pointer causes undefined behavior + --> $DIR/lint-deref-nullptr.rs:25:18 + | +LL | let ub = *std::ptr::null::(); + | ^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | a null pointer is dereferenced + | this code causes undefined behavior when executed + +error: Dereferencing a null pointer causes undefined behavior + --> $DIR/lint-deref-nullptr.rs:27:18 + | +LL | let ub = *std::ptr::null_mut::(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | a null pointer is dereferenced + | this code causes undefined behavior when executed + +error: aborting due to 10 previous errors + From 3d215bdf42514311e48f5dc1cbf71cbd3d74e732 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ali=C3=A9nore=20Bouttefeux?= Date: Wed, 7 Apr 2021 20:53:58 +0200 Subject: [PATCH 3/7] change documentation of lint --- compiler/rustc_lint/src/builtin.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index cb81c330b718b..e449cd8c7559a 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -2969,16 +2969,25 @@ declare_lint! { /// which causes [undefined behavior]. /// /// ### Example - /// /// ```rust,no_run - /// let x: i32 = unsafe { - /// *ptr::null() + /// unsafe { + /// &*core::ptr::null::() /// }; /// ``` /// ```rust,no_run /// unsafe { - /// *(0 as *const i32); - /// } + /// core::ptr::addr_of!(*std::ptr::null::()) + /// }; + /// ``` + /// ```rust,no_run + /// unsafe { + /// *core::ptr::null::() + /// }; + /// ``` + /// ```rust,no_run + /// unsafe { + /// *(0 as *const i32) + /// }; /// ``` /// /// {{produces}} From 0531ed0b6220257d7c79405ca7d81d9fe66976be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ali=C3=A9nore=20Bouttefeux?= Date: Thu, 8 Apr 2021 12:09:32 +0200 Subject: [PATCH 4/7] fix lint doc --- compiler/rustc_lint/src/builtin.rs | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index e449cd8c7559a..905a808f51f97 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -2969,32 +2969,21 @@ declare_lint! { /// which causes [undefined behavior]. /// /// ### Example + /// /// ```rust,no_run + /// # #![allow(unused)] /// unsafe { - /// &*core::ptr::null::() - /// }; - /// ``` - /// ```rust,no_run - /// unsafe { - /// core::ptr::addr_of!(*std::ptr::null::()) - /// }; - /// ``` - /// ```rust,no_run - /// unsafe { - /// *core::ptr::null::() - /// }; - /// ``` - /// ```rust,no_run - /// unsafe { - /// *(0 as *const i32) - /// }; + /// let x = &*core::ptr::null::(); + /// let x = core::ptr::addr_of!(*std::ptr::null::()); + /// let x = *core::ptr::null::(); + /// let x = *(0 as *const i32); + /// } /// ``` /// /// {{produces}} /// /// ### Explanation /// - /// /// Dereferencing a null pointer causes [undefined behavior] even as a place expression, /// like `&*(0 as *const i32)` or `addr_of!(*(0 as *const i32))`. /// From 79666c8857870ed8c16de932781ad50624e1accb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ali=C3=A9nore=20Bouttefeux?= Date: Fri, 9 Apr 2021 16:13:04 +0200 Subject: [PATCH 5/7] changes based on review --- compiler/rustc_lint/src/builtin.rs | 10 ++-- src/test/ui/lint/lint-deref-nullptr.rs | 20 +++---- src/test/ui/lint/lint-deref-nullptr.stderr | 70 +++++++--------------- 3 files changed, 34 insertions(+), 66 deletions(-) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 905a808f51f97..f19572550ebd0 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -2972,10 +2972,10 @@ declare_lint! { /// /// ```rust,no_run /// # #![allow(unused)] + /// use std::ptr; /// unsafe { - /// let x = &*core::ptr::null::(); - /// let x = core::ptr::addr_of!(*std::ptr::null::()); - /// let x = *core::ptr::null::(); + /// let x = &*ptr::null::(); + /// let x = ptr::addr_of!(*ptr::null::()); /// 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", diff --git a/src/test/ui/lint/lint-deref-nullptr.rs b/src/test/ui/lint/lint-deref-nullptr.rs index 7b10e711c27b8..a5aee73514031 100644 --- a/src/test/ui/lint/lint-deref-nullptr.rs +++ b/src/test/ui/lint/lint-deref-nullptr.rs @@ -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::(); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer let ub = *core::ptr::null_mut::(); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer let ub = *(core::ptr::null::() as *const i32); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer let ub = *(core::ptr::null::() 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::(); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer core::ptr::addr_of!(*core::ptr::null::()); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer std::ptr::addr_of_mut!(*core::ptr::null_mut::()); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer let ub = *std::ptr::null::(); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer let ub = *std::ptr::null_mut::(); - //~^ ERROR Dereferencing a null pointer causes undefined behavior + //~^ ERROR dereferencing a null pointer } } diff --git a/src/test/ui/lint/lint-deref-nullptr.stderr b/src/test/ui/lint/lint-deref-nullptr.stderr index 4fc6c54e19707..ba27d2c45fcc7 100644 --- a/src/test/ui/lint/lint-deref-nullptr.stderr +++ b/src/test/ui/lint/lint-deref-nullptr.stderr @@ -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::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | 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::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | 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::() 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::() 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::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | 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::()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | 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::()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | 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::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | 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::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | 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 From c288414757975874005f45b4bcb5c5d50cbe227a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ali=C3=A9nore=20Bouttefeux?= Date: Sat, 10 Apr 2021 15:40:07 +0200 Subject: [PATCH 6/7] add test offset of a field --- src/test/ui/lint/lint-deref-nullptr.rs | 26 ++++++--- src/test/ui/lint/lint-deref-nullptr.stderr | 64 ++++++++++++---------- 2 files changed, 52 insertions(+), 38 deletions(-) diff --git a/src/test/ui/lint/lint-deref-nullptr.rs b/src/test/ui/lint/lint-deref-nullptr.rs index a5aee73514031..1dc54a5622b74 100644 --- a/src/test/ui/lint/lint-deref-nullptr.rs +++ b/src/test/ui/lint/lint-deref-nullptr.rs @@ -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::(); + let ub = *ptr::null::(); + //~^ ERROR dereferencing a null pointer + let ub = *ptr::null_mut::(); //~^ ERROR dereferencing a null pointer - let ub = *core::ptr::null_mut::(); + let ub = *(ptr::null::() as *const i32); //~^ ERROR dereferencing a null pointer - let ub = *(core::ptr::null::() as *const i32); + let ub = *(ptr::null::() as *mut i32 as *mut usize as *const u8); //~^ ERROR dereferencing a null pointer - let ub = *(core::ptr::null::() as *mut i32 as *mut usize as *const u8); + let ub = &*ptr::null::(); //~^ ERROR dereferencing a null pointer - let ub = &*core::ptr::null::(); + ptr::addr_of!(*ptr::null::()); //~^ ERROR dereferencing a null pointer - core::ptr::addr_of!(*core::ptr::null::()); + ptr::addr_of_mut!(*ptr::null_mut::()); //~^ ERROR dereferencing a null pointer - std::ptr::addr_of_mut!(*core::ptr::null_mut::()); + let ub = *ptr::null::(); //~^ ERROR dereferencing a null pointer - let ub = *std::ptr::null::(); + let ub = *ptr::null_mut::(); //~^ ERROR dereferencing a null pointer - let ub = *std::ptr::null_mut::(); + let offset = ptr::addr_of!((*ptr::null::()).field); //~^ ERROR dereferencing a null pointer } } diff --git a/src/test/ui/lint/lint-deref-nullptr.stderr b/src/test/ui/lint/lint-deref-nullptr.stderr index ba27d2c45fcc7..40fdfad2368e6 100644 --- a/src/test/ui/lint/lint-deref-nullptr.stderr +++ b/src/test/ui/lint/lint-deref-nullptr.stderr @@ -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 @@ -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::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed +LL | let ub = *ptr::null::(); + | ^^^^^^^^^^^^^^^^^^^ 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::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed +LL | let ub = *ptr::null_mut::(); + | ^^^^^^^^^^^^^^^^^^^^^^^ 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::() as *const i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed +LL | let ub = *(ptr::null::() 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::() 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::() as *mut i32 as *mut usize as *const u8); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed +LL | let ub = &*ptr::null::(); + | ^^^^^^^^^^^^^^^^^^^ 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::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed +LL | ptr::addr_of!(*ptr::null::()); + | ^^^^^^^^^^^^^^^^^^^ 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::()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed +LL | ptr::addr_of_mut!(*ptr::null_mut::()); + | ^^^^^^^^^^^^^^^^^^^^^^^ 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::()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed +LL | let ub = *ptr::null::(); + | ^^^^^^^^^^^^^^^^^^^ 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::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed +LL | let ub = *ptr::null_mut::(); + | ^^^^^^^^^^^^^^^^^^^^^^^ 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::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed +LL | let offset = ptr::addr_of!((*ptr::null::()).field); + | ^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed -error: aborting due to 10 previous errors +error: aborting due to 11 previous errors From 7f0f83a26fdec4caa2a8512f9fc611d504b7aad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ali=C3=A9nore=20Bouttefeux?= Date: Sat, 10 Apr 2021 16:17:09 +0200 Subject: [PATCH 7/7] remove redundant test --- src/test/ui/lint/lint-deref-nullptr.rs | 6 ++--- src/test/ui/lint/lint-deref-nullptr.stderr | 26 +++++++++------------- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/test/ui/lint/lint-deref-nullptr.rs b/src/test/ui/lint/lint-deref-nullptr.rs index 1dc54a5622b74..d052dbd9b647c 100644 --- a/src/test/ui/lint/lint-deref-nullptr.rs +++ b/src/test/ui/lint/lint-deref-nullptr.rs @@ -24,14 +24,12 @@ fn f() { //~^ ERROR dereferencing a null pointer let ub = &*ptr::null::(); //~^ ERROR dereferencing a null pointer + let ub = &*ptr::null_mut::(); + //~^ ERROR dereferencing a null pointer ptr::addr_of!(*ptr::null::()); //~^ ERROR dereferencing a null pointer ptr::addr_of_mut!(*ptr::null_mut::()); //~^ ERROR dereferencing a null pointer - let ub = *ptr::null::(); - //~^ ERROR dereferencing a null pointer - let ub = *ptr::null_mut::(); - //~^ ERROR dereferencing a null pointer let offset = ptr::addr_of!((*ptr::null::()).field); //~^ ERROR dereferencing a null pointer } diff --git a/src/test/ui/lint/lint-deref-nullptr.stderr b/src/test/ui/lint/lint-deref-nullptr.stderr index 40fdfad2368e6..c6f432e4e4207 100644 --- a/src/test/ui/lint/lint-deref-nullptr.stderr +++ b/src/test/ui/lint/lint-deref-nullptr.stderr @@ -41,34 +41,28 @@ LL | let ub = &*ptr::null::(); | ^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed error: dereferencing a null pointer - --> $DIR/lint-deref-nullptr.rs:27:23 + --> $DIR/lint-deref-nullptr.rs:27:19 + | +LL | let ub = &*ptr::null_mut::(); + | ^^^^^^^^^^^^^^^^^^^^^^^ 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::()); | ^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed error: dereferencing a null pointer - --> $DIR/lint-deref-nullptr.rs:29:27 + --> $DIR/lint-deref-nullptr.rs:31:27 | LL | ptr::addr_of_mut!(*ptr::null_mut::()); | ^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed error: dereferencing a null pointer - --> $DIR/lint-deref-nullptr.rs:31:18 - | -LL | let ub = *ptr::null::(); - | ^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed - -error: dereferencing a null pointer - --> $DIR/lint-deref-nullptr.rs:33:18 - | -LL | let ub = *ptr::null_mut::(); - | ^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed - -error: dereferencing a null pointer - --> $DIR/lint-deref-nullptr.rs:35:36 + --> $DIR/lint-deref-nullptr.rs:33:36 | LL | let offset = ptr::addr_of!((*ptr::null::()).field); | ^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed -error: aborting due to 11 previous errors +error: aborting due to 10 previous errors