Skip to content

Commit

Permalink
pe: Adjust coff symbol offset to account for the strtable length field (
Browse files Browse the repository at this point in the history
fixes #318)

* Adjust coff symbol offset to account for the strtable length field
  • Loading branch information
SquareMan authored Aug 1, 2022
1 parent 0fea4f0 commit eaba4ed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/pe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,26 @@ mod tests {
];

#[test]
fn issue_309() {
fn string_table_excludes_length() {
let coff = Coff::parse(&&COFF_FILE_SINGLE_STRING_IN_STRING_TABLE[..]).unwrap();
let string_table = coff.strings.to_vec().unwrap();

assert!(string_table == vec!["ExitProcess"]);
}

#[test]
fn symbol_name_excludes_length() {
let coff = Coff::parse(&COFF_FILE_SINGLE_STRING_IN_STRING_TABLE).unwrap();
let strings = coff.strings;
let symbols = coff
.symbols
.iter()
.filter(|(_, name, _)| name.is_none())
.map(|(_, _, sym)| sym.name(&strings).unwrap().to_owned())
.collect::<Vec<_>>();
assert_eq!(symbols, vec!["ExitProcess"])
}

#[test]
fn invalid_dos_header() {
if let Ok(_) = PE::parse(&INVALID_DOS_SIGNATURE) {
Expand Down
8 changes: 7 additions & 1 deletion src/pe/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,14 @@ impl Symbol {
///
/// Returns `None` if the name is inline.
pub fn name_offset(&self) -> Option<u32> {
// Symbol offset starts at the strtable's length, so let's adjust it
let length_field_size = core::mem::size_of::<u32>() as u32;

if self.name[0] == 0 {
self.name.pread_with(4, scroll::LE).ok()
self.name
.pread_with(4, scroll::LE)
.ok()
.map(|offset: u32| offset - length_field_size)
} else {
None
}
Expand Down

0 comments on commit eaba4ed

Please sign in to comment.