Skip to content

Commit c5ce4c0

Browse files
petrosaggbchalios
authored andcommitted
cpuid: replace transmute with safe version
Signed-off-by: Petros Angelatos <petrosagg@gmail.com>
1 parent 1d28149 commit c5ce4c0

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/cpuid/src/common.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ pub fn get_cpuid(function: u32, count: u32) -> Result<CpuidResult, Error> {
7171
/// Extracts the CPU vendor id from leaf 0x0.
7272
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
7373
pub fn get_vendor_id_from_host() -> Result<[u8; 12], Error> {
74-
// SAFETY: This is safe because the resulting type has a lower alignment requirement
75-
get_cpuid(0, 0).map(|vendor_entry| unsafe {
76-
std::mem::transmute::<[u32; 3], [u8; 12]>([
77-
vendor_entry.ebx,
78-
vendor_entry.edx,
79-
vendor_entry.ecx,
80-
])
74+
get_cpuid(0, 0).map(|vendor_entry| {
75+
let b = vendor_entry.ebx.to_ne_bytes();
76+
let d = vendor_entry.edx.to_ne_bytes();
77+
let c = vendor_entry.ecx.to_ne_bytes();
78+
[
79+
b[0], b[1], b[2], b[3], d[0], d[1], d[2], d[3], c[0], c[1], c[2], c[3],
80+
]
8181
})
8282
}
8383

0 commit comments

Comments
 (0)