We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
checked_add
Layout::repeat
1 parent 3ff17e7 commit a983e05Copy full SHA for a983e05
src/libcore/alloc.rs
@@ -239,8 +239,11 @@ impl Layout {
239
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
240
#[inline]
241
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: () })?;
+ // This cannot overflow. Quoting from the invariant of Layout:
+ // > `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());
247
let alloc_size = padded_size.checked_mul(n)
248
.ok_or(LayoutErr { private: () })?;
249
0 commit comments