Skip to content

Commit 7391009

Browse files
authored
Rollup merge of #63980 - DutchGhost:master, r=cramertj
add missing `#[repr(C)]` on the Slices union Adds the `#[repr(C)]` attribute to the `Slices` union used to convert an `&str` into a `&[u8]`. Without the attribute, the union has an unspecified layout: https://doc.rust-lang.org/reference/types/union.html, so performing the 'transmute' is unsound without the attribute (as far as I understand). The `Repr` union, used for converting a raw ptr + len to a slice has this attribute as well: https://github.com/rust-lang/rust/blob/master/src/libcore/ptr/mod.rs#L211-#L216
2 parents e3028db + 080fdb8 commit 7391009

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

src/libcore/str/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2170,6 +2170,7 @@ impl str {
21702170
#[inline(always)]
21712171
#[rustc_const_unstable(feature="const_str_as_bytes")]
21722172
pub const fn as_bytes(&self) -> &[u8] {
2173+
#[repr(C)]
21732174
union Slices<'a> {
21742175
str: &'a str,
21752176
slice: &'a [u8],

0 commit comments

Comments
 (0)