From 6ed5d835665af89c66ea8ec7f57130f92dc9b76f Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Thu, 3 Jul 2014 08:40:08 -0700 Subject: [PATCH 1/2] collections: grow should use the overflow-checked reserve_additional --- src/libcollections/vec.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 2ffc168f82c0e..b8efaa78af3a9 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -253,8 +253,7 @@ impl Vec { /// assert_eq!(vec, vec!("hello", "world", "world")); /// ``` pub fn grow(&mut self, n: uint, value: &T) { - let new_len = self.len() + n; - self.reserve(new_len); + self.reserve_additional(n); let mut i: uint = 0u; while i < n { From 128edc387213e59f162e10b4c5c31e93365d87a6 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Thu, 3 Jul 2014 08:45:28 -0700 Subject: [PATCH 2/2] collections: Fix conditional when reserving extra vec space --- src/libcollections/vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index b8efaa78af3a9..d53ecabd5a9cb 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -496,7 +496,7 @@ impl Vec { /// assert!(vec.capacity() >= 10); /// ``` pub fn reserve(&mut self, capacity: uint) { - if capacity >= self.len { + if capacity > self.cap { self.reserve_exact(num::next_power_of_two(capacity)) } }