]> git.lizzy.rs Git - rust.git/commitdiff
Add tests for overflow in Vec::drain
authorTomasz Miąsko <tomasz.miasko@gmail.com>
Tue, 4 Aug 2020 00:00:00 +0000 (00:00 +0000)
committerTomasz Miąsko <tomasz.miasko@gmail.com>
Fri, 4 Sep 2020 21:16:53 +0000 (23:16 +0200)
library/alloc/tests/vec.rs

index 8e66c8a22cec594f39176dd62fb5c273ce6547c8..8abebd940d20fcf1527726031c73a01decea19c9 100644 (file)
@@ -3,6 +3,7 @@
 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};
@@ -566,6 +567,16 @@ 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() {
@@ -573,6 +584,20 @@ fn test_drain_inclusive_out_of_bounds() {
     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;