about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthew Kraai <kraai@ftbfs.org>2019-12-09 06:49:37 -0800
committerMatthew Kraai <kraai@ftbfs.org>2019-12-09 06:49:37 -0800
commita983e0590a43ed8b0f60417828efd4e79b51f494 (patch)
tree1464e9be05bc7d7e3c3d4e1793f9f6e0bd575a75
parent3ff17e7c5faf604dcbfcb96a786df78e80f7e4f4 (diff)
downloadrust-a983e0590a43ed8b0f60417828efd4e79b51f494.tar.gz
rust-a983e0590a43ed8b0f60417828efd4e79b51f494.zip
Remove `checked_add` in `Layout::repeat`
-rw-r--r--src/libcore/alloc.rs7
1 files changed, 5 insertions, 2 deletions
diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs
index 20248f7f6c1..3ebdb916f99 100644
--- a/src/libcore/alloc.rs
+++ b/src/libcore/alloc.rs
@@ -239,8 +239,11 @@ impl 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: () })?;