Skip to content

Commit 79b1954

Browse files
committed
fs: throw fchmodSync errors in JS
PR-URL: #19041 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent c6acfdb commit 79b1954

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

lib/fs.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,9 @@ fs.fchmodSync = function(fd, mode) {
10041004
validateUint32(mode, 'mode');
10051005
if (mode < 0 || mode > 0o777)
10061006
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');
1007-
return binding.fchmod(fd, mode);
1007+
const ctx = {};
1008+
binding.fchmod(fd, mode, undefined, ctx);
1009+
handleErrorFromBinding(ctx);
10081010
};
10091011

10101012
if (constants.O_SYMLINK !== undefined) {

src/node_file.cc

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,18 +1493,24 @@ static void Chmod(const FunctionCallbackInfo<Value>& args) {
14931493
static void FChmod(const FunctionCallbackInfo<Value>& args) {
14941494
Environment* env = Environment::GetCurrent(args);
14951495

1496+
const int argc = args.Length();
1497+
CHECK_GE(argc, 2);
1498+
14961499
CHECK(args[0]->IsInt32());
1497-
CHECK(args[1]->IsInt32());
1500+
const int fd = args[0].As<Int32>()->Value();
14981501

1499-
int fd = args[0]->Int32Value();
1500-
int mode = static_cast<int>(args[1]->Int32Value());
1502+
CHECK(args[1]->IsInt32());
1503+
const int mode = args[1].As<Int32>()->Value();
15011504

15021505
FSReqBase* req_wrap = GetReqWrap(env, args[2]);
1503-
if (req_wrap != nullptr) {
1506+
if (req_wrap != nullptr) { // fchmod(fd, mode, req)
15041507
AsyncCall(env, req_wrap, args, "fchmod", UTF8, AfterNoArgs,
15051508
uv_fs_fchmod, fd, mode);
1506-
} else {
1507-
SYNC_CALL(fchmod, 0, fd, mode);
1509+
} else { // fchmod(fd, mode, undefined, ctx)
1510+
CHECK_EQ(argc, 4);
1511+
fs_req_wrap req_wrap;
1512+
SyncCall(env, args[3], &req_wrap, "fchmod",
1513+
uv_fs_fchmod, fd, mode);
15081514
}
15091515
}
15101516

test/parallel/test-fs-error-messages.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,3 +729,23 @@ if (!common.isAIX) {
729729
);
730730
});
731731
}
732+
733+
// fchmod
734+
{
735+
const validateError = (err) => {
736+
assert.strictEqual(err.message, 'EBADF: bad file descriptor, fchmod');
737+
assert.strictEqual(err.errno, uv.UV_EBADF);
738+
assert.strictEqual(err.code, 'EBADF');
739+
assert.strictEqual(err.syscall, 'fchmod');
740+
return true;
741+
};
742+
743+
common.runWithInvalidFD((fd) => {
744+
fs.fchmod(fd, 0o666, common.mustCall(validateError));
745+
746+
assert.throws(
747+
() => fs.fchmodSync(fd, 0o666),
748+
validateError
749+
);
750+
});
751+
}

0 commit comments

Comments
 (0)