Skip to content

Commit bef7be0

Browse files
committed
Add a precondition check for Layout::from_size_align_unchecked
1 parent 9704e2d commit bef7be0

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

Diff for: core/src/alloc/layout.rs

+27-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use crate::error::Error;
88
use crate::ptr::{Alignment, NonNull};
9-
use crate::{cmp, fmt, mem};
9+
use crate::{assert_unsafe_precondition, cmp, fmt, mem};
1010

1111
// While this function is used in one place and its implementation
1212
// could be inlined, the previous attempts to do so made rustc
@@ -66,12 +66,25 @@ impl Layout {
6666
#[inline]
6767
#[rustc_allow_const_fn_unstable(ptr_alignment_type)]
6868
pub const fn from_size_align(size: usize, align: usize) -> Result<Self, LayoutError> {
69-
if !align.is_power_of_two() {
70-
return Err(LayoutError);
69+
if Layout::is_size_align_valid(size, align) {
70+
// SAFETY: Layout::is_size_align_valid checks the preconditions for this call.
71+
let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
72+
Ok(layout)
73+
} else {
74+
Err(LayoutError)
7175
}
76+
}
7277

73-
// SAFETY: just checked that align is a power of two.
74-
Layout::from_size_alignment(size, unsafe { Alignment::new_unchecked(align) })
78+
const fn is_size_align_valid(size: usize, align: usize) -> bool {
79+
if !align.is_power_of_two() {
80+
return false;
81+
}
82+
// SAFETY: Precondition checked directly above.
83+
let align = unsafe { Alignment::new_unchecked(align) };
84+
if size > Self::max_size_for_align(align) {
85+
return false;
86+
}
87+
true
7588
}
7689

7790
#[inline(always)]
@@ -116,6 +129,15 @@ impl Layout {
116129
#[inline]
117130
#[rustc_allow_const_fn_unstable(ptr_alignment_type)]
118131
pub const unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Self {
132+
assert_unsafe_precondition!(
133+
check_library_ub,
134+
"Layout::from_size_align_unchecked requires that align is a power of 2 \
135+
and the rounded-up allocation size does not exceed isize::MAX",
136+
(
137+
size: usize = size,
138+
align: usize = align,
139+
) => Layout::is_size_align_valid(size, align)
140+
);
119141
// SAFETY: the caller is required to uphold the preconditions.
120142
unsafe { Layout { size, align: Alignment::new_unchecked(align) } }
121143
}

Diff for: core/src/result.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,6 @@ impl<T, E> Result<T, E> {
14811481
#[track_caller]
14821482
#[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")]
14831483
pub unsafe fn unwrap_unchecked(self) -> T {
1484-
debug_assert!(self.is_ok());
14851484
match self {
14861485
Ok(t) => t,
14871486
// SAFETY: the safety contract must be upheld by the caller.
@@ -1513,7 +1512,6 @@ impl<T, E> Result<T, E> {
15131512
#[track_caller]
15141513
#[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")]
15151514
pub unsafe fn unwrap_err_unchecked(self) -> E {
1516-
debug_assert!(self.is_err());
15171515
match self {
15181516
// SAFETY: the safety contract must be upheld by the caller.
15191517
Ok(_) => unsafe { hint::unreachable_unchecked() },

0 commit comments

Comments
 (0)