diff --git a/aya/src/obj/btf/types.rs b/aya/src/obj/btf/types.rs index d31dcca2..6d65330a 100644 --- a/aya/src/obj/btf/types.rs +++ b/aya/src/obj/btf/types.rs @@ -894,12 +894,12 @@ unsafe fn read_array(data: &[u8], len: usize) -> Result, BtfError> { if mem::size_of::() * len > data.len() { return Err(BtfError::InvalidTypeInfo); } - - Ok((0..len) - .map(|i| { - ptr::read_unaligned::((data.as_ptr() as usize + i * mem::size_of::()) as *const T) - }) - .collect::>()) + let data = &data[0..mem::size_of::() * len]; + let r = data + .chunks(mem::size_of::()) + .map(|chunk| ptr::read_unaligned(chunk.as_ptr() as *const T)) + .collect(); + Ok(r) } impl BtfType { diff --git a/aya/src/obj/mod.rs b/aya/src/obj/mod.rs index dca36b70..c5abe056 100644 --- a/aya/src/obj/mod.rs +++ b/aya/src/obj/mod.rs @@ -1348,15 +1348,10 @@ pub(crate) fn copy_instructions(data: &[u8]) -> Result, ParseError if data.len() % mem::size_of::() > 0 { return Err(ParseError::InvalidProgramCode); } - let num_instructions = data.len() / mem::size_of::(); - let instructions = (0..num_instructions) - .map(|i| unsafe { - ptr::read_unaligned( - (data.as_ptr() as usize + i * mem::size_of::()) as *const bpf_insn, - ) - }) + let instructions = data + .chunks_exact(mem::size_of::()) + .map(|d| unsafe { ptr::read_unaligned(d.as_ptr() as *const bpf_insn) }) .collect::>(); - Ok(instructions) }