From b2924a3a264732e6de6898a1f03d7cb22d1d0dc5 Mon Sep 17 00:00:00 2001 From: Dave Tucker Date: Fri, 9 Sep 2022 00:45:29 +0000 Subject: [PATCH] aya-log: Make miri happy Miri took issue about using slice::from_raw_parts without checking for alignment. Instead, we can simply convert to a [u8;16] into a [u16;8] by iterating in chunks of 2 and bitshifting (remembering that these arrays are in network-endian order). Signed-off-by: Dave Tucker --- aya-log/src/lib.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/aya-log/src/lib.rs b/aya-log/src/lib.rs index 5786955c..b56c16e6 100644 --- a/aya-log/src/lib.rs +++ b/aya-log/src/lib.rs @@ -53,7 +53,7 @@ use std::{ fmt::{LowerHex, UpperHex}, io, mem, net::{Ipv4Addr, Ipv6Addr}, - ptr, slice, str, + ptr, str, sync::Arc, }; @@ -412,10 +412,11 @@ fn log_buf(mut buf: &[u8], logger: &dyn Log) -> Result<(), ()> { full_log_msg.push_str(&value.format(last_hint.take())?); } Argument::ArrU16Len8 => { - let ptr = attr.value.as_ptr().cast::(); - let slice = unsafe { slice::from_raw_parts(ptr, 8) }; + let data: [u8; 16] = attr.value.try_into().map_err(|_| ())?; let mut value: [u16; 8] = Default::default(); - value.copy_from_slice(slice); + for (i, s) in data.chunks_exact(2).enumerate() { + value[i] = ((s[1] as u16) << 8) | s[0] as u16; + } full_log_msg.push_str(&value.format(last_hint.take())?); } Argument::Str => match str::from_utf8(attr.value) {