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] add test offset of a field --- src/test/ui/lint/lint-deref-nullptr.rs | 26 ++++--- src/test/ui/lint/lint-deref-nullptr.stderr | 80 ++++++++++++---------- 2 files changed, 60 insertions(+), 46 deletions(-) diff --git a/src/test/ui/lint/lint-deref-nullptr.rs b/src/test/ui/lint/lint-deref-nullptr.rs index a5aee7351403..1dc54a5622b7 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 = *core::ptr::null_mut::(); + let ub = *ptr::null_mut::(); //~^ ERROR dereferencing a null pointer - let ub = *(core::ptr::null::() as *const i32); + let ub = *(ptr::null::() as *const i32); //~^ ERROR dereferencing a null pointer - let ub = *(core::ptr::null::() as *mut i32 as *mut usize as *const u8); + let ub = *(ptr::null::() as *mut i32 as *mut usize as *const u8); //~^ ERROR dereferencing a null pointer - let ub = &*core::ptr::null::(); + let ub = &*ptr::null::(); //~^ ERROR dereferencing a null pointer - core::ptr::addr_of!(*core::ptr::null::()); + ptr::addr_of!(*ptr::null::()); //~^ ERROR dereferencing a null pointer - std::ptr::addr_of_mut!(*core::ptr::null_mut::()); + ptr::addr_of_mut!(*ptr::null_mut::()); //~^ ERROR dereferencing a null pointer - let ub = *std::ptr::null::(); + let ub = *ptr::null::(); //~^ ERROR dereferencing a null pointer - let ub = *std::ptr::null_mut::(); + 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 ba27d2c45fcc..40fdfad2368e 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 @@ -10,59 +10,65 @@ note: the lint level is defined here LL | #![deny(deref_nullptr)] | ^^^^^^^^^^^^^ -error: dereferencing a null pointer - --> $DIR/lint-deref-nullptr.rs:11:18 - | -LL | let ub = *core::ptr::null::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed - -error: dereferencing a null pointer - --> $DIR/lint-deref-nullptr.rs:13:18 - | -LL | let ub = *core::ptr::null_mut::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed - -error: dereferencing a null pointer - --> $DIR/lint-deref-nullptr.rs:15:18 - | -LL | let ub = *(core::ptr::null::() as *const i32); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed - 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); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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:19:18 | -LL | let ub = &*core::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:21:29 + --> $DIR/lint-deref-nullptr.rs:21:18 | -LL | core::ptr::addr_of!(*core::ptr::null::()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ 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:23:32 + --> $DIR/lint-deref-nullptr.rs:23:18 | -LL | std::ptr::addr_of_mut!(*core::ptr::null_mut::()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed +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:18 + --> $DIR/lint-deref-nullptr.rs:25:19 | -LL | let ub = *std::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:27:18 + --> $DIR/lint-deref-nullptr.rs:27:23 | -LL | let ub = *std::ptr::null_mut::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed +LL | ptr::addr_of!(*ptr::null::()); + | ^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed -error: aborting due to 10 previous errors +error: dereferencing a null pointer + --> $DIR/lint-deref-nullptr.rs:29: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 + | +LL | let offset = ptr::addr_of!((*ptr::null::()).field); + | ^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed + +error: aborting due to 11 previous errors