Skip to content

Commit f8cfb2f

Browse files
committed
Add tests for overflow in String / VecDeque operations using ranges
1 parent d98bac4 commit f8cfb2f

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

library/alloc/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![feature(slice_ptr_get)]
1515
#![feature(split_inclusive)]
1616
#![feature(binary_heap_retain)]
17+
#![feature(deque_range)]
1718
#![feature(inplace_iteration)]
1819
#![feature(iter_map_while)]
1920

library/alloc/tests/string.rs

+29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::borrow::Cow;
22
use std::collections::TryReserveError::*;
33
use std::mem::size_of;
4+
use std::ops::Bound::*;
45

56
pub trait IntoCow<'a, B: ?Sized>
67
where
@@ -463,6 +464,20 @@ fn test_drain() {
463464
assert_eq!(t, "");
464465
}
465466

467+
#[test]
468+
#[should_panic]
469+
fn test_drain_start_overflow() {
470+
let mut s = String::from("abc");
471+
s.drain((Excluded(usize::MAX), Included(0)));
472+
}
473+
474+
#[test]
475+
#[should_panic]
476+
fn test_drain_end_overflow() {
477+
let mut s = String::from("abc");
478+
s.drain((Included(0), Included(usize::MAX)));
479+
}
480+
466481
#[test]
467482
fn test_replace_range() {
468483
let mut s = "Hello, world!".to_owned();
@@ -500,6 +515,20 @@ fn test_replace_range_inclusive_out_of_bounds() {
500515
s.replace_range(5..=5, "789");
501516
}
502517

518+
#[test]
519+
#[should_panic]
520+
fn test_replace_range_start_overflow() {
521+
let mut s = String::from("123");
522+
s.replace_range((Excluded(usize::MAX), Included(0)), "");
523+
}
524+
525+
#[test]
526+
#[should_panic]
527+
fn test_replace_range_end_overflow() {
528+
let mut s = String::from("456");
529+
s.replace_range((Included(0), Included(usize::MAX)), "");
530+
}
531+
503532
#[test]
504533
fn test_replace_range_empty() {
505534
let mut s = String::from("12345");

library/alloc/tests/vec_deque.rs

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::collections::TryReserveError::*;
22
use std::collections::{vec_deque::Drain, VecDeque};
33
use std::fmt::Debug;
44
use std::mem::size_of;
5+
use std::ops::Bound::*;
56
use std::panic::{catch_unwind, AssertUnwindSafe};
67

78
use crate::hash;
@@ -115,6 +116,20 @@ fn test_index_out_of_bounds() {
115116
deq[3];
116117
}
117118

119+
#[test]
120+
#[should_panic]
121+
fn test_range_start_overflow() {
122+
let deq = VecDeque::from(vec![1, 2, 3]);
123+
deq.range((Included(0), Included(usize::MAX)));
124+
}
125+
126+
#[test]
127+
#[should_panic]
128+
fn test_range_end_overflow() {
129+
let deq = VecDeque::from(vec![1, 2, 3]);
130+
deq.range((Excluded(usize::MAX), Included(0)));
131+
}
132+
118133
#[derive(Clone, PartialEq, Debug)]
119134
enum Taggy {
120135
One(i32),

0 commit comments

Comments
 (0)