Skip to content

Commit

Permalink
Fix pointer decoding issue
Browse files Browse the repository at this point in the history
  • Loading branch information
hf committed Jan 11, 2020
1 parent 5860e19 commit 32d81b4
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src-input/duk_util_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,24 +197,20 @@ DUK_INTERNAL duk_size_t duk_encode_pointer_cstr(char* buf, duk_size_t sz, void*

duk_memzero(buf, sz);

if (sz < 2 * sizeof(ptraccess.bytes) + 1) {
if (sz < 2 * sizeof(void*) + 1) {
return 0;
}

for (i = 0; i < sz; i++) {
buf[i] = 0;
}

ptraccess.ptr = ptr;

for (i = 0; i < sizeof(ptraccess.bytes); i++) {
for (i = 0; i < sizeof(void*); i++) {
buf[2 * i + 0] = hex[(ptraccess.bytes[i] >> 4) & 0xF];
buf[2 * i + 1] = hex[(ptraccess.bytes[i] >> 0) & 0xF];
}

return 2 * sizeof(ptraccess.bytes);
return 2 * sizeof(void*);
#else
int compsize = DUK_SNPRINTF(buf, sz, "%p", ptr);
int compsize = DUK_SNPRINTF(buf, sz, DUK_STR_FMT_PTR, ptr);

if (compsize > 0 && ((duk_size_t) compsize) < sz) {
return (duk_size_t) compsize;
Expand All @@ -235,11 +231,11 @@ DUK_INTERNAL int duk_decode_pointer_cstr(const char* buf, duk_size_t sz, void**

*ptr = NULL;

if (sz <= 2 * sizeof(ptraccess.bytes) || 0 != buf[sz]) {
if (sz < 2 * sizeof(void*) + 1 || 0 != buf[sz]) {
return 0; /* syntax error */
}

for (i = 0; i < 2 * sizeof(ptraccess.bytes); i++) {
for (i = 0; i < 2 * sizeof(void*); i++) {
if (buf[i] >= '0' && buf[i] <= '9') {
continue;
}
Expand All @@ -251,18 +247,20 @@ DUK_INTERNAL int duk_decode_pointer_cstr(const char* buf, duk_size_t sz, void**
return 0; /* syntax error */
}

for (i = 0; i < sizeof(ptraccess.bytes); i++) {
for (i = 0; i < sizeof(void*); i++) {
a = (unsigned char) buf[2 * i + 0];
b = (unsigned char) buf[2 * i + 1];

if (a >= 'a') {
a -= 'a';
a += 10;
} else {
a -= '0';
}

if (b >= 'a') {
b -= 'a';
b += 10;
} else {
b -= '0';
}
Expand All @@ -287,7 +285,7 @@ DUK_INTERNAL int duk_decode_pointer_cstr(const char* buf, duk_size_t sz, void**
goto syntax_error;

safe_sscanf:
res = DUK_SSCANF(buf, "%p", ptr);
res = DUK_SSCANF(buf, DUK_STR_FMT_PTR, ptr);

if (1 != res) {
goto syntax_error;
Expand Down

0 comments on commit 32d81b4

Please sign in to comment.