about summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-08-11 02:46:32 +0000
committerbors <bors@rust-lang.org>2022-08-11 02:46:32 +0000
commit187654481fd828e495919295369d33827f10e1c4 (patch)
tree6d55e7dfa5f26a0c40bfd7aee5a6e533222c9f9e /library/std/src
parent908fc5b26d15fc96d630ab921e70b2db77a532c4 (diff)
parent0cf950375140ab293ad5af1e7a59556335c409da (diff)
downloadrust-187654481fd828e495919295369d33827f10e1c4.tar.gz
rust-187654481fd828e495919295369d33827f10e1c4.zip
Auto merge of #100298 - BlackHoleFox:hashmap_keygen_cleanup, r=Mark-Simulacrum
Replace pointer casting in hashmap_random_keys with safe code

The old code was unnecessarily unsafe and relied on the layout of tuples always being the same as an array of the same size (which might be bad with `-Z randomize-layout`)?

The replacement has [identical codegen](https://rust.godbolt.org/z/qxsvdb8nx), so it seems like a reasonable change.
Diffstat (limited to 'library/std/src')
-rw-r--r--library/std/src/sys/unix/rand.rs18
1 files changed, 9 insertions, 9 deletions
diff --git a/library/std/src/sys/unix/rand.rs b/library/std/src/sys/unix/rand.rs
index bf49204881d..a6fe07873d7 100644
--- a/library/std/src/sys/unix/rand.rs
+++ b/library/std/src/sys/unix/rand.rs
@@ -1,13 +1,13 @@
-use crate::mem;
-use crate::slice;
-
 pub fn hashmap_random_keys() -> (u64, u64) {
-    let mut v = (0, 0);
-    unsafe {
-        let view = slice::from_raw_parts_mut(&mut v as *mut _ as *mut u8, mem::size_of_val(&v));
-        imp::fill_bytes(view);
-    }
-    v
+    const KEY_LEN: usize = core::mem::size_of::<u64>();
+
+    let mut v = [0u8; KEY_LEN * 2];
+    imp::fill_bytes(&mut v);
+
+    let key1 = v[0..KEY_LEN].try_into().unwrap();
+    let key2 = v[KEY_LEN..].try_into().unwrap();
+
+    (u64::from_ne_bytes(key1), u64::from_ne_bytes(key2))
 }
 
 #[cfg(all(