]> git.lizzy.rs Git - rust.git/commitdiff
add ptr_offset_from OOB test, and update test errors
authorRalf Jung <post@ralfj.de>
Thu, 10 Mar 2022 23:56:19 +0000 (18:56 -0500)
committerRalf Jung <post@ralfj.de>
Thu, 10 Mar 2022 23:56:19 +0000 (18:56 -0500)
tests/compile-fail/intrinsics/copy_null.rs
tests/compile-fail/intrinsics/ptr_offset_0_plus_0.rs
tests/compile-fail/intrinsics/ptr_offset_from_oob.rs [new file with mode: 0644]
tests/compile-fail/intrinsics/write_bytes_null.rs
tests/compile-fail/null_pointer_deref_zst.rs
tests/compile-fail/null_pointer_write_zst.rs

index 60cb7e4eff513892f71e021ccacb414589d4f959..8f32ad1a760ffb4745f43936b8f420ec41c649a2 100644 (file)
@@ -9,5 +9,5 @@ fn main() {
     let mut data = [0u16; 4];
     let ptr = &mut data[0] as *mut u16;
     // Even copying 0 elements from NULL should error.
-    unsafe { copy_nonoverlapping(std::ptr::null(), ptr, 0); } //~ ERROR: memory access failed: 0x0 is not a valid pointer
+    unsafe { copy_nonoverlapping(std::ptr::null(), ptr, 0); } //~ ERROR: memory access failed: null pointer is not a valid pointer
 }
index d8a8f66e7aeed9694dbe7ab936b1d4a740dac722..248d85d65e24b4d9bcff8c6db80c67ebe908c44b 100644 (file)
@@ -1,4 +1,4 @@
-// error-pattern: pointer arithmetic failed: 0x0 is not a valid pointer
+// error-pattern: pointer arithmetic failed: null pointer is not a valid pointer
 
 fn main() {
     let x = 0 as *mut i32;
diff --git a/tests/compile-fail/intrinsics/ptr_offset_from_oob.rs b/tests/compile-fail/intrinsics/ptr_offset_from_oob.rs
new file mode 100644 (file)
index 0000000..ef1ca1e
--- /dev/null
@@ -0,0 +1,11 @@
+#![feature(core_intrinsics)]
+
+use std::intrinsics::ptr_offset_from;
+
+fn main() {
+    let start_ptr = &4 as *const _ as *const u8;
+    let length = 10;
+    let end_ptr = start_ptr.wrapping_add(length);
+    // Even if the offset is 0, a dangling OOB pointer is not allowed.
+    unsafe { ptr_offset_from(end_ptr, end_ptr) }; //~ERROR pointer at offset 10 is out-of-bounds
+}
index c3f77b27b4ff0ed16b964770a04504d917a5dee7..60966f0a94c0d257952b1640e05ce893614136a9 100644 (file)
@@ -6,5 +6,5 @@
 }
 
 fn main() {
-    unsafe { write_bytes::<u8>(std::ptr::null_mut(), 0, 0) }; //~ ERROR memory access failed: 0x0 is not a valid pointer
+    unsafe { write_bytes::<u8>(std::ptr::null_mut(), 0, 0) }; //~ ERROR memory access failed: null pointer is not a valid pointer
 }
index 04617c58f3cefd9b71c4a362eb9cea4f5f5f3d44..f3830c078e5e7da788889734ccd13b3d595677b1 100644 (file)
@@ -3,6 +3,6 @@
 
 #[allow(deref_nullptr)]
 fn main() {
-    let x: () = unsafe { *std::ptr::null() }; //~ ERROR dereferencing pointer failed: 0x0 is not a valid pointer
+    let x: () = unsafe { *std::ptr::null() }; //~ ERROR dereferencing pointer failed: null pointer is not a valid pointer
     panic!("this should never print: {:?}", x);
 }
index 46a8345b1b45a37090fda1644e553b772a0ebd06..63474d9651756444bce8152e0dc09587c99ffab5 100644 (file)
@@ -1,6 +1,6 @@
 // Some optimizations remove ZST accesses, thus masking this UB.
 // compile-flags: -Zmir-opt-level=0
-// error-pattern: memory access failed: 0x0 is not a valid pointer
+// error-pattern: memory access failed: null pointer is not a valid pointer
 
 #[allow(deref_nullptr)]
 fn main() {