about summary refs log tree commit diff
path: root/library/alloc
diff options
context:
space:
mode:
authorJacob Pratt <jacob@jhpratt.dev>2024-03-20 20:29:46 -0400
committerGitHub <noreply@github.com>2024-03-20 20:29:46 -0400
commit7a5ffccded0180807ba3de6bef06037b9ec89257 (patch)
treea6b6535380292033ba7d3f891509ba531e03923f /library/alloc
parentc6a49220d61f904063f5374c13961949509578e6 (diff)
parent92f668c20b155736ef8278cb02456340097f0840 (diff)
downloadrust-7a5ffccded0180807ba3de6bef06037b9ec89257.tar.gz
rust-7a5ffccded0180807ba3de6bef06037b9ec89257.zip
Rollup merge of #122765 - workingjubilee:test-for-vec-handling-usize-max, r=Nilstrieb
Add `usize::MAX` arg tests for Vec

Tests to prevent recurrence of the UB from the rust-lang/rust#122760 issue.

I skipped the `with_capacity`, `drain`, `reserve`, etc. APIs because they actually had a good assortment of tests earlier in the same file.

r? Nilstrieb
Diffstat (limited to 'library/alloc')
-rw-r--r--library/alloc/tests/vec.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs
index aa95b4e9770..f1f841fe190 100644
--- a/library/alloc/tests/vec.rs
+++ b/library/alloc/tests/vec.rs
@@ -2643,3 +2643,44 @@ fn test_vec_from_array_ref() {
 fn test_vec_from_array_mut_ref() {
     assert_eq!(Vec::from(&mut [1, 2, 3]), vec![1, 2, 3]);
 }
+
+/// This assortment of tests, in combination with miri, verifies we handle UB on fishy arguments
+/// in the stdlib. Draining and extending the allocation are fairly well-tested earlier, but
+/// `vec.insert(usize::MAX, val)` once slipped by!
+///
+/// All code that manipulates the collection types should be tested with "trivially wrong" args.
+#[test]
+fn max_dont_panic() {
+    let mut v = vec![0];
+    let _ = v.get(usize::MAX);
+    v.shrink_to(usize::MAX);
+    v.truncate(usize::MAX);
+}
+
+#[test]
+#[should_panic]
+fn max_insert() {
+    let mut v = vec![0];
+    v.insert(usize::MAX, 1);
+}
+
+#[test]
+#[should_panic]
+fn max_remove() {
+    let mut v = vec![0];
+    v.remove(usize::MAX);
+}
+
+#[test]
+#[should_panic]
+fn max_splice() {
+    let mut v = vec![0];
+    v.splice(usize::MAX.., core::iter::once(1));
+}
+
+#[test]
+#[should_panic]
+fn max_swap_remove() {
+    let mut v = vec![0];
+    v.swap_remove(usize::MAX);
+}