Skip to content

Commit

Permalink
Auto merge of #259 - mbrubeck:overflow, r=emilio
Browse files Browse the repository at this point in the history
Panic on arithmetic overflow in drain

Fixes #258.
  • Loading branch information
bors-servo authored Mar 23, 2021
2 parents 4e53e07 + c3e7f21 commit 503ce70
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,11 +725,11 @@ impl<A: Array> SmallVec<A> {
let len = self.len();
let start = match range.start_bound() {
Included(&n) => n,
Excluded(&n) => n + 1,
Excluded(&n) => n.checked_add(1).expect("Range start out of bounds"),
Unbounded => 0,
};
let end = match range.end_bound() {
Included(&n) => n + 1,
Included(&n) => n.checked_add(1).expect("Range end out of bounds"),
Excluded(&n) => n,
Unbounded => len,
};
Expand Down
7 changes: 7 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,13 @@ fn test_invalid_grow() {
v.grow(5);
}

#[test]
#[should_panic]
fn drain_overflow() {
let mut v: SmallVec<[u8; 8]> = smallvec![0];
v.drain(..=std::usize::MAX);
}

#[test]
fn test_insert_from_slice() {
let mut v: SmallVec<[u8; 8]> = SmallVec::new();
Expand Down

0 comments on commit 503ce70

Please sign in to comment.