Skip to content

Commit 289a208

Browse files
committed
Add a precondition check for Layout::from_size_align_unchecked
1 parent 02c7a59 commit 289a208

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

library/core/src/alloc/layout.rs

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// collections, resulting in having to optimize down excess IR multiple times.
55
// Your performance intuition is useless. Run perf.
66

7+
use crate::assert_unsafe_precondition;
78
use crate::cmp;
89
use crate::error::Error;
910
use crate::fmt;
@@ -118,6 +119,15 @@ impl Layout {
118119
#[inline]
119120
#[rustc_allow_const_fn_unstable(ptr_alignment_type)]
120121
pub const unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Self {
122+
assert_unsafe_precondition!(
123+
check_library_ub,
124+
"Layout::from_size_align_unchecked requires that align is a power of 2 \
125+
and the rounded-up allocation size does not exceed isize::MAX",
126+
(
127+
size: usize = size,
128+
align: usize = align,
129+
) => Layout::from_size_align(size, align).is_ok()
130+
);
121131
// SAFETY: the caller is required to uphold the preconditions.
122132
unsafe { Layout { size, align: Alignment::new_unchecked(align) } }
123133
}

0 commit comments

Comments
 (0)