]> git.lizzy.rs Git - rust.git/commitdiff
Avoid overflow in `VecDeque::with_capacity_in()`.
authorHans Kratz <hans@appfour.com>
Mon, 18 Oct 2021 11:15:45 +0000 (13:15 +0200)
committerHans Kratz <hans@appfour.com>
Mon, 18 Oct 2021 11:18:12 +0000 (13:18 +0200)
library/alloc/src/collections/vec_deque/mod.rs

index c890ff4ac5e2b7bff45511a0c80d3f00bcaff865..de607c8fdab31dd098a5ee88c1f76dd81f41ac85 100644 (file)
@@ -543,9 +543,9 @@ pub fn new_in(alloc: A) -> VecDeque<T, A> {
     /// ```
     #[unstable(feature = "allocator_api", issue = "32838")]
     pub fn with_capacity_in(capacity: usize, alloc: A) -> VecDeque<T, A> {
+        assert!(capacity < 1_usize << usize::BITS - 1, "capacity overflow");
         // +1 since the ringbuffer always leaves one space empty
         let cap = cmp::max(capacity + 1, MINIMUM_CAPACITY + 1).next_power_of_two();
-        assert!(cap > capacity, "capacity overflow");
 
         VecDeque { tail: 0, head: 0, buf: RawVec::with_capacity_in(cap, alloc) }
     }