Skip to content

Commit

Permalink
Add tests for overflow in Vec::drain
Browse files Browse the repository at this point in the history
  • Loading branch information
tmiasko committed Sep 4, 2020
1 parent b54386a commit d98bac4
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions library/alloc/tests/vec.rs
Expand Up @@ -3,6 +3,7 @@ use std::collections::TryReserveError::*;
use std::fmt::Debug;
use std::iter::InPlaceIterable;
use std::mem::size_of;
use std::ops::Bound::*;
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::rc::Rc;
use std::vec::{Drain, IntoIter};
Expand Down Expand Up @@ -566,13 +567,37 @@ fn test_drain_max_vec_size() {
assert_eq!(v.len(), usize::MAX - 1);
}

#[test]
#[should_panic]
fn test_drain_index_overflow() {
let mut v = Vec::<()>::with_capacity(usize::MAX);
unsafe {
v.set_len(usize::MAX);
}
v.drain(0..=usize::MAX);
}

#[test]
#[should_panic]
fn test_drain_inclusive_out_of_bounds() {
let mut v = vec![1, 2, 3, 4, 5];
v.drain(5..=5);
}

#[test]
#[should_panic]
fn test_drain_start_overflow() {
let mut v = vec![1, 2, 3];
v.drain((Excluded(usize::MAX), Included(0)));
}

#[test]
#[should_panic]
fn test_drain_end_overflow() {
let mut v = vec![1, 2, 3];
v.drain((Included(0), Included(usize::MAX)));
}

#[test]
fn test_drain_leak() {
static mut DROPS: i32 = 0;
Expand Down

0 comments on commit d98bac4

Please sign in to comment.