Skip to content

Commit a101f65

Browse files
workingjubileembrubeck
authored andcommitted
Add more tests for UB
1 parent 7cf929a commit a101f65

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/tests.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,3 +1070,36 @@ fn test_extract_if() {
10701070
assert_eq!(a, SmallVec::<u8, 2>::from_slice(&[1u8, 2, 4, 5, 7, 8]));
10711071
assert_eq!(b, SmallVec::<u8, 2>::from_slice(&[3u8, 6]));
10721072
}
1073+
1074+
/// This assortment of tests, in combination with miri, verifies we handle UB on fishy arguments
1075+
/// given to SmallVec. Draining and extending the allocation are fairly well-tested earlier, but
1076+
/// `smallvec.insert(usize::MAX, val)` once slipped by!
1077+
///
1078+
/// All code that indexes into SmallVecs should be tested with such "trivially wrong" args.
1079+
#[test]
1080+
fn max_dont_panic() {
1081+
let mut sv: SmallVec<i32, 2> = smallvec![0];
1082+
let _ = sv.get(usize::MAX);
1083+
sv.truncate(usize::MAX);
1084+
}
1085+
1086+
#[test]
1087+
#[should_panic]
1088+
fn max_remove() {
1089+
let mut sv: SmallVec<i32, 2> = smallvec![0];
1090+
sv.remove(usize::MAX);
1091+
}
1092+
1093+
#[test]
1094+
#[should_panic]
1095+
fn max_swap_remove() {
1096+
let mut sv: SmallVec<i32, 2> = smallvec![0];
1097+
sv.swap_remove(usize::MAX);
1098+
}
1099+
1100+
#[test]
1101+
#[should_panic]
1102+
fn max_insert() {
1103+
let mut sv: SmallVec<i32, 2> = smallvec![0];
1104+
sv.insert(usize::MAX, 0);
1105+
}

0 commit comments

Comments
 (0)