Skip to content

Commit d98bac4

Browse files
committed
Add tests for overflow in Vec::drain
1 parent b54386a commit d98bac4

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

library/alloc/tests/vec.rs

+25
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::collections::TryReserveError::*;
33
use std::fmt::Debug;
44
use std::iter::InPlaceIterable;
55
use std::mem::size_of;
6+
use std::ops::Bound::*;
67
use std::panic::{catch_unwind, AssertUnwindSafe};
78
use std::rc::Rc;
89
use std::vec::{Drain, IntoIter};
@@ -566,13 +567,37 @@ fn test_drain_max_vec_size() {
566567
assert_eq!(v.len(), usize::MAX - 1);
567568
}
568569

570+
#[test]
571+
#[should_panic]
572+
fn test_drain_index_overflow() {
573+
let mut v = Vec::<()>::with_capacity(usize::MAX);
574+
unsafe {
575+
v.set_len(usize::MAX);
576+
}
577+
v.drain(0..=usize::MAX);
578+
}
579+
569580
#[test]
570581
#[should_panic]
571582
fn test_drain_inclusive_out_of_bounds() {
572583
let mut v = vec![1, 2, 3, 4, 5];
573584
v.drain(5..=5);
574585
}
575586

587+
#[test]
588+
#[should_panic]
589+
fn test_drain_start_overflow() {
590+
let mut v = vec![1, 2, 3];
591+
v.drain((Excluded(usize::MAX), Included(0)));
592+
}
593+
594+
#[test]
595+
#[should_panic]
596+
fn test_drain_end_overflow() {
597+
let mut v = vec![1, 2, 3];
598+
v.drain((Included(0), Included(usize::MAX)));
599+
}
600+
576601
#[test]
577602
fn test_drain_leak() {
578603
static mut DROPS: i32 = 0;

0 commit comments

Comments
 (0)