Skip to content

Commit dc39b35

Browse files
committed
Expose provenance instead of transmuting provenance-ful memory to u128
1 parent 5b9db69 commit dc39b35

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

library/core/src/any.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,15 @@ impl TypeId {
775775
}
776776

777777
fn as_u128(self) -> u128 {
778-
// SAFETY: we know there are 16 bytes without provenance at this location
779-
unsafe { crate::ptr::read_unaligned(&self.data as *const _ as *const u128) }
778+
let mut bytes = [0; 16];
779+
780+
// This is a provenance-stripping memcpy.
781+
for (i, chunk) in self.data.iter().copied().enumerate() {
782+
let chunk = chunk.expose_provenance().to_ne_bytes();
783+
let start = i * chunk.len();
784+
bytes[start..(start + chunk.len())].copy_from_slice(&chunk);
785+
}
786+
u128::from_ne_bytes(bytes)
780787
}
781788
}
782789

0 commit comments

Comments
 (0)