Skip to content

Commit

Permalink
improve reading of segment content
Browse files Browse the repository at this point in the history
  • Loading branch information
yeggor committed Oct 26, 2024
1 parent dd8115e commit 05105ec
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub enum Error {
InvalidImportShm(shared_memory::ShmemError),
#[error("invalid file-system path: {0}")]
InvalidImportFile(std::io::Error),
#[error("invalid segment")]
InvalidSegment,
#[error("coult not export to file: {0}")]
CannotExportToFile(std::io::Error),
#[error("coult not map file: {0}")]
Expand Down Expand Up @@ -411,14 +413,19 @@ impl<'db> RadareExporter<'db> {
&mut self,
seg: SegmentInfo,
) -> Result<flatbuffers::WIPOffset<schema::Segment<'db>>, Error> {
// TODO: remove this alloc?
let mut content = vec![0u8; seg.vsize as usize];
if seg.size > 0 {
assert!(seg.size <= seg.vsize);
content[..seg.size as usize].copy_from_slice(
&self.backing[seg.paddr as usize..(seg.paddr as usize + seg.size as usize)],
);
}
let content = if seg.size > 0 {
if seg.size > seg.vsize {
return Err(Error::InvalidSegment);
}

let start = seg.paddr as usize;
let end = start
.checked_add(seg.size as usize)
.ok_or(Error::InvalidSegment)?;
self.backing.get(start..end).ok_or(Error::InvalidSegment)?
} else {
&[]
};

let name = self.builder.create_string(seg.name);
let bytes = self.builder.create_vector(&content);
Expand Down

0 comments on commit 05105ec

Please sign in to comment.