Avoid integer to pointer casts

Instead, operate on byte slices if possible. That's the first step in
getting rid of miri warnings about Strict Provenance[0].

[0] https://doc.rust-lang.org/nightly/std/ptr/index.html#strict-provenance

Signed-off-by: Michal Rostecki <vadorovsky@gmail.com>
pull/398/head
Michal Rostecki 2 years ago
parent a93a975cc6
commit 2432677b2b

@ -894,12 +894,12 @@ unsafe fn read_array<T>(data: &[u8], len: usize) -> Result<Vec<T>, BtfError> {
if mem::size_of::<T>() * len > data.len() { if mem::size_of::<T>() * len > data.len() {
return Err(BtfError::InvalidTypeInfo); return Err(BtfError::InvalidTypeInfo);
} }
let data = &data[0..mem::size_of::<T>() * len];
Ok((0..len) let r = data
.map(|i| { .chunks(mem::size_of::<T>())
ptr::read_unaligned::<T>((data.as_ptr() as usize + i * mem::size_of::<T>()) as *const T) .map(|chunk| ptr::read_unaligned(chunk.as_ptr() as *const T))
}) .collect();
.collect::<Vec<T>>()) Ok(r)
} }
impl BtfType { impl BtfType {

@ -1348,15 +1348,10 @@ pub(crate) fn copy_instructions(data: &[u8]) -> Result<Vec<bpf_insn>, ParseError
if data.len() % mem::size_of::<bpf_insn>() > 0 { if data.len() % mem::size_of::<bpf_insn>() > 0 {
return Err(ParseError::InvalidProgramCode); return Err(ParseError::InvalidProgramCode);
} }
let num_instructions = data.len() / mem::size_of::<bpf_insn>(); let instructions = data
let instructions = (0..num_instructions) .chunks_exact(mem::size_of::<bpf_insn>())
.map(|i| unsafe { .map(|d| unsafe { ptr::read_unaligned(d.as_ptr() as *const bpf_insn) })
ptr::read_unaligned(
(data.as_ptr() as usize + i * mem::size_of::<bpf_insn>()) as *const bpf_insn,
)
})
.collect::<Vec<_>>(); .collect::<Vec<_>>();
Ok(instructions) Ok(instructions)
} }

Loading…
Cancel
Save