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