Skip to content

Commit

Permalink
src: use S_ISDIR to check if the file is a directory
Browse files Browse the repository at this point in the history
PR-URL: #52164
Fixes: #52159
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
theanarkh authored and marco-ippolito committed Jun 17, 2024
1 parent 0f78656 commit fad107d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
int rc = uv_fs_stat(env->event_loop(), &req, *path, nullptr);
if (rc == 0) {
const uv_stat_t* const s = static_cast<const uv_stat_t*>(req.ptr);
rc = !!(s->st_mode & S_IFDIR);
rc = S_ISDIR(s->st_mode);
}
uv_fs_req_cleanup(&req);

Expand Down Expand Up @@ -3079,7 +3079,7 @@ BindingData::FilePathIsFileReturnType BindingData::FilePathIsFile(

if (rc == 0) {
const uv_stat_t* const s = static_cast<const uv_stat_t*>(req.ptr);
rc = !!(s->st_mode & S_IFDIR);
rc = S_ISDIR(s->st_mode);
}

uv_fs_req_cleanup(&req);
Expand Down
2 changes: 1 addition & 1 deletion src/permission/fs_permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<const uv_stat_t*>(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 + "*";
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-fs-readdir-recursive.js
Original file line number Diff line number Diff line change
@@ -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();
}));

0 comments on commit fad107d

Please sign in to comment.