pe-parser-library: Fix GetDataDirectoryEntry (#95)

* pe-parser-library: Fix GetDataDirectoryEntry

Avoids UB.

* pe-parser-library: clang-format

* pe-parser-library: Use ref
This commit is contained in:
William Woodruff 2019-10-21 19:51:41 -04:00 committed by GitHub
parent b3ab75577d
commit 40987e1f7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 10 deletions

View File

@ -234,5 +234,7 @@ const char *GetMachineAsString(parsed_pe *pe);
const char *GetSubsystemAsString(parsed_pe *pe); const char *GetSubsystemAsString(parsed_pe *pe);
// get a table or string by its data directory entry // get a table or string by its data directory entry
const void *GetDataDirectoryEntry(parsed_pe *pe, data_directory_kind dirnum); bool GetDataDirectoryEntry(parsed_pe *pe,
data_directory_kind dirnum,
std::vector<std::uint8_t> &raw_entry);
} // namespace peparse } // namespace peparse

View File

@ -2205,10 +2205,14 @@ const char *GetSubsystemAsString(parsed_pe *pe) {
} }
} }
const void *GetDataDirectoryEntry(parsed_pe *pe, data_directory_kind dirnum) { bool GetDataDirectoryEntry(parsed_pe *pe,
data_directory_kind dirnum,
std::vector<std::uint8_t> &raw_entry) {
raw_entry.clear();
if (pe == nullptr) { if (pe == nullptr) {
PE_ERR(PEERR_NONE); PE_ERR(PEERR_NONE);
return nullptr; return false;
} }
data_directory dir; data_directory dir;
@ -2221,30 +2225,30 @@ const void *GetDataDirectoryEntry(parsed_pe *pe, data_directory_kind dirnum) {
addr = dir.VirtualAddress + pe->peHeader.nt.OptionalHeader64.ImageBase; addr = dir.VirtualAddress + pe->peHeader.nt.OptionalHeader64.ImageBase;
} else { } else {
PE_ERR(PEERR_MAGIC); PE_ERR(PEERR_MAGIC);
return nullptr; return false;
} }
if (dir.Size <= 0) { if (dir.Size <= 0) {
PE_ERR(PEERR_SIZE); PE_ERR(PEERR_SIZE);
return nullptr; return false;
} }
section sec; section sec;
if (!getSecForVA(pe->internal->secs, addr, sec)) { if (!getSecForVA(pe->internal->secs, addr, sec)) {
PE_ERR(PEERR_SECTVA); PE_ERR(PEERR_SECTVA);
return nullptr; return false;
} }
auto off = static_cast<std::uint32_t>(addr - sec.sectionBase); auto off = static_cast<std::uint32_t>(addr - sec.sectionBase);
if (off + dir.Size >= sec.sectionData->bufLen) { if (off + dir.Size >= sec.sectionData->bufLen) {
PE_ERR(PEERR_SIZE); PE_ERR(PEERR_SIZE);
return nullptr; return false;
} }
std::vector<uint8_t> rawEntry(sec.sectionData->buf + off, raw_entry.assign(sec.sectionData->buf + off,
sec.sectionData->buf + off + dir.Size); sec.sectionData->buf + off + dir.Size);
return rawEntry.data(); return true;
} }
} // namespace peparse } // namespace peparse