@@ -65,10 +65,14 @@ use core::ops;
65
65
use core:: ptr;
66
66
use core:: ptr:: Unique ;
67
67
use core:: slice;
68
+ use core:: isize;
68
69
use core:: usize;
69
70
70
71
use borrow:: { Cow , IntoCow } ;
71
72
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
+
72
76
/// A growable list type, written `Vec<T>` but pronounced 'vector.'
73
77
///
74
78
/// # Examples
@@ -305,13 +309,15 @@ impl<T> Vec<T> {
305
309
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
306
310
pub fn reserve ( & mut self , additional : usize ) {
307
311
if self . cap - self . len < additional {
308
- let err_msg = "Vec::reserve: `usize ` overflow" ;
312
+ let err_msg = "Vec::reserve: `isize ` overflow" ;
309
313
310
314
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
+ } ) ;
315
321
}
316
322
}
317
323
@@ -642,10 +648,10 @@ impl<T> Vec<T> {
642
648
#[ inline( never) ]
643
649
fn resize < T > ( vec : & mut Vec < T > ) {
644
650
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" ) }
646
652
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 ;
649
655
}
650
656
unsafe {
651
657
let ptr = alloc_or_realloc ( * vec. ptr , old_size, size) ;
0 commit comments