Skip to content

Commit c3e7f21

Browse files
committed
Panic on arithmetic overflow in drain
Fixes #258.
1 parent 4e53e07 commit c3e7f21

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/lib.rs

Lines changed: 2 additions & 2 deletions
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

Lines changed: 7 additions & 0 deletions
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)