Skip to content

Commit

Permalink
Rollup merge of #63980 - DutchGhost:master, r=cramertj
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Centril authored Aug 29, 2019
2 parents e3028db + 080fdb8 commit 7391009
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,7 @@ impl str {
#[inline(always)]
#[rustc_const_unstable(feature="const_str_as_bytes")]
pub const fn as_bytes(&self) -> &[u8] {
#[repr(C)]
union Slices<'a> {
str: &'a str,
slice: &'a [u8],
Expand Down

0 comments on commit 7391009

Please sign in to comment.