fs: fix recursive readdir with buffer encoding - #64954
Open
canblmz1 wants to merge 1 commit into
Open
Conversation
`fs.readdir()`, `fs.readdirSync()`, and `fs.promises.readdir()` threw
ERR_INVALID_ARG_TYPE (or crashed the process outright in the callback
case) when called with both `{ recursive: true }` and
`{ encoding: 'buffer' }`, because the internal recursive walk used
`path.join()`, `path.relative()`, and a CommonJS-module-resolution
specific native stat binding, none of which accept Buffer arguments.
Adds `relativeToBasePath()` and `isDirectoryPath()` helpers to
`internal/fs/utils`, alongside the existing (but previously
unexported) `join()` helper, all of which handle both string and
Buffer paths. The recursive readdir implementations in `lib/fs.js`
and `lib/internal/fs/promises.js` now use these instead of calling
`path`/the module-resolution stat binding directly.
`isDirectoryPath()` falls back to the general-purpose `stat` binding
(the same one `fs.statSync()` uses) for Buffer paths, so it also
handles non-UTF8 file names correctly instead of round-tripping
through a lossy string conversion.
Fixes: nodejs#58892
Signed-off-by: Can <hello@syntaxandco.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
fs.readdir(),fs.readdirSync(), andfs.promises.readdir()throwERR_INVALID_ARG_TYPE(or crash the process outright in the callbackcase) when called with both
{ recursive: true }and{ encoding: 'buffer' }. The internal recursive walk builds paths withpath.join()/path.relative(), and checks whether an entry is adirectory via a CommonJS-module-resolution-specific native stat
binding (
internalModuleStat) - none of these acceptBufferarguments, which is what entry names become once
encoding: 'buffer'is set.
Fix
internal/fs/utilsalready had an unexportedjoin()helper thatcorrectly handles string/Buffer combinations; this exports it and
adds two more helpers alongside it:
relativeToBasePath()(avoidsneeding
path.relative()'s Buffer support, since the full path isalways built by repeatedly joining onto the base path) and
isDirectoryPath().isDirectoryPath()keeps usinginternalModuleStatfor the stringfast path (unchanged behavior/performance for the common case), but
falls back to the general-purpose
statbinding - the same onefs.statSync()uses - for Buffer paths. That binding handlesBuffers correctly at the native layer, so this also avoids a lossy
string round-trip for non-UTF8 file names, rather than just papering
over the reported crash.
lib/fs.js(handleDirents/handleFilePaths) andlib/internal/fs/promises.js(readdirRecursive) both had theirown copy of this bug and are updated to use the shared helpers.
Testing
Added
test/parallel/test-fs-readdir-recursive-buffer.js, coveringreaddirSync,readdirSyncwithwithFileTypes, thereaddircallback form, and
fs.promises.readdir, all with{ recursive: true, encoding: 'buffer' }.Ran the full
test/parallel/test-fs-readdir*.jssuite plus a broadertest/parallel/test-fs-*.jssweep (257 files) locally on Windows(clang-cl build) - all pass except 7 pre-existing failures unrelated
to this change (
EPERMonsymlinkSyncdue to this environment notrunning elevated/without Developer Mode, verified individually).
Fixes: #58892