Skip to content

Commit

Permalink
Fix UB due to iterator reaching before begin() in rich header parsing (
Browse files Browse the repository at this point in the history
…avast#987)

* Fix UB due to iterator before begin()

* pelib/RichHeader: change it++ to ++it

Co-authored-by: Peter Matula <p3t3r.matula@gmail.com>
  • Loading branch information
HoundThe and PeterMatula authored Jul 15, 2021
1 parent b3ba8a8 commit a2a4226
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/pelib/RichHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,15 +882,15 @@ namespace

// Start analyzing from the end - "Rich" marker
// and move upwards to decrypted "DanS" marker
for (auto i = richSignature - 1; i >= rich.begin(); --i)
for (auto it = std::make_reverse_iterator(richSignature); it < rich.rend(); ++it)
{
std::uint32_t decrypted_dword = *i ^ key;
std::uint32_t decrypted_dword = *it ^ key;
decryptedHeader.push_back(decrypted_dword);
// "DanS" - 0x536e6144 signals the start (end) of the rich header
if (decrypted_dword == 0x536e6144)
{
// Set the offset to "DanS"
this->offset = (i - rich.begin()) * 4;
this->offset = std::distance(it + 1, rich.rend()) * 4;
// Because we are analysing bottom up, reverse the vector
std::reverse(decryptedHeader.begin(), decryptedHeader.end());
break;
Expand Down

0 comments on commit a2a4226

Please sign in to comment.