fs: make mutating `options` in Callback `readdir()` not affect results

PR-URL: https://github.com/nodejs/node/pull/56057
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
pull/56243/head
LiviaMedeiros 2024-12-07 17:56:43 +08:00 committed by Node.js GitHub Bot
parent b5e25aa2ce
commit 91099658a6
2 changed files with 11 additions and 0 deletions

View File

@ -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;
}

View File

@ -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;