mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Use reference algorithms for LEB128 parsing
This fixes a bug in signed LEB128 parsing (sign extension stage) which would sometimes cause debug info to look very strange.
This commit is contained in:
parent
a2cfb7eb94
commit
81b7c072ed
Notes:
sideshowbarker
2024-07-19 01:29:44 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/81b7c072edb
1 changed files with 13 additions and 14 deletions
|
@ -96,9 +96,9 @@ public:
|
|||
bool read_LEB128_unsigned(size_t& result)
|
||||
{
|
||||
const auto backup = m_offset;
|
||||
|
||||
result = 0;
|
||||
size_t num_bytes = 0;
|
||||
|
||||
size_t shift = 0;
|
||||
while (true) {
|
||||
if (eof()) {
|
||||
m_offset = backup;
|
||||
|
@ -106,12 +106,11 @@ public:
|
|||
return false;
|
||||
}
|
||||
|
||||
const u8 byte = m_bytes[m_offset];
|
||||
result = (result) | (static_cast<size_t>(byte & ~(1 << 7)) << (num_bytes * 7));
|
||||
++m_offset;
|
||||
if (!(byte & (1 << 7)))
|
||||
const u8 byte = m_bytes[m_offset++];
|
||||
result |= (byte & 0x7f) << shift;
|
||||
if (byte & 0x80)
|
||||
break;
|
||||
++num_bytes;
|
||||
shift += 7;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -122,9 +121,11 @@ public:
|
|||
const auto backup = m_offset;
|
||||
|
||||
result = 0;
|
||||
size_t num_bytes = 0;
|
||||
size_t shift = 0;
|
||||
u8 byte = 0;
|
||||
|
||||
size_t size = sizeof(ssize_t) * 8;
|
||||
|
||||
do {
|
||||
if (eof()) {
|
||||
m_offset = backup;
|
||||
|
@ -132,15 +133,13 @@ public:
|
|||
return false;
|
||||
}
|
||||
|
||||
byte = m_bytes[m_offset];
|
||||
result = (result) | (static_cast<size_t>(byte & ~(1 << 7)) << (num_bytes * 7));
|
||||
++m_offset;
|
||||
++num_bytes;
|
||||
byte = m_bytes[m_offset++];
|
||||
result |= (byte & 0x7f) << shift;
|
||||
} while (byte & (1 << 7));
|
||||
|
||||
if (num_bytes * 7 < sizeof(size_t) * 4 && (byte & 0x40)) {
|
||||
if (shift < size && (byte & 0x40)) {
|
||||
// sign extend
|
||||
result |= ((size_t)(-1) << (num_bytes * 7));
|
||||
result |= (0xffffffffu << shift);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
Loading…
Reference in a new issue