Skip to content

Commit 04d7c7a

Browse files
LiviaMedeirosruyadorno
authored andcommitted
fs: make mutating options in Callback readdir() not affect results
PR-URL: #56057 Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 92bcd52 commit 04d7c7a

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/fs.js

+3
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,9 @@ function readdir(path, options, callback) {
15331533
}
15341534

15351535
if (options.recursive) {
1536+
// Make shallow copy to prevent mutating options from affecting results
1537+
options = copyObject(options);
1538+
15361539
readdirRecursive(path, options, callback);
15371540
return;
15381541
}

test/parallel/test-fs-readdir-types.js

+8
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ fs.readdir(readdirDir, {
8686
assertDirents(await direntsPromise);
8787
})().then(common.mustCall());
8888

89+
{
90+
const options = { recursive: true, withFileTypes: true };
91+
fs.readdir(readdirDir, options, common.mustSucceed((dirents) => {
92+
assertDirents(dirents);
93+
}));
94+
options.withFileTypes = false;
95+
}
96+
8997
// Check for correct types when the binding returns unknowns
9098
const UNKNOWN = constants.UV_DIRENT_UNKNOWN;
9199
const oldReaddir = binding.readdir;

0 commit comments

Comments
 (0)