Skip to content

Commit f8493d0

Browse files
author
bcoopers
committed
Change max size to isize
1 parent 4f06ced commit f8493d0

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/libcollections/vec.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,14 @@ use core::ops;
6565
use core::ptr;
6666
use core::ptr::Unique;
6767
use core::slice;
68+
use core::isize;
6869
use core::usize;
6970

7071
use borrow::{Cow, IntoCow};
7172

73+
// FIXME- fix places which assume the max vector allowed has memory usize::MAX.
74+
static MAX_MEMORY_SIZE: usize = isize::MAX as usize;
75+
7276
/// A growable list type, written `Vec<T>` but pronounced 'vector.'
7377
///
7478
/// # Examples
@@ -305,13 +309,15 @@ impl<T> Vec<T> {
305309
#[stable(feature = "rust1", since = "1.0.0")]
306310
pub fn reserve(&mut self, additional: usize) {
307311
if self.cap - self.len < additional {
308-
let err_msg = "Vec::reserve: `usize` overflow";
312+
let err_msg = "Vec::reserve: `isize` overflow";
309313

310314
let new_min_cap = self.len.checked_add(additional).expect(err_msg);
311-
match new_min_cap.checked_next_power_of_two() {
312-
None => self.grow_capacity(usize::MAX),
313-
Some(x) => self.grow_capacity(x),
314-
}
315+
if new_min_cap > MAX_MEMORY_SIZE { panic!(err_msg) }
316+
self.grow_capacity(match new_min_cap.checked_next_power_of_two() {
317+
Some(x) if x > MAX_MEMORY_SIZE => MAX_MEMORY_SIZE,
318+
None => MAX_MEMORY_SIZE,
319+
Some(x) => x,
320+
});
315321
}
316322
}
317323

@@ -642,10 +648,10 @@ impl<T> Vec<T> {
642648
#[inline(never)]
643649
fn resize<T>(vec: &mut Vec<T>) {
644650
let old_size = vec.cap * mem::size_of::<T>();
645-
if old_size == usize::MAX { panic!("capacity overflow") }
651+
if old_size >= MAX_MEMORY_SIZE { panic!("capacity overflow") }
646652
let mut size = max(old_size, 2 * mem::size_of::<T>()) * 2;
647-
if old_size > size {
648-
size = usize::MAX;
653+
if old_size > size || size > MAX_MEMORY_SIZE {
654+
size = MAX_MEMORY_SIZE;
649655
}
650656
unsafe {
651657
let ptr = alloc_or_realloc(*vec.ptr, old_size, size);

0 commit comments

Comments
 (0)