about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAliƩnore Bouttefeux <alienore.bouttefeux@gmail.com>2021-04-10 15:40:07 +0200
committerAliƩnore Bouttefeux <alienore.bouttefeux@gmail.com>2021-04-10 15:40:07 +0200
commitc288414757975874005f45b4bcb5c5d50cbe227a (patch)
treefa3e8f8bde5988d9a508aa4fd2e39c447de6b1b2
parent79666c8857870ed8c16de932781ad50624e1accb (diff)
downloadrust-c288414757975874005f45b4bcb5c5d50cbe227a.tar.gz
rust-c288414757975874005f45b4bcb5c5d50cbe227a.zip
add test offset of a field
-rw-r--r--src/test/ui/lint/lint-deref-nullptr.rs26
-rw-r--r--src/test/ui/lint/lint-deref-nullptr.stderr64
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 a5aee735140..1dc54a5622b 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::<i32>();
+        let ub = *ptr::null::<i32>();
+        //~^ ERROR dereferencing a null pointer
+        let ub = *ptr::null_mut::<i32>();
         //~^ ERROR dereferencing a null pointer
-        let ub = *core::ptr::null_mut::<i32>();
+        let ub = *(ptr::null::<i16>() as *const i32);
         //~^ ERROR dereferencing a null pointer
-        let ub = *(core::ptr::null::<i16>() as *const i32);
+        let ub = *(ptr::null::<i16>() as *mut i32 as *mut usize as *const u8);
         //~^ ERROR dereferencing a null pointer
-        let ub = *(core::ptr::null::<i16>() as *mut i32 as *mut usize as *const u8);
+        let ub = &*ptr::null::<i32>();
         //~^ ERROR dereferencing a null pointer
-        let ub = &*core::ptr::null::<i32>();
+        ptr::addr_of!(*ptr::null::<i32>());
         //~^ ERROR dereferencing a null pointer
-        core::ptr::addr_of!(*core::ptr::null::<i32>());
+        ptr::addr_of_mut!(*ptr::null_mut::<i32>());
         //~^ ERROR dereferencing a null pointer
-        std::ptr::addr_of_mut!(*core::ptr::null_mut::<i32>());
+        let ub = *ptr::null::<i32>();
         //~^ ERROR dereferencing a null pointer
-        let ub = *std::ptr::null::<i32>();
+        let ub = *ptr::null_mut::<i32>();
         //~^ ERROR dereferencing a null pointer
-        let ub = *std::ptr::null_mut::<i32>();
+        let offset = ptr::addr_of!((*ptr::null::<Struct>()).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 ba27d2c45fc..40fdfad2368 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::<i32>();
-   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
+LL |         let ub = *ptr::null::<i32>();
+   |                  ^^^^^^^^^^^^^^^^^^^ 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::<i32>();
-   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
+LL |         let ub = *ptr::null_mut::<i32>();
+   |                  ^^^^^^^^^^^^^^^^^^^^^^^ 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::<i16>() as *const i32);
-   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
+LL |         let ub = *(ptr::null::<i16>() 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::<i16>() 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::<i16>() as *mut i32 as *mut usize as *const u8);
-   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
+LL |         let ub = &*ptr::null::<i32>();
+   |                   ^^^^^^^^^^^^^^^^^^^ 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::<i32>();
-   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
+LL |         ptr::addr_of!(*ptr::null::<i32>());
+   |                       ^^^^^^^^^^^^^^^^^^^ 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::<i32>());
-   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
+LL |         ptr::addr_of_mut!(*ptr::null_mut::<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:31:18
    |
-LL |         std::ptr::addr_of_mut!(*core::ptr::null_mut::<i32>());
-   |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
+LL |         let ub = *ptr::null::<i32>();
+   |                  ^^^^^^^^^^^^^^^^^^^ 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::<i32>();
-   |                  ^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
+LL |         let ub = *ptr::null_mut::<i32>();
+   |                  ^^^^^^^^^^^^^^^^^^^^^^^ 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::<i32>();
-   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
+LL |         let offset = ptr::addr_of!((*ptr::null::<Struct>()).field);
+   |                                    ^^^^^^^^^^^^^^^^^^^^^^^^ this code causes undefined behavior when executed
 
-error: aborting due to 10 previous errors
+error: aborting due to 11 previous errors