Skip to content

Commit 0811cb3

Browse files
committed
Handle DOMException names with embedded NUL correctly
Thanks to saghul for noticing.
1 parent 395ef80 commit 0811cb3

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

quickjs.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57756,17 +57756,18 @@ static JSValue js_domexception_get_message(JSContext *ctx, JSValueConst this_val
5775657756
static JSValue js_domexception_get_code(JSContext *ctx, JSValueConst this_val)
5775757757
{
5775857758
JSDOMExceptionData *s;
57759-
const char *name;
57759+
const char *name, *it;
5776057760
int i;
57761+
size_t len;
5776157762

5776257763
s = JS_GetOpaque2(ctx, this_val, JS_CLASS_DOM_EXCEPTION);
5776357764
if (!s)
5776457765
return JS_EXCEPTION;
5776557766
if (s->code == -1) {
57766-
name = JS_ToCString(ctx, s->name);
57767+
name = JS_ToCStringLen(ctx, &len, s->name);
5776757768
for (i = 0; i < countof(js_dom_exception_names_table); i++) {
57768-
if (js_dom_exception_names_table[i].name &&
57769-
!strcmp(js_dom_exception_names_table[i].name, name)) {
57769+
it = js_dom_exception_names_table[i].name;
57770+
if (it && !strcmp(it, name) && len == strlen(it)) {
5777057771
s->code = i;
5777157772
break;
5777257773
}

tests/test_domexception.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { assert, assertThrows } from "./assert.js";
33
function test_code() {
44
let ex = new DOMException();
55
assert(ex.code, 0);
6+
ex = new DOMException("", "HierarchyRequestError\0test");
7+
assert(ex.code, 0);
68
ex = new DOMException("test", "HierarchyRequestError");
79
assert(ex.code, 3);
810
assert(ex.code, ex.HIERARCHY_REQUEST_ERR);

0 commit comments

Comments
 (0)