Skip to content

Commit 1b0bb35

Browse files
committed
Unrevert "Remove checked_add in Layout::repeat"
1 parent bfc32dd commit 1b0bb35

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

src/libcore/alloc.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,11 @@ impl Layout {
241241
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
242242
#[inline]
243243
pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutErr> {
244-
// Warning, removing the checked_add here led to segfaults in #67174. Further
245-
// analysis in #69225 seems to indicate that this is an LTO-related
246-
// miscompilation, so #67174 might be able to be reapplied in the future.
247-
let padded_size = self
248-
.size()
249-
.checked_add(self.padding_needed_for(self.align()))
250-
.ok_or(LayoutErr { private: () })?;
244+
// This cannot overflow. Quoting from the invariant of Layout:
245+
// > `size`, when rounded up to the nearest multiple of `align`,
246+
// > must not overflow (i.e., the rounded value must be less than
247+
// > `usize::MAX`)
248+
let padded_size = self.size() + self.padding_needed_for(self.align());
251249
let alloc_size = padded_size.checked_mul(n).ok_or(LayoutErr { private: () })?;
252250

253251
unsafe {

0 commit comments

Comments
 (0)