Skip to content

Commit 4f02ab8

Browse files
saghuloleavr
andauthored
Fix use-after-free on error during module evaluation
E.g. if during evaluation of module A, we start loading module B and an error occurs. This results in a call to js_free_modules() with JS_FREE_MODULE_NOT_EVALUATED, and since module A isn't yet evaluated, it gets freed prematurely. To solve this we improve js_free_modules() to ensure `eval_mark` is not set. Once js_evaluate_module() returns for module A, it will notice that an exception occurred and call js_free_modules() with JS_FREE_MODULE_NOT_EVALUATED. Since `eval_mark` has been cleared by then, module A gets cleaned up as well. Co-authored-by: Ole André Vadla Ravnås <[email protected]>
1 parent a3a57fe commit 4f02ab8

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

quickjs.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2225,7 +2225,8 @@ static void js_free_modules(JSContext *ctx, JSFreeModuleEnum flag)
22252225
JSModuleDef *m = list_entry(el, JSModuleDef, link);
22262226
if (flag == JS_FREE_MODULE_ALL ||
22272227
(flag == JS_FREE_MODULE_NOT_RESOLVED && !m->resolved) ||
2228-
(flag == JS_FREE_MODULE_NOT_EVALUATED && !m->evaluated)) {
2228+
(flag == JS_FREE_MODULE_NOT_EVALUATED && !m->evaluated
2229+
&& !m->eval_mark)) {
22292230
js_free_module_def(ctx, m);
22302231
}
22312232
}
@@ -28009,6 +28010,7 @@ static JSValue js_evaluate_module(JSContext *ctx, JSModuleDef *m)
2800928010
ret_val = js_evaluate_module(ctx, m1);
2801028011
if (JS_IsException(ret_val)) {
2801128012
m->eval_mark = FALSE;
28013+
js_free_modules(ctx, JS_FREE_MODULE_NOT_EVALUATED);
2801228014
goto clean;
2801328015
}
2801428016
if (!JS_IsUndefined(ret_val)) {

0 commit comments

Comments
 (0)