Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: make mutating options in readdir() not affect results #56057

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,10 @@ async function readdirRecursive(originalPath, options) {

async function readdir(path, options) {
options = getOptions(options);

// Make shallow copy to prevent mutating options from affecting results
options = copyObject(options);

path = getValidatedPath(path);
if (options.recursive) {
return readdirRecursive(path, options);
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-fs-readdir-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ fs.readdir(readdirDir, {
assertDirents(dirents);
})().then(common.mustCall());

// Check that mutating options doesn't affect results
(async () => {
const options = { withFileTypes: true };
const direntsPromise = fs.promises.readdir(readdirDir, options);
options.withFileTypes = false;
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;
Expand Down
Loading