fix cursor crash

This commit is contained in:
tqcq 2024-04-07 09:09:54 +00:00
parent 60ff77ec1d
commit 2d12117cfb

View File

@ -4,8 +4,8 @@
#include "internal.hh"
#include <stdexcept>
#include <cstring>
#include <stdexcept>
using namespace std;
@ -18,12 +18,11 @@ cursor::sleb128()
uint64_t result = 0;
unsigned shift = 0;
while (pos < sec->end) {
uint8_t byte = *(uint8_t*)(pos++);
result |= (uint64_t)(byte & 0x7f) << shift;
uint8_t byte = *(uint8_t *) (pos++);
result |= (uint64_t) (byte & 0x7f) << shift;
shift += 7;
if ((byte & 0x80) == 0) {
if (shift < sizeof(result)*8 && (byte & 0x40))
result |= -((uint64_t)1 << shift);
if (shift < sizeof(result) * 8 && (byte & 0x40)) result |= -((uint64_t) 1 << shift);
return result;
}
}
@ -91,8 +90,9 @@ cursor::string(std::string &out)
{
size_t size;
const char *p = this->cstr(&size);
out.resize(size);
memmove(&out.front(), p, size);
// out.resize(size);
// memmove(&out.front(), p, size);
out = std::string(p, p + size);
}
const char *
@ -100,12 +100,9 @@ cursor::cstr(size_t *size_out)
{
// Scan string size
const char *p = pos;
while (pos < sec->end && *pos)
pos++;
if (pos == sec->end)
throw format_error("unterminated string");
if (size_out)
*size_out = pos - p;
while (pos < sec->end && *pos) pos++;
if (pos == sec->end) throw format_error("unterminated string");
if (size_out) *size_out = pos - p;
pos++;
return p;
}
@ -179,18 +176,16 @@ cursor::skip_form(DW_FORM form)
case DW_FORM::sdata:
case DW_FORM::udata:
case DW_FORM::ref_udata:
while (pos < sec->end && (*(uint8_t*)pos & 0x80))
pos++;
while (pos < sec->end && (*(uint8_t *) pos & 0x80)) pos++;
pos++;
break;
case DW_FORM::string:
while (pos < sec->end && *pos)
pos++;
while (pos < sec->end && *pos) pos++;
pos++;
break;
case DW_FORM::indirect:
skip_form((DW_FORM)uleb128());
skip_form((DW_FORM) uleb128());
break;
default: