about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorRemy Rakic <remy.rakic@gmail.com>2020-02-28 12:22:28 +0100
committerRemy Rakic <remy.rakic@gmail.com>2020-02-28 12:22:28 +0100
commit1b0bb35ca3cd8e80c9255eefb1e35381ec9aa73a (patch)
treeda7aedaa16d5b0d96d2136ccf14a885cdd70a313 /src/libcore
parentbfc32dd106f0fe191b8bcd9b76348a8875c30a60 (diff)
downloadrust-1b0bb35ca3cd8e80c9255eefb1e35381ec9aa73a.tar.gz
rust-1b0bb35ca3cd8e80c9255eefb1e35381ec9aa73a.zip
Unrevert "Remove `checked_add` in `Layout::repeat`"
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/alloc.rs12
1 files changed, 5 insertions, 7 deletions
diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs
index a04e75bc7ce..71f7f971eab 100644
--- a/src/libcore/alloc.rs
+++ b/src/libcore/alloc.rs
@@ -241,13 +241,11 @@ impl Layout {
     #[unstable(feature = "alloc_layout_extra", issue = "55724")]
     #[inline]
     pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutErr> {
-        // Warning, removing the checked_add here led to segfaults in #67174. Further
-        // analysis in #69225 seems to indicate that this is an LTO-related
-        // miscompilation, so #67174 might be able to be reapplied in the future.
-        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: () })?;
 
         unsafe {