From 36227a5310f3a58c6be1fb1bfd1f833983c29d71 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 16 Oct 2024 10:13:38 +0200 Subject: [PATCH] Fix cyclic import/export segfault (#568) Before this commit it segfaulted, now it throws a SyntaxError. That's still not correct behavior but better than segfaulting. To be continued. Includes a small run-test262 fix to handle Windows line endings. Refs: https://github.com/quickjs-ng/quickjs/issues/567 --- quickjs.c | 5 +++++ run-test262.c | 2 +- tests.conf | 1 + tests/fixture_cyclic_import.js | 2 ++ tests/test_cyclic_import.js | 12 ++++++++++++ 5 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/fixture_cyclic_import.js create mode 100644 tests/test_cyclic_import.js diff --git a/quickjs.c b/quickjs.c index 7d80009..401c48a 100644 --- a/quickjs.c +++ b/quickjs.c @@ -26774,6 +26774,11 @@ static JSValue js_build_module_ns(JSContext *ctx, JSModuleDef *m) case EXPORTED_NAME_NORMAL: { JSVarRef *var_ref = en->u.var_ref; + if (!var_ref) { + js_resolve_export_throw_error(ctx, JS_RESOLVE_RES_CIRCULAR, + m, en->export_name); + goto fail; + } pr = add_property(ctx, p, en->export_name, JS_PROP_ENUMERABLE | JS_PROP_WRITABLE | JS_PROP_VARREF); diff --git a/run-test262.c b/run-test262.c index e9ff83b..58f3fb8 100644 --- a/run-test262.c +++ b/run-test262.c @@ -1836,7 +1836,7 @@ int run_test(const char *filename, int *msec) if (q) { while (isspace((unsigned char)*q)) q++; - error_type = strdup_len(q, strcspn(q, " \n")); + error_type = strdup_len(q, strcspn(q, " \r\n")); } is_negative = TRUE; } diff --git a/tests.conf b/tests.conf index 30ee794..0a3808e 100644 --- a/tests.conf +++ b/tests.conf @@ -4,5 +4,6 @@ verbose=yes testdir=tests [exclude] +tests/fixture_cyclic_import.js tests/microbench.js tests/test_worker_module.js diff --git a/tests/fixture_cyclic_import.js b/tests/fixture_cyclic_import.js new file mode 100644 index 0000000..bac80d8 --- /dev/null +++ b/tests/fixture_cyclic_import.js @@ -0,0 +1,2 @@ +import * as a from "./test_cyclic_import.js" +export function f(x) { return 2 * a.g(x) } diff --git a/tests/test_cyclic_import.js b/tests/test_cyclic_import.js new file mode 100644 index 0000000..bf51d9b --- /dev/null +++ b/tests/test_cyclic_import.js @@ -0,0 +1,12 @@ +/*--- +negative: + phase: resolution + type: SyntaxError +---*/ +// FIXME(bnoordhuis) shouldn't throw SyntaxError but that's still better +// than segfaulting, see https://github.com/quickjs-ng/quickjs/issues/567 +import {assert} from "./assert.js" +import {f} from "./fixture_cyclic_import.js" +export {f} +export function g(x) { return x + 1 } +assert(f(1), 4)