From fd3594b9ee56a288058de0b3e9d2aa5098e98944 Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Sat, 9 Nov 2019 15:19:09 -0500 Subject: [PATCH 1/7] Layout::pad_to_align is infallible --- src/libcore/alloc.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index 1b06baeb711c2..c6d18b04a31dd 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -213,16 +213,18 @@ impl Layout { /// Creates a layout by rounding the size of this layout up to a multiple /// of the layout's alignment. /// - /// Returns `Err` if the padded size would overflow. - /// /// This is equivalent to adding the result of `padding_needed_for` /// to the layout's current size. #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[inline] - pub fn pad_to_align(&self) -> Result { + pub fn pad_to_align(&self) -> Layout { let pad = self.padding_needed_for(self.align()); - let new_size = self.size().checked_add(pad) - .ok_or(LayoutErr { private: () })?; + // This cannot overflow: it is an invariant of Layout that + // > `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 new_size = self.size() + pad; + debug_assert!(new_size > self.size()); Layout::from_size_align(new_size, self.align()) } From 183e61b66174e76520b38cf5153f8fb6f5cdce94 Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Sat, 9 Nov 2019 15:28:32 -0500 Subject: [PATCH 2/7] Fix Layout::pad_to_align type mismatch --- src/libcore/alloc.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index c6d18b04a31dd..7d621090f07ae 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -224,9 +224,10 @@ impl Layout { // > must not overflow (i.e., the rounded value must be less than // > `usize::MAX`) let new_size = self.size() + pad; - debug_assert!(new_size > self.size()); - Layout::from_size_align(new_size, self.align()) + // SAFETY: This necessarily respectes the from_size_align + // prerequisites per the above. + unsafe { Layout::from_size_align_unchecked(new_size, self.align()) } } /// Creates a layout describing the record for `n` instances of From 87b8ab3734c9dfebe9ff9037f15b7e54f36f9a56 Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Sat, 9 Nov 2019 15:38:53 -0500 Subject: [PATCH 3/7] Remove Layout::pad_to_unlign unwrap --- src/liballoc/sync.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 4b10f089c2950..edd32fe6d41fd 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -751,7 +751,7 @@ impl Arc { // reference (see #54908). let layout = Layout::new::>() .extend(value_layout).unwrap().0 - .pad_to_align().unwrap(); + .pad_to_align(); let mem = Global.alloc(layout) .unwrap_or_else(|_| handle_alloc_error(layout)); From b688d480a6a90b995f3142f0db3338cad14f17c9 Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Sat, 9 Nov 2019 15:39:24 -0500 Subject: [PATCH 4/7] Remove Layout::pad_to_align unwrap --- src/liballoc/rc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index a11f9e8c14579..c92ac7f1343d8 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -897,7 +897,7 @@ impl Rc { // reference (see #54908). let layout = Layout::new::>() .extend(value_layout).unwrap().0 - .pad_to_align().unwrap(); + .pad_to_align(); // Allocate for the layout. let mem = Global.alloc(layout) From b9da350b09385dd969f5da3c3f9be0b356897099 Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Sat, 9 Nov 2019 18:28:41 -0500 Subject: [PATCH 5/7] Fix typo Co-Authored-By: Mazdak Farrokhzad --- src/libcore/alloc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index 7d621090f07ae..d08440192b08d 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -225,7 +225,7 @@ impl Layout { // > `usize::MAX`) let new_size = self.size() + pad; - // SAFETY: This necessarily respectes the from_size_align + // SAFETY: This necessarily respects the from_size_align // prerequisites per the above. unsafe { Layout::from_size_align_unchecked(new_size, self.align()) } } From 6773064b0529c258c4f69bb7848371553447b088 Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Mon, 25 Nov 2019 16:05:33 -0500 Subject: [PATCH 6/7] Remove unsafe in Layout::pad_to_align --- src/libcore/alloc.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index d08440192b08d..68fa2b5c20da2 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -225,9 +225,7 @@ impl Layout { // > `usize::MAX`) let new_size = self.size() + pad; - // SAFETY: This necessarily respects the from_size_align - // prerequisites per the above. - unsafe { Layout::from_size_align_unchecked(new_size, self.align()) } + Layout::from_size_align(new_size, self.align()).unwrap() } /// Creates a layout describing the record for `n` instances of From d1e53da80992d27073b3e918bd56d6c0692f160c Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Mon, 25 Nov 2019 16:39:24 -0500 Subject: [PATCH 7/7] Clarify Layout::pad_to_align safety comment --- src/libcore/alloc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index 68fa2b5c20da2..4798769823f43 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -219,7 +219,7 @@ impl Layout { #[inline] pub fn pad_to_align(&self) -> Layout { let pad = self.padding_needed_for(self.align()); - // This cannot overflow: it is an invariant of Layout that + // 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`)