From c93760da9dbe0c3117c678c977d48cd56a356f8d Mon Sep 17 00:00:00 2001 From: bcoopers Date: Sun, 29 Mar 2015 16:07:24 -0400 Subject: [PATCH] Vector can currently panic when pushing an element or reserving space for only half of the maximum size available on the architecture. This allows vectors to keep expanding with those two methods until the amount of bytes exceeds usize. --- src/libcollections/vec.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 14bc7f65e096..51ad64331e29 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -306,9 +306,12 @@ impl Vec { pub fn reserve(&mut self, additional: usize) { if self.cap - self.len < additional { let err_msg = "Vec::reserve: `usize` overflow"; - let new_cap = self.len.checked_add(additional).expect(err_msg) - .checked_next_power_of_two().expect(err_msg); - self.grow_capacity(new_cap); + + let new_min_cap = self.len.checked_add(additional).expect(err_msg); + match new_min_cap.checked_next_power_of_two() { + None => self.grow_capacity(new_min_cap), + Some(x) => self.grow_capacity(x), + } } } @@ -639,8 +642,11 @@ impl Vec { #[inline(never)] fn resize(vec: &mut Vec) { let old_size = vec.cap * mem::size_of::(); - let size = max(old_size, 2 * mem::size_of::()) * 2; - if old_size > size { panic!("capacity overflow") } + if old_size == std::usize::MAX { panic!("capacity overflow") } + let mut size = max(old_size, 2 * mem::size_of::()) * 2; + if old_size > size { + size = std::usize::MAX; + } unsafe { let ptr = alloc_or_realloc(*vec.ptr, old_size, size); if ptr.is_null() { ::alloc::oom() }