]> git.lizzy.rs Git - rust.git/commitdiff
Remove `checked_add` in `Layout::repeat`
authorMatthew Kraai <kraai@ftbfs.org>
Mon, 9 Dec 2019 14:49:37 +0000 (06:49 -0800)
committerMatthew Kraai <kraai@ftbfs.org>
Mon, 9 Dec 2019 14:49:37 +0000 (06:49 -0800)
src/libcore/alloc.rs

index 20248f7f6c13e374e471f423a37b48ef14556f24..3ebdb916f99358917e7358de02623bea515428ca 100644 (file)
@@ -239,8 +239,11 @@ pub fn pad_to_align(&self) -> Layout {
     #[unstable(feature = "alloc_layout_extra", issue = "55724")]
     #[inline]
     pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutErr> {
-        let padded_size = self.size().checked_add(self.padding_needed_for(self.align()))
-            .ok_or(LayoutErr { private: () })?;
+        // This cannot overflow. Quoting from the invariant of Layout:
+        // > `size`, when rounded up to the nearest multiple of `align`,
+        // > must not overflow (i.e., the rounded value must be less than
+        // > `usize::MAX`)
+        let padded_size = self.size() + self.padding_needed_for(self.align());
         let alloc_size = padded_size.checked_mul(n)
             .ok_or(LayoutErr { private: () })?;