@@ -370,6 +370,10 @@ impl<T, A: Allocator> RawVec<T, A> {
370
370
self . cap = Self :: capacity_from_bytes ( ptr. len ( ) ) ;
371
371
}
372
372
373
+ // This method must only be called after `needs_to_grow(len, additional)`
374
+ // succeeds. Otherwise, if `T` is zero-sized it will cause a divide by
375
+ // zero.
376
+ //
373
377
// This method is usually instantiated many times. So we want it to be as
374
378
// small as possible, to improve compile times. But we also want as much of
375
379
// its contents to be statically computable as possible, to make the
@@ -381,13 +385,9 @@ impl<T, A: Allocator> RawVec<T, A> {
381
385
// This is ensured by the calling contexts.
382
386
debug_assert ! ( additional > 0 ) ;
383
387
384
- if mem:: size_of :: < T > ( ) == 0 {
385
- // Since we return a capacity of `usize::MAX` when `elem_size` is
386
- // 0, getting to here necessarily means the `RawVec` is overfull.
387
- return Err ( CapacityOverflow . into ( ) ) ;
388
- }
389
-
390
- // Nothing we can really do about these checks, sadly.
388
+ // Nothing we can really do about these checks, sadly. (If this method
389
+ // is called for a zero-sized `T` after `needs_to_grow()` has
390
+ // succeeded, this early return will occur.)
391
391
let required_cap = len. checked_add ( additional) . ok_or ( CapacityOverflow ) ?;
392
392
393
393
// This guarantees exponential growth. The doubling cannot overflow
@@ -403,16 +403,17 @@ impl<T, A: Allocator> RawVec<T, A> {
403
403
Ok ( ( ) )
404
404
}
405
405
406
+ // This method must only be called after `needs_to_grow(len, additional)`
407
+ // succeeds. Otherwise, if `T` is zero-sized it will cause a divide by
408
+ // zero.
409
+ //
406
410
// The constraints on this method are much the same as those on
407
411
// `grow_amortized`, but this method is usually instantiated less often so
408
412
// it's less critical.
409
413
fn grow_exact ( & mut self , len : usize , additional : usize ) -> Result < ( ) , TryReserveError > {
410
- if mem:: size_of :: < T > ( ) == 0 {
411
- // Since we return a capacity of `usize::MAX` when the type size is
412
- // 0, getting to here necessarily means the `RawVec` is overfull.
413
- return Err ( CapacityOverflow . into ( ) ) ;
414
- }
415
-
414
+ // Nothing we can really do about these checks, sadly. (If this method
415
+ // is called for a zero-sized `T` after `needs_to_grow()` has
416
+ // succeeded, this early return will occur.)
416
417
let cap = len. checked_add ( additional) . ok_or ( CapacityOverflow ) ?;
417
418
let new_layout = Layout :: array :: < T > ( cap) ;
418
419
0 commit comments