Skip to content

Commit f6665a5

Browse files
workingjubileembrubeck
authored andcommitted
Add more tests for UB
1 parent f8136b8 commit f6665a5

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/tests.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,3 +1023,29 @@ fn drain_keep_rest() {
10231023

10241024
assert_eq!(a, SmallVec::<[i32; 3]>::from_slice(&[1i32, 3, 5, 6, 7, 8]));
10251025
}
1026+
1027+
/// This assortment of tests, in combination with miri, verifies we handle UB on fishy arguments
1028+
/// given to SmallVec. Draining and extending the allocation are fairly well-tested earlier, but
1029+
/// `smallvec.insert(usize::MAX, val)` once slipped by!
1030+
///
1031+
/// All code that indexes into SmallVecs should be tested with such "trivially wrong" args.
1032+
#[test]
1033+
fn max_dont_panic() {
1034+
let mut sv: SmallVec<[i32; 2]> = smallvec![0];
1035+
let _ = sv.get(usize::MAX);
1036+
sv.truncate(usize::MAX);
1037+
}
1038+
1039+
#[test]
1040+
#[should_panic]
1041+
fn max_remove() {
1042+
let mut sv: SmallVec<[i32; 2]> = smallvec![0];
1043+
sv.remove(usize::MAX);
1044+
}
1045+
1046+
#[test]
1047+
#[should_panic]
1048+
fn max_swap_remove() {
1049+
let mut sv: SmallVec<[i32; 2]> = smallvec![0];
1050+
sv.swap_remove(usize::MAX);
1051+
}

0 commit comments

Comments
 (0)