Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 4 additions & 38 deletions src/linux/mem_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,44 +99,10 @@ impl MemReader {
src: usize,
length: std::num::NonZeroUsize,
) -> Result<Vec<u8>, CopyFromProcessError> {
let length = length.into();
let layout =
std::alloc::Layout::array::<u8>(length).map_err(|_err| CopyFromProcessError {
child: self.pid.as_raw(),
src,
offset: 0,
length,
source: nix::errno::Errno::EINVAL,
})?;

// SAFETY: we've guaranteed the layout we're allocating is valid at this point
let output = unsafe {
let ptr = std::alloc::alloc(layout);
if ptr.is_null() {
return Err(CopyFromProcessError {
child: self.pid.as_raw(),
src,
offset: 0,
length,
source: nix::errno::Errno::ENOMEM,
});
}
std::slice::from_raw_parts_mut(ptr, length)
};

match self.read(src, output) {
Ok(read) => {
// SAFETY: we've filled initialized read bytes of our allocation block
unsafe { Ok(Vec::from_raw_parts(output.as_mut_ptr(), read, length)) }
}
Err(err) => {
// SAFETY: the pointer and layout are the same we just allocated
unsafe {
std::alloc::dealloc(output.as_mut_ptr(), layout);
}
Err(err)
}
}
let mut output = vec![0u8; length.into()];
let bytes_read = self.read(src, &mut output)?;
output.truncate(bytes_read);
Ok(output)
}

pub fn read(&mut self, src: usize, dst: &mut [u8]) -> Result<usize, CopyFromProcessError> {
Expand Down
Loading