about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2021-03-10 10:02:39 +0100
committerRalf Jung <post@ralfj.de>2021-03-10 10:20:27 +0100
commit4d748624c09692c05528b7c166b2ccb9cdbdda61 (patch)
tree6d4cfb91d519f241707dd7dd4d5adf6e0b3fb044
parent9f27a13f5f620d1fcdc810b82033cee55804c3be (diff)
downloadrust-4d748624c09692c05528b7c166b2ccb9cdbdda61.tar.gz
rust-4d748624c09692c05528b7c166b2ccb9cdbdda61.zip
add regression test
-rw-r--r--src/test/ui/consts/copy-intrinsic.rs31
-rw-r--r--src/test/ui/consts/copy-intrinsic.stderr37
2 files changed, 68 insertions, 0 deletions
diff --git a/src/test/ui/consts/copy-intrinsic.rs b/src/test/ui/consts/copy-intrinsic.rs
new file mode 100644
index 00000000000..6b46b9317d0
--- /dev/null
+++ b/src/test/ui/consts/copy-intrinsic.rs
@@ -0,0 +1,31 @@
+// ignore-tidy-linelength
+#![feature(const_mut_refs, const_intrinsic_copy, const_ptr_offset)]
+use std::ptr;
+
+const COPY_ZERO: () = unsafe {
+    // Since we are not copying anything, this should be allowed.
+    let src = ();
+    let mut dst = ();
+    ptr::copy_nonoverlapping(&src as *const _ as *const i32, &mut dst as *mut _ as *mut i32, 0);
+};
+
+const COPY_OOB_1: () = unsafe {
+    let mut x = 0i32;
+    let dangle = (&mut x as *mut i32).wrapping_add(10);
+    // Even if the first ptr is an int ptr and this is a ZST copy, we should detect dangling 2nd ptrs.
+    ptr::copy_nonoverlapping(0x100 as *const i32, dangle, 0); //~ ERROR any use of this value will cause an error
+    //~| memory access failed: pointer must be in-bounds
+    //~| previously accepted
+};
+const COPY_OOB_2: () = unsafe {
+    let x = 0i32;
+    let dangle = (&x as *const i32).wrapping_add(10);
+    // Even if the second ptr is an int ptr and this is a ZST copy, we should detect dangling 1st ptrs.
+    ptr::copy_nonoverlapping(dangle, 0x100 as *mut i32, 0); //~ ERROR any use of this value will cause an error
+    //~| memory access failed: pointer must be in-bounds
+    //~| previously accepted
+};
+
+
+fn main() {
+}
diff --git a/src/test/ui/consts/copy-intrinsic.stderr b/src/test/ui/consts/copy-intrinsic.stderr
new file mode 100644
index 00000000000..9157ba50dde
--- /dev/null
+++ b/src/test/ui/consts/copy-intrinsic.stderr
@@ -0,0 +1,37 @@
+error: any use of this value will cause an error
+  --> $DIR/copy-intrinsic.rs:16:5
+   |
+LL | / const COPY_OOB_1: () = unsafe {
+LL | |     let mut x = 0i32;
+LL | |     let dangle = (&mut x as *mut i32).wrapping_add(10);
+LL | |     // Even if the first ptr is an int ptr and this is a ZST copy, we should detect dangling 2nd ptrs.
+LL | |     ptr::copy_nonoverlapping(0x100 as *const i32, dangle, 0);
+   | |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: pointer must be in-bounds at offset 40, but is outside bounds of alloc4 which has size 4
+LL | |
+LL | |
+LL | | };
+   | |__-
+   |
+   = note: `#[deny(const_err)]` on by default
+   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
+   = note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
+
+error: any use of this value will cause an error
+  --> $DIR/copy-intrinsic.rs:24:5
+   |
+LL | / const COPY_OOB_2: () = unsafe {
+LL | |     let x = 0i32;
+LL | |     let dangle = (&x as *const i32).wrapping_add(10);
+LL | |     // Even if the second ptr is an int ptr and this is a ZST copy, we should detect dangling 1st ptrs.
+LL | |     ptr::copy_nonoverlapping(dangle, 0x100 as *mut i32, 0);
+   | |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: pointer must be in-bounds at offset 40, but is outside bounds of alloc6 which has size 4
+LL | |
+LL | |
+LL | | };
+   | |__-
+   |
+   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
+   = note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
+
+error: aborting due to 2 previous errors
+