Skip to content

Commit 764386f

Browse files
author
pluris
committed
src: add getValidateFd() to node_file
1 parent c710f94 commit 764386f

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

lib/internal/fs/sync.js

-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ function close(fd) {
8989
}
9090

9191
function fsync(fd) {
92-
fd = getValidatedFd(fd);
93-
9492
return binding.fsyncSync(fd);
9593
}
9694

src/node_file.cc

+22-4
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,26 @@ inline int64_t GetOffset(Local<Value> value) {
116116
return IsSafeJsInt(value) ? value.As<Integer>()->Value() : -1;
117117
}
118118

119+
inline int GetValidatedFd(Environment* env, Local<Value> value) {
120+
if (!value->IsInt32()) {
121+
env->isolate()->ThrowException(ERR_INVALID_ARG_TYPE(
122+
env->isolate(),
123+
"Invalid argument. The fd must be int32."));
124+
return 1 << 30;
125+
}
126+
127+
const int fd = value.As<Int32>()->Value();
128+
129+
if (fd < 0 || fd > INT32_MAX) {
130+
env->isolate()->ThrowException(ERR_OUT_OF_RANGE(
131+
env->isolate(),
132+
"It must be >= 0 && <= INT32_MAX. Received %d", fd));
133+
return 1 << 30;
134+
}
135+
136+
return fd;
137+
}
138+
119139
static const char* get_fs_func_name_by_type(uv_fs_type req_type) {
120140
switch (req_type) {
121141
#define FS_TYPE_TO_NAME(type, name) \
@@ -1647,10 +1667,8 @@ static void FsyncSync(const FunctionCallbackInfo<Value>& args) {
16471667
const int argc = args.Length();
16481668
CHECK_GE(argc, 1);
16491669

1650-
CHECK(args[0]->IsInt32());
1651-
1652-
const int fd = args[0].As<Int32>()->Value();
1653-
CHECK_GE(fd, 0);
1670+
const int fd = GetValidatedFd(env, args[0]);
1671+
if (fd == (1 << 30)) return;
16541672

16551673
uv_fs_t req;
16561674
FS_SYNC_TRACE_BEGIN(fsync);

0 commit comments

Comments
 (0)