Skip to content

Commit

Permalink
Rollup merge of #75146 - tmiasko:range-overflow, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
Detect overflow in proc_macro_server subspan

* Detect overflow in proc_macro_server subspan
* Add tests for overflow in Vec::drain
* Add tests for overflow in String / VecDeque operations using ranges
  • Loading branch information
Dylan-DPC authored Sep 15, 2020
2 parents 4f0c245 + f8cfb2f commit fb9bb2b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_expand/src/proc_macro_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,12 +584,12 @@ impl server::Literal for Rustc<'_> {

let start = match start {
Bound::Included(lo) => lo,
Bound::Excluded(lo) => lo + 1,
Bound::Excluded(lo) => lo.checked_add(1)?,
Bound::Unbounded => 0,
};

let end = match end {
Bound::Included(hi) => hi + 1,
Bound::Included(hi) => hi.checked_add(1)?,
Bound::Excluded(hi) => hi,
Bound::Unbounded => length,
};
Expand Down
1 change: 1 addition & 0 deletions library/alloc/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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
Original file line number Diff line number Diff line change
@@ -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 @@ -467,6 +468,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 @@ -504,6 +519,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
25 changes: 25 additions & 0 deletions library/alloc/tests/vec.rs
Original file line number Diff line number Diff line change
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 @@ -645,13 +646,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
15 changes: 15 additions & 0 deletions library/alloc/tests/vec_deque.rs
Original file line number Diff line number Diff line change
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 fb9bb2b

Please sign in to comment.