Skip to content

Commit

Permalink
Add tests for overflow in String / VecDeque operations using ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
tmiasko committed Sep 4, 2020
1 parent d98bac4 commit f8cfb2f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions library/alloc/tests/lib.rs
Expand Up @@ -14,6 +14,7 @@
#![feature(slice_ptr_get)]
#![feature(split_inclusive)]
#![feature(binary_heap_retain)]
#![feature(deque_range)]
#![feature(inplace_iteration)]
#![feature(iter_map_while)]

Expand Down
29 changes: 29 additions & 0 deletions library/alloc/tests/string.rs
@@ -1,6 +1,7 @@
use std::borrow::Cow;
use std::collections::TryReserveError::*;
use std::mem::size_of;
use std::ops::Bound::*;

pub trait IntoCow<'a, B: ?Sized>
where
Expand Down Expand Up @@ -463,6 +464,20 @@ fn test_drain() {
assert_eq!(t, "");
}

#[test]
#[should_panic]
fn test_drain_start_overflow() {
let mut s = String::from("abc");
s.drain((Excluded(usize::MAX), Included(0)));
}

#[test]
#[should_panic]
fn test_drain_end_overflow() {
let mut s = String::from("abc");
s.drain((Included(0), Included(usize::MAX)));
}

#[test]
fn test_replace_range() {
let mut s = "Hello, world!".to_owned();
Expand Down Expand Up @@ -500,6 +515,20 @@ fn test_replace_range_inclusive_out_of_bounds() {
s.replace_range(5..=5, "789");
}

#[test]
#[should_panic]
fn test_replace_range_start_overflow() {
let mut s = String::from("123");
s.replace_range((Excluded(usize::MAX), Included(0)), "");
}

#[test]
#[should_panic]
fn test_replace_range_end_overflow() {
let mut s = String::from("456");
s.replace_range((Included(0), Included(usize::MAX)), "");
}

#[test]
fn test_replace_range_empty() {
let mut s = String::from("12345");
Expand Down
15 changes: 15 additions & 0 deletions library/alloc/tests/vec_deque.rs
Expand Up @@ -2,6 +2,7 @@ use std::collections::TryReserveError::*;
use std::collections::{vec_deque::Drain, VecDeque};
use std::fmt::Debug;
use std::mem::size_of;
use std::ops::Bound::*;
use std::panic::{catch_unwind, AssertUnwindSafe};

use crate::hash;
Expand Down Expand Up @@ -115,6 +116,20 @@ fn test_index_out_of_bounds() {
deq[3];
}

#[test]
#[should_panic]
fn test_range_start_overflow() {
let deq = VecDeque::from(vec![1, 2, 3]);
deq.range((Included(0), Included(usize::MAX)));
}

#[test]
#[should_panic]
fn test_range_end_overflow() {
let deq = VecDeque::from(vec![1, 2, 3]);
deq.range((Excluded(usize::MAX), Included(0)));
}

#[derive(Clone, PartialEq, Debug)]
enum Taggy {
One(i32),
Expand Down

0 comments on commit f8cfb2f

Please sign in to comment.