Skip to content

Commit

Permalink
mach: ignore fileoff only when filesize is empty
Browse files Browse the repository at this point in the history
Fixes #172
  • Loading branch information
raindev authored and m4b committed Jul 1, 2019
1 parent e57f2d4 commit 7f248a1
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/mach/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,16 @@ impl<'a> ctx::IntoCtx<container::Ctx> for Segment<'a> {
}
}

/// Read data that belongs to a segment if the offset is within the boundaries of bytes.
fn segment_data(bytes: &[u8], fileoff :u64, filesize :u64) -> Result<&[u8], error::Error> {
let data :&[u8] = if filesize != 0 {
bytes.pread_with(fileoff as usize, filesize as usize)?
} else {
&[]
};
Ok(data)
}

impl<'a> Segment<'a> {
/// Create a new, blank segment, with cmd either `LC_SEGMENT_64`, or `LC_SEGMENT`, depending on `ctx`.
/// **NB** You are responsible for providing a correctly marshalled byte array as the sections. You should not use this for anything other than writing.
Expand Down Expand Up @@ -424,7 +434,6 @@ impl<'a> Segment<'a> {
}
/// Convert the raw C 32-bit segment command to a generalized version
pub fn from_32(bytes: &'a[u8], segment: &SegmentCommand32, offset: usize, ctx: container::Ctx) -> Result<Self, error::Error> {
let data = bytes.pread_with(segment.fileoff as usize, segment.filesize as usize)?;
Ok(Segment {
cmd: segment.cmd,
cmdsize: segment.cmdsize,
Expand All @@ -437,15 +446,14 @@ impl<'a> Segment<'a> {
initprot: segment.initprot,
nsects: segment.nsects,
flags: segment.flags,
data,
data: segment_data(bytes, segment.fileoff as u64, segment.filesize as u64)?,
offset,
raw_data: bytes,
ctx,
})
}
/// Convert the raw C 64-bit segment command to a generalized version
pub fn from_64(bytes: &'a [u8], segment: &SegmentCommand64, offset: usize, ctx: container::Ctx) -> Result<Self, error::Error> {
let data = bytes.pread_with(segment.fileoff as usize, segment.filesize as usize)?;
Ok(Segment {
cmd: segment.cmd,
cmdsize: segment.cmdsize,
Expand All @@ -458,8 +466,8 @@ impl<'a> Segment<'a> {
initprot: segment.initprot,
nsects: segment.nsects,
flags: segment.flags,
data: segment_data(bytes, segment.fileoff, segment.filesize)?,
offset,
data,
raw_data: bytes,
ctx,
})
Expand Down

0 comments on commit 7f248a1

Please sign in to comment.