Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 80262a8

Browse files
committed
Auto merge of rust-lang#126556 - saethlin:layout-precondition, r=<try>
Add a precondition check for Layout::from_size_align_unchecked Ran into this while looking into rust-lang/miri#3679. This is of course not the cause of the ICE, but the reproducer doesn't encounter a precondition check and it ought to.
2 parents 2c93fab + b3de247 commit 80262a8

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

library/core/src/alloc/layout.rs

Lines changed: 10 additions & 1 deletion
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
@@ -116,6 +116,15 @@ impl Layout {
116116
#[inline]
117117
#[rustc_allow_const_fn_unstable(ptr_alignment_type)]
118118
pub const unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Self {
119+
assert_unsafe_precondition!(
120+
check_library_ub,
121+
"Layout::from_size_align_unchecked requires that align is a power of 2 \
122+
and the rounded-up allocation size does not exceed isize::MAX",
123+
(
124+
size: usize = size,
125+
align: usize = align,
126+
) => Layout::from_size_align(size, align).is_ok()
127+
);
119128
// SAFETY: the caller is required to uphold the preconditions.
120129
unsafe { Layout { size, align: Alignment::new_unchecked(align) } }
121130
}

library/core/src/result.rs

Lines changed: 0 additions & 2 deletions
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)