Skip to content

Commit 126b4d2

Browse files
committed
io::Error: avoid double-boxing of custom repr on 32 bits
1 parent 7de190a commit 126b4d2

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

library/std/src/io/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ impl Error {
601601
}
602602

603603
fn _new(kind: ErrorKind, error: Box<dyn error::Error + Send + Sync>) -> Error {
604-
Error { repr: Repr::new_custom(Box::new(Custom { kind, error })) }
604+
Error { repr: Repr::new_custom(Custom { kind, error }) }
605605
}
606606

607607
/// Creates a new I/O error from a known kind of error as well as a constant

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ unsafe impl Send for Repr {}
133133
unsafe impl Sync for Repr {}
134134

135135
impl Repr {
136-
pub(super) fn new_custom(b: Box<Custom>) -> Self {
137-
let p = Box::into_raw(b).cast::<u8>();
136+
pub(super) fn new_custom(b: Custom) -> Self {
137+
let p = Box::into_raw(Box::new(b)).cast::<u8>();
138138
// Should only be possible if an allocator handed out a pointer with
139139
// wrong alignment.
140140
debug_assert_eq!(p.addr() & TAG_MASK, 0);
@@ -217,11 +217,11 @@ impl Repr {
217217
}
218218

219219
#[inline]
220-
pub(super) fn into_data(self) -> ErrorData<Box<Custom>> {
220+
pub(super) fn into_data(self) -> ErrorData<Custom> {
221221
let this = core::mem::ManuallyDrop::new(self);
222222
// Safety: We're a Repr, decode_repr is fine. The `Box::from_raw` is
223223
// safe because we prevent double-drop using `ManuallyDrop`.
224-
unsafe { decode_repr(this.0, |p| Box::from_raw(p)) }
224+
unsafe { decode_repr(this.0, |p| *Box::from_raw(p)) }
225225
}
226226
}
227227

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
55
use super::{Custom, ErrorData, ErrorKind, RawOsError, SimpleMessage};
66

7-
type Inner = ErrorData<Box<Custom>>;
7+
type Inner = ErrorData<Custom>;
88

99
pub(super) struct Repr(Inner);
1010

1111
impl Repr {
1212
#[inline]
13-
pub(super) fn new_custom(b: Box<Custom>) -> Self {
13+
pub(super) fn new_custom(b: Custom) -> Self {
1414
Self(Inner::Custom(b))
1515
}
1616
#[inline]
@@ -26,7 +26,7 @@ impl Repr {
2626
Self(Inner::SimpleMessage(m))
2727
}
2828
#[inline]
29-
pub(super) fn into_data(self) -> ErrorData<Box<Custom>> {
29+
pub(super) fn into_data(self) -> ErrorData<Custom> {
3030
self.0
3131
}
3232
#[inline]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ fn test_debug_error() {
1515
let msg = error_string(code);
1616
let kind = decode_error_kind(code);
1717
let err = Error {
18-
repr: Repr::new_custom(Box::new(Custom {
18+
repr: Repr::new_custom(Custom {
1919
kind: ErrorKind::InvalidInput,
2020
error: Box::new(Error { repr: super::Repr::new_os(code) }),
21-
})),
21+
}),
2222
};
2323
let expected = format!(
2424
"Custom {{ \

0 commit comments

Comments
 (0)