From 2df8257c257f4ed051a234bed1942d0bb119b372 Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Fri, 29 Nov 2024 00:52:15 +0800 Subject: [PATCH] fs: make mutating `options` in Promises `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/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;