From 91099658a618732b2cf4a0bec334df26a9ebaaf4 Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Sat, 7 Dec 2024 17:56:43 +0800 Subject: [PATCH] fs: make mutating `options` in Callback `readdir()` not affect results MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/56057 Reviewed-By: Rafael Gonzaga Reviewed-By: Luigi Pinca Reviewed-By: Ruben Bridgewater Reviewed-By: Juan José Arboleda Reviewed-By: James M Snell --- lib/fs.js | 3 +++ test/parallel/test-fs-readdir-types.js | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/fs.js b/lib/fs.js index 9d941dd65ab613..af72ac36144c55 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1531,6 +1531,9 @@ function readdir(path, options, callback) { } if (options.recursive) { + // Make shallow copy to prevent mutating options from affecting results + options = copyObject(options); + readdirRecursive(path, options, callback); return; } diff --git a/test/parallel/test-fs-readdir-types.js b/test/parallel/test-fs-readdir-types.js index 848d62399f0494..c6225c919e4a22 100644 --- a/test/parallel/test-fs-readdir-types.js +++ b/test/parallel/test-fs-readdir-types.js @@ -86,6 +86,14 @@ fs.readdir(readdirDir, { assertDirents(await direntsPromise); })().then(common.mustCall()); +{ + const options = { recursive: true, withFileTypes: true }; + fs.readdir(readdirDir, options, common.mustSucceed((dirents) => { + assertDirents(dirents); + })); + options.withFileTypes = false; +} + // Check for correct types when the binding returns unknowns const UNKNOWN = constants.UV_DIRENT_UNKNOWN; const oldReaddir = binding.readdir;