Skip to content

Commit

Permalink
Add more tests for UB
Browse files Browse the repository at this point in the history
  • Loading branch information
workingjubilee authored and mbrubeck committed Mar 20, 2024
1 parent f8136b8 commit f6665a5
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,3 +1023,29 @@ fn drain_keep_rest() {

assert_eq!(a, SmallVec::<[i32; 3]>::from_slice(&[1i32, 3, 5, 6, 7, 8]));
}

/// This assortment of tests, in combination with miri, verifies we handle UB on fishy arguments
/// given to SmallVec. Draining and extending the allocation are fairly well-tested earlier, but
/// `smallvec.insert(usize::MAX, val)` once slipped by!
///
/// All code that indexes into SmallVecs should be tested with such "trivially wrong" args.
#[test]
fn max_dont_panic() {
let mut sv: SmallVec<[i32; 2]> = smallvec![0];
let _ = sv.get(usize::MAX);
sv.truncate(usize::MAX);
}

#[test]
#[should_panic]
fn max_remove() {
let mut sv: SmallVec<[i32; 2]> = smallvec![0];
sv.remove(usize::MAX);
}

#[test]
#[should_panic]
fn max_swap_remove() {
let mut sv: SmallVec<[i32; 2]> = smallvec![0];
sv.swap_remove(usize::MAX);
}

0 comments on commit f6665a5

Please sign in to comment.