diff options
| author | Sebastian Hahn <sebastian@torproject.org> | 2020-02-17 18:55:41 +0100 |
|---|---|---|
| committer | Sebastian Hahn <sebastian@torproject.org> | 2020-02-18 23:57:48 +0100 |
| commit | 3e17d191fa47b9ab262d0efa3a39e500e9fb1667 (patch) | |
| tree | 6836e41cf732613d926f82d381398bcbc14348e9 /src/libcore | |
| parent | 0176a9eef845e7421b7e2f7ef015333a41a7c027 (diff) | |
Revert "Remove `checked_add` in `Layout::repeat`"
This fixes a a segfault in safe code, a stable regression. Reported in \#69225. This reverts commit a983e0590a43ed8b0f60417828efd4e79b51f494. Also adds a test for the expected behaviour.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/alloc.rs | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index 71f7f971eab..a04e75bc7ce 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -241,11 +241,13 @@ impl Layout { #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[inline] pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutErr> { - // 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()); + // 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: () })?; let alloc_size = padded_size.checked_mul(n).ok_or(LayoutErr { private: () })?; unsafe { |
