diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 54528c50f1d1e..3aac1829a0c73 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -1482,7 +1482,7 @@ impl FromIterator for Vec { None => return Vec::new(), Some(element) => { let (lower, _) = iterator.size_hint(); - let mut vector = Vec::with_capacity(1 + lower); + let mut vector = Vec::with_capacity(lower.saturating_add(1)); unsafe { ptr::write(vector.get_unchecked_mut(0), element); vector.set_len(1); @@ -1570,10 +1570,11 @@ impl Vec { let len = self.len(); if len == self.capacity() { let (lower, _) = iterator.size_hint(); - self.reserve(lower + 1); + self.reserve(lower.saturating_add(1)); } unsafe { ptr::write(self.get_unchecked_mut(len), element); + // NB can't overflow since we would have had to alloc the address space self.set_len(len + 1); } }