Skip to content

Commit 503ce70

Browse files
authored
Auto merge of #259 - mbrubeck:overflow, r=emilio
Panic on arithmetic overflow in drain Fixes #258.
2 parents 4e53e07 + c3e7f21 commit 503ce70

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -725,11 +725,11 @@ impl<A: Array> SmallVec<A> {
725725
let len = self.len();
726726
let start = match range.start_bound() {
727727
Included(&n) => n,
728-
Excluded(&n) => n + 1,
728+
Excluded(&n) => n.checked_add(1).expect("Range start out of bounds"),
729729
Unbounded => 0,
730730
};
731731
let end = match range.end_bound() {
732-
Included(&n) => n + 1,
732+
Included(&n) => n.checked_add(1).expect("Range end out of bounds"),
733733
Excluded(&n) => n,
734734
Unbounded => len,
735735
};

src/tests.rs

+7
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,13 @@ fn test_invalid_grow() {
423423
v.grow(5);
424424
}
425425

426+
#[test]
427+
#[should_panic]
428+
fn drain_overflow() {
429+
let mut v: SmallVec<[u8; 8]> = smallvec![0];
430+
v.drain(..=std::usize::MAX);
431+
}
432+
426433
#[test]
427434
fn test_insert_from_slice() {
428435
let mut v: SmallVec<[u8; 8]> = SmallVec::new();

0 commit comments

Comments
 (0)