diff --git a/src/node_file.cc b/src/node_file.cc index 0ec5c6f4845e7b..b0aa53420c4efb 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1151,7 +1151,7 @@ static void InternalModuleStat(const FunctionCallbackInfo& args) { int rc = uv_fs_stat(env->event_loop(), &req, *path, nullptr); if (rc == 0) { const uv_stat_t* const s = static_cast(req.ptr); - rc = !!(s->st_mode & S_IFDIR); + rc = S_ISDIR(s->st_mode); } uv_fs_req_cleanup(&req); @@ -3079,7 +3079,7 @@ BindingData::FilePathIsFileReturnType BindingData::FilePathIsFile( if (rc == 0) { const uv_stat_t* const s = static_cast(req.ptr); - rc = !!(s->st_mode & S_IFDIR); + rc = S_ISDIR(s->st_mode); } uv_fs_req_cleanup(&req); diff --git a/src/permission/fs_permission.cc b/src/permission/fs_permission.cc index 0c881fa266d23d..745ddd96c089ed 100644 --- a/src/permission/fs_permission.cc +++ b/src/permission/fs_permission.cc @@ -20,7 +20,7 @@ std::string WildcardIfDir(const std::string& res) noexcept { int rc = uv_fs_stat(nullptr, &req, res.c_str(), nullptr); if (rc == 0) { const uv_stat_t* const s = static_cast(req.ptr); - if (s->st_mode & S_IFDIR) { + if ((s->st_mode & S_IFMT) == S_IFDIR) { // add wildcard when directory if (res.back() == node::kPathSeparator) { return res + "*"; diff --git a/test/parallel/test-fs-readdir-recursive.js b/test/parallel/test-fs-readdir-recursive.js new file mode 100644 index 00000000000000..f32e600d2a3660 --- /dev/null +++ b/test/parallel/test-fs-readdir-recursive.js @@ -0,0 +1,14 @@ +'use strict'; +const common = require('../common'); +const fs = require('fs'); +const net = require('net'); + +const tmpdir = require('../common/tmpdir'); +tmpdir.refresh(); + +const server = net.createServer().listen(common.PIPE, common.mustCall(() => { + // The process should not crash + // See https://github.com/nodejs/node/issues/52159 + fs.readdirSync(tmpdir.path, { recursive: true }); + server.close(); +}));