Skip to content

Commit

Permalink
do not return an error when a data directory failed parsing (#45)
Browse files Browse the repository at this point in the history
* do not return an error when a data directory failed parsing

* add some error logs
  • Loading branch information
ayoubfaouzi authored Aug 4, 2022
1 parent 91c735f commit ad766b6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Unit tests for CLR directory [#34](https://github.com/saferwall/pe/pull/28).
- Unit tests for Rich header [#33](https://github.com/saferwall/pe/pull/33).

## Changed

- Do not return an error when parsing a data directory fails [#45](https://github.com/saferwall/pe/pull/45).
- Remove pointers from fields in the main `File` structure [#44](https://github.com/saferwall/pe/pull/44).

### Fixed

- Fix getting section data repeatedly thanks to [wanglei-coder](https://github.com/wanglei-coder) [#41](https://github.com/saferwall/pe/pull/41).
Expand Down
18 changes: 9 additions & 9 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (pe *File) Parse() error {
// Parse the Rich header.
err = pe.ParseRichHeader()
if err != nil {
return err
pe.logger.Errorf("rich header parsing failed: %v", err)
}

// Parse the NT header.
Expand All @@ -178,7 +178,10 @@ func (pe *File) Parse() error {
}

// Parse COFF symbol table.
_ = pe.ParseCOFFSymbolTable()
err = pe.ParseCOFFSymbolTable()
if err != nil {
pe.logger.Errorf("coff symbols parsing failed: %v", err)
}

// Parse the Section Header.
err = pe.ParseSectionHeader()
Expand All @@ -192,8 +195,7 @@ func (pe *File) Parse() error {
}

// Parse the Data Directory entries.
err = pe.ParseDataDirectories()
return err
return pe.ParseDataDirectories()
}

// PrettyDataDirectory returns the string representations
Expand Down Expand Up @@ -276,24 +278,22 @@ func (pe *File) ParseDataDirectories() error {
// keep parsing data directories even though some entries fails.
defer func() {
if e := recover(); e != nil {
pe.logger.Errorf("Unhandled Exception when trying to parse data directory %s, reason: %v",
pe.logger.Errorf("unhandled exception when parsing data directory %s, reason: %v",
pe.PrettyDataDirectory(entryIndex), e)
foundErr = true
}
}()

// the last entry in the data directories must be zero and
// is reserved.
// the last entry in the data directories is reserved and must be zero.
if entryIndex == ImageDirectoryEntryReserved {
pe.Anomalies = append(pe.Anomalies, AnoReservedDataDirectoryEntry)
return
}

err := funcMaps[entryIndex](va, size)
if err != nil {
pe.logger.Warnf("Failed to parse data directory %s, reason: %v",
pe.logger.Warnf("failed to parse data directory %s, reason: %v",
pe.PrettyDataDirectory(entryIndex), err)
foundErr = true
}
}()
}
Expand Down

0 comments on commit ad766b6

Please sign in to comment.