about summary refs log tree commit diff
diff options
context:
space:
mode:
authorxizheyin <xizheyin@smail.nju.edu.cn>2025-07-15 15:10:24 +0800
committerxizheyin <xizheyin@smail.nju.edu.cn>2025-07-15 15:12:24 +0800
commita74a28493a00900616d5e52cd85b8b6bae761935 (patch)
tree6ca6b57ef75e27b8b0d77962a25ea11b26e9f2fd
parent48caa5f8897263da935ebdc2bbec8a95b5d3d025 (diff)
downloadrust-a74a28493a00900616d5e52cd85b8b6bae761935.tar.gz
rust-a74a28493a00900616d5e52cd85b8b6bae761935.zip
Add tests for UB check in `set_len`, `from_raw_parts_in`, `from_parts_in`
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
-rw-r--r--tests/ui/precondition-checks/vec-from-parts.rs15
-rw-r--r--tests/ui/precondition-checks/vec-from-raw-parts.rs29
-rw-r--r--tests/ui/precondition-checks/vec-set-len.rs11
3 files changed, 55 insertions, 0 deletions
diff --git a/tests/ui/precondition-checks/vec-from-parts.rs b/tests/ui/precondition-checks/vec-from-parts.rs
new file mode 100644
index 00000000000..0bafb5aa715
--- /dev/null
+++ b/tests/ui/precondition-checks/vec-from-parts.rs
@@ -0,0 +1,15 @@
+//@ run-fail
+//@ compile-flags: -Cdebug-assertions=yes
+//@ error-pattern: unsafe precondition(s) violated: Vec::from_parts_in requires that length <= capacity
+#![feature(allocator_api)]
+
+use std::ptr::NonNull;
+
+fn main() {
+    let ptr: NonNull<i32> = std::ptr::NonNull::dangling();
+    // Test Vec::from_parts_in with length > capacity
+    unsafe {
+        let alloc = std::alloc::Global;
+        let _vec = Vec::from_parts_in(ptr, 10, 5, alloc);
+    }
+}
diff --git a/tests/ui/precondition-checks/vec-from-raw-parts.rs b/tests/ui/precondition-checks/vec-from-raw-parts.rs
new file mode 100644
index 00000000000..884d34c0a56
--- /dev/null
+++ b/tests/ui/precondition-checks/vec-from-raw-parts.rs
@@ -0,0 +1,29 @@
+//@ run-fail
+//@ compile-flags: -Cdebug-assertions=yes
+//@ error-pattern: unsafe precondition(s) violated: Vec::from_raw_parts_in requires that length <= capacity
+//@ revisions: vec_from_raw_parts vec_from_raw_parts_in string_from_raw_parts
+
+#![feature(allocator_api)]
+
+fn main() {
+    let ptr = std::ptr::null_mut::<u8>();
+    // Test Vec::from_raw_parts with length > capacity
+    unsafe {
+        #[cfg(vec_from_raw_parts)]
+        let _vec = Vec::from_raw_parts(ptr, 10, 5);
+    }
+
+    // Test Vec::from_raw_parts_in with length > capacity
+    unsafe {
+        let alloc = std::alloc::Global;
+        #[cfg(vec_from_raw_parts_in)]
+        let _vec = Vec::from_raw_parts_in(ptr, 10, 5, alloc);
+    }
+
+    // Test String::from_raw_parts with length > capacity
+    // Because it calls Vec::from_raw_parts, it should also fail
+    unsafe {
+        #[cfg(string_from_raw_parts)]
+        let _vec = String::from_raw_parts(ptr, 10, 5);
+    }
+}
diff --git a/tests/ui/precondition-checks/vec-set-len.rs b/tests/ui/precondition-checks/vec-set-len.rs
new file mode 100644
index 00000000000..0987e7fe028
--- /dev/null
+++ b/tests/ui/precondition-checks/vec-set-len.rs
@@ -0,0 +1,11 @@
+//@ run-fail
+//@ compile-flags: -Cdebug-assertions=yes
+//@ error-pattern: unsafe precondition(s) violated: Vec::set_len requires that new_len <= capacity()
+
+fn main() {
+    let mut vec: Vec<i32> = Vec::with_capacity(5);
+    // Test set_len with length > capacity
+    unsafe {
+        vec.set_len(10);
+    }
+}