From 0bab5d840252475ab1eac513356e1e553ac812e7 Mon Sep 17 00:00:00 2001 From: Matthew Douglass <5410142+mdouglass@users.noreply.github.com> Date: Tue, 18 Jul 2023 09:08:00 -0700 Subject: [PATCH 1/2] fs: read all results for subdirectories in opendir Fixes #48820 --- lib/internal/fs/dir.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/internal/fs/dir.js b/lib/internal/fs/dir.js index ec0562843d5f5c..03de09e379ca71 100644 --- a/lib/internal/fs/dir.js +++ b/lib/internal/fs/dir.js @@ -166,18 +166,22 @@ class Dir { pathModule.toNamespacedPath(dirent.path), this[kDirOptions].encoding, undefined, - ctx, + ctx ); handleErrorFromBinding(ctx); - const result = handle.read( - this[kDirOptions].encoding, - this[kDirOptions].bufferSize, - undefined, - ctx, - ); + while (true) { + const result = handle.read( + this[kDirOptions].encoding, + this[kDirOptions].bufferSize, + undefined, + ctx, + ); - if (result) { - this.processReadResult(dirent.path, result); + if (result) { + this.processReadResult(dirent.path, result); + } else { + break; + } } handle.close(undefined, ctx); @@ -206,7 +210,7 @@ class Dir { this[kDirOptions].encoding, this[kDirOptions].bufferSize, undefined, - ctx, + ctx ); handleErrorFromBinding(ctx); From 860657ddee28da01f9bd633349320a78043b64d3 Mon Sep 17 00:00:00 2001 From: Matthew Douglass <5410142+mdouglass@users.noreply.github.com> Date: Tue, 18 Jul 2023 11:51:12 -0700 Subject: [PATCH 2/2] update test to use artificially low bufferSize to demonstrate bug --- test/sequential/test-fs-opendir-recursive.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/sequential/test-fs-opendir-recursive.js b/test/sequential/test-fs-opendir-recursive.js index 935be957e903c2..58c42f371647cd 100644 --- a/test/sequential/test-fs-opendir-recursive.js +++ b/test/sequential/test-fs-opendir-recursive.js @@ -152,7 +152,7 @@ function processDirSync(dir) { // Opendir read results sync { - const dir = fs.opendirSync(testDir, { recursive: true }); + const dir = fs.opendirSync(testDir, { recursive: true, bufferSize: 1 }); processDirSync(dir); dir.closeSync(); }