Skip to content

Commit a983e05

Browse files
committed
Remove checked_add in Layout::repeat
1 parent 3ff17e7 commit a983e05

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/libcore/alloc.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,11 @@ impl Layout {
239239
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
240240
#[inline]
241241
pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutErr> {
242-
let padded_size = self.size().checked_add(self.padding_needed_for(self.align()))
243-
.ok_or(LayoutErr { private: () })?;
242+
// This cannot overflow. Quoting from the invariant of Layout:
243+
// > `size`, when rounded up to the nearest multiple of `align`,
244+
// > must not overflow (i.e., the rounded value must be less than
245+
// > `usize::MAX`)
246+
let padded_size = self.size() + self.padding_needed_for(self.align());
244247
let alloc_size = padded_size.checked_mul(n)
245248
.ok_or(LayoutErr { private: () })?;
246249

0 commit comments

Comments
 (0)