From 779031f02a59fb27f02c2b135c63cbec5309feb2 Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Fri, 29 Nov 2024 00:52:15 +0800 Subject: [PATCH 1/2] fs: make mutating `options` in Promises `readdir()` not affect results --- lib/internal/fs/promises.js | 4 ++++ test/parallel/test-fs-readdir-types.js | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 41be9a2a213b13..404f1f65edeb6c 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -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); diff --git a/test/parallel/test-fs-readdir-types.js b/test/parallel/test-fs-readdir-types.js index 3cc6b1cceff7fc..848d62399f0494 100644 --- a/test/parallel/test-fs-readdir-types.js +++ b/test/parallel/test-fs-readdir-types.js @@ -78,6 +78,14 @@ 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()); + // Check for correct types when the binding returns unknowns const UNKNOWN = constants.UV_DIRENT_UNKNOWN; const oldReaddir = binding.readdir; From aa7c1ef8f967d169232910c782220ff86cd5222d Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Sat, 7 Dec 2024 17:56:43 +0800 Subject: [PATCH 2/2] fs: make mutating `options` in Callback `readdir()` not affect results --- 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;