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() {
return Err(BtfError::InvalidTypeInfo);
}
Ok((0..len)
.map(|i| {
ptr::read_unaligned::<T>((data.as_ptr() as usize + i * mem::size_of::<T>()) as *const T)
})
.collect::<Vec<T>>())
let data = &data[0..mem::size_of::<T>() * len];
let r = data
.chunks(mem::size_of::<T>())
.map(|chunk| ptr::read_unaligned(chunk.as_ptr() as *const T))
.collect();
Ok(r)
}
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 {
return Err(ParseError::InvalidProgramCode);
}
let num_instructions = data.len() / mem::size_of::<bpf_insn>();
let instructions = (0..num_instructions)
.map(|i| unsafe {
ptr::read_unaligned(
(data.as_ptr() as usize + i * mem::size_of::<bpf_insn>()) as *const bpf_insn,
)
})
let instructions = data
.chunks_exact(mem::size_of::<bpf_insn>())
.map(|d| unsafe { ptr::read_unaligned(d.as_ptr() as *const bpf_insn) })
.collect::<Vec<_>>();
Ok(instructions)
}

Loading…
Cancel
Save