Skip to content

Commit b898ad4

Browse files
committed
Ensure io::Error's bitpacked repr doesn't accidentally impl UnwindSafe
1 parent 9f4dc0b commit b898ad4

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

library/std/src/io/error/repr_bitpacked.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
105105
use super::{Custom, ErrorData, ErrorKind, SimpleMessage};
106106
use alloc::boxed::Box;
107+
use core::marker::PhantomData;
107108
use core::mem::{align_of, size_of};
108109
use core::ptr::NonNull;
109110

@@ -115,7 +116,7 @@ const TAG_OS: usize = 0b10;
115116
const TAG_SIMPLE: usize = 0b11;
116117

117118
#[repr(transparent)]
118-
pub(super) struct Repr(NonNull<()>);
119+
pub(super) struct Repr(NonNull<()>, PhantomData<ErrorData<Box<Custom>>>);
119120

120121
// All the types `Repr` stores internally are Send + Sync, and so is it.
121122
unsafe impl Send for Repr {}
@@ -145,7 +146,7 @@ impl Repr {
145146
// box, and `TAG_CUSTOM` just... isn't zero -- it's `0b01`). Therefore,
146147
// `TAG_CUSTOM + p` isn't zero and so `tagged` can't be, and the
147148
// `new_unchecked` is safe.
148-
let res = Self(unsafe { NonNull::new_unchecked(tagged) });
149+
let res = Self(unsafe { NonNull::new_unchecked(tagged) }, PhantomData);
149150
// quickly smoke-check we encoded the right thing (This generally will
150151
// only run in libstd's tests, unless the user uses -Zbuild-std)
151152
debug_assert!(matches!(res.data(), ErrorData::Custom(_)), "repr(custom) encoding failed");
@@ -156,7 +157,7 @@ impl Repr {
156157
pub(super) fn new_os(code: i32) -> Self {
157158
let utagged = ((code as usize) << 32) | TAG_OS;
158159
// Safety: `TAG_OS` is not zero, so the result of the `|` is not 0.
159-
let res = Self(unsafe { NonNull::new_unchecked(utagged as *mut ()) });
160+
let res = Self(unsafe { NonNull::new_unchecked(utagged as *mut ()) }, PhantomData);
160161
// quickly smoke-check we encoded the right thing (This generally will
161162
// only run in libstd's tests, unless the user uses -Zbuild-std)
162163
debug_assert!(
@@ -170,7 +171,7 @@ impl Repr {
170171
pub(super) fn new_simple(kind: ErrorKind) -> Self {
171172
let utagged = ((kind as usize) << 32) | TAG_SIMPLE;
172173
// Safety: `TAG_SIMPLE` is not zero, so the result of the `|` is not 0.
173-
let res = Self(unsafe { NonNull::new_unchecked(utagged as *mut ()) });
174+
let res = Self(unsafe { NonNull::new_unchecked(utagged as *mut ()) }, PhantomData);
174175
// quickly smoke-check we encoded the right thing (This generally will
175176
// only run in libstd's tests, unless the user uses -Zbuild-std)
176177
debug_assert!(
@@ -184,7 +185,7 @@ impl Repr {
184185
#[inline]
185186
pub(super) const fn new_simple_message(m: &'static SimpleMessage) -> Self {
186187
// Safety: References are never null.
187-
Self(unsafe { NonNull::new_unchecked(m as *const _ as *mut ()) })
188+
Self(unsafe { NonNull::new_unchecked(m as *const _ as *mut ()) }, PhantomData)
188189
}
189190

190191
#[inline]

0 commit comments

Comments
 (0)