From: Dmitry Volyntsev Date: Wed, 2 Dec 2026 01:43:23 +0000 (-0800) Subject: FS: fix option value leaks in QuickJS X-Git-Url: https://git.kaiwu.me/web/http/static/%7B@url%7D?a=commitdiff_plain;h=e0aa8931301bf077bb623baf97f161587947c776;p=njs.git FS: fix option value leaks in QuickJS Previously, the values of the flag, mode, recursive, withFileTypes, bigint and throwIfNoEntry options obtained with JS_GetPropertyStr() were not freed after conversion. Reference-counted values, such as objects and non-atom strings, leaked; an object leak also made QuickJS assert on runtime destruction. The issue was introduced in 1f8f9992 (0.8.9). --- diff --git a/external/qjs_fs_module.c b/external/qjs_fs_module.c index bd19f8c2..ab3376c7 100644 --- a/external/qjs_fs_module.c +++ b/external/qjs_fs_module.c @@ -613,8 +613,8 @@ qjs_fs_mkdir(JSContext *cx, JSValueConst this_val, int argc, v = JS_GetPropertyStr(cx, options, "mode"); if (!JS_IsUndefined(v) && !JS_IsException(v)) { md = qjs_fs_mode(cx, v, 0777); + JS_FreeValue(cx, v); if (md == (mode_t) -1) { - JS_FreeValue(cx, v); return JS_EXCEPTION; } } @@ -622,6 +622,7 @@ qjs_fs_mkdir(JSContext *cx, JSValueConst this_val, int argc, v = JS_GetPropertyStr(cx, options, "recursive"); if (!JS_IsUndefined(v) && !JS_IsException(v)) { recursive = JS_ToBool(cx, v); + JS_FreeValue(cx, v); } } @@ -918,8 +919,8 @@ qjs_fs_read_file(JSContext *cx, JSValueConst this_val, int argc, v = JS_GetPropertyStr(cx, options, "flag"); if (!JS_IsUndefined(v) && !JS_IsException(v)) { flags = qjs_fs_flags(cx, v, O_RDONLY); + JS_FreeValue(cx, v); if (flags == -1) { - JS_FreeValue(cx, v); return JS_EXCEPTION; } } @@ -1180,6 +1181,7 @@ qjs_fs_readdir(JSContext *cx, JSValueConst this_val, int argc, v = JS_GetPropertyStr(cx, options, "withFileTypes"); if (!JS_IsUndefined(v) && !JS_IsException(v)) { types = JS_ToBool(cx, v); + JS_FreeValue(cx, v); } } @@ -1714,6 +1716,7 @@ qjs_fs_rmdir(JSContext *cx, JSValueConst this_val, int argc, v = JS_GetPropertyStr(cx, options, "recursive"); if (!JS_IsUndefined(v) && !JS_IsException(v)) { recursive = JS_ToBool(cx, v); + JS_FreeValue(cx, v); } } @@ -1877,6 +1880,7 @@ qjs_fs_stat(JSContext *cx, JSValueConst this_val, int argc, JSValueConst *argv, value = JS_GetPropertyStr(cx, options, "bigint"); if (!JS_IsUndefined(value)) { + JS_FreeValue(cx, value); JS_ThrowTypeError(cx, "\"bigint\" is not supported"); return JS_EXCEPTION; } @@ -1885,6 +1889,7 @@ qjs_fs_stat(JSContext *cx, JSValueConst this_val, int argc, JSValueConst *argv, value = JS_GetPropertyStr(cx, options, "throwIfNoEntry"); if (!JS_IsUndefined(value)) { throw = JS_ToBool(cx, value); + JS_FreeValue(cx, value); } } } @@ -2230,8 +2235,8 @@ qjs_fs_write_file(JSContext *cx, JSValueConst this_val, int argc, v = JS_GetPropertyStr(cx, options, "flag"); if (!JS_IsUndefined(v) && !JS_IsException(v)) { flags = qjs_fs_flags(cx, v, O_CREAT | O_WRONLY); + JS_FreeValue(cx, v); if (flags == -1) { - JS_FreeValue(cx, v); return JS_EXCEPTION; } } @@ -2239,8 +2244,8 @@ qjs_fs_write_file(JSContext *cx, JSValueConst this_val, int argc, v = JS_GetPropertyStr(cx, options, "mode"); if (!JS_IsUndefined(v) && !JS_IsException(v)) { md = qjs_fs_mode(cx, v, 0666); + JS_FreeValue(cx, v); if (md == (mode_t) -1) { - JS_FreeValue(cx, v); return JS_EXCEPTION; } } diff --git a/test/fs/readdir.t.mjs b/test/fs/readdir.t.mjs index 2aae3c4a..16c64038 100644 --- a/test/fs/readdir.t.mjs +++ b/test/fs/readdir.t.mjs @@ -84,7 +84,7 @@ var testSync = () => new Promise((resolve, reject) => { throw new Error('fs.readdirSync - error 3'); } - var dir = fs.readdirSync(dname, { withFileTypes: true }); + var dir = fs.readdirSync(dname, { withFileTypes: {} }); var dir_utf8 = fs.readdirSync(dname_utf8, { withFileTypes: true }); if (dir.length != dir_utf8.length || dir.length != 3) { throw new Error('fs.readdirSync - error 4');