]> git.kaiwu.me - njs.git/commitdiff
FS: fix option value leaks in QuickJS
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 2 Dec 2026 01:43:23 +0000 (17:43 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Thu, 3 Dec 2026 01:27:35 +0000 (17:27 -0800)
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).

external/qjs_fs_module.c
test/fs/readdir.t.mjs

index bd19f8c2bb5aa0e15939a95a579b41ad7b72a1ad..ab3376c7e0d9bc00e34f0cda77f6cca3f611b158 100644 (file)
@@ -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;
             }
         }
index 2aae3c4ae36d794df9568e82edad5aaacf6c8a4a..16c64038af1c3f688c2c58aecd84599d1b9235c8 100644 (file)
@@ -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');