Skip to content

Commit

Permalink
src: add getValidateFd() to node_file
Browse files Browse the repository at this point in the history
  • Loading branch information
pluris committed Sep 27, 2023
1 parent c710f94 commit 764386f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
2 changes: 0 additions & 2 deletions lib/internal/fs/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ function close(fd) {
}

function fsync(fd) {
fd = getValidatedFd(fd);

return binding.fsyncSync(fd);
}

Expand Down
26 changes: 22 additions & 4 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,26 @@ inline int64_t GetOffset(Local<Value> value) {
return IsSafeJsInt(value) ? value.As<Integer>()->Value() : -1;
}

inline int GetValidatedFd(Environment* env, Local<Value> value) {
if (!value->IsInt32()) {
env->isolate()->ThrowException(ERR_INVALID_ARG_TYPE(
env->isolate(),
"Invalid argument. The fd must be int32."));
return 1 << 30;
}

const int fd = value.As<Int32>()->Value();

if (fd < 0 || fd > INT32_MAX) {
env->isolate()->ThrowException(ERR_OUT_OF_RANGE(
env->isolate(),
"It must be >= 0 && <= INT32_MAX. Received %d", fd));
return 1 << 30;
}

return fd;
}

static const char* get_fs_func_name_by_type(uv_fs_type req_type) {
switch (req_type) {
#define FS_TYPE_TO_NAME(type, name) \
Expand Down Expand Up @@ -1647,10 +1667,8 @@ static void FsyncSync(const FunctionCallbackInfo<Value>& args) {
const int argc = args.Length();
CHECK_GE(argc, 1);

CHECK(args[0]->IsInt32());

const int fd = args[0].As<Int32>()->Value();
CHECK_GE(fd, 0);
const int fd = GetValidatedFd(env, args[0]);
if (fd == (1 << 30)) return;

uv_fs_t req;
FS_SYNC_TRACE_BEGIN(fsync);
Expand Down

0 comments on commit 764386f

Please sign in to comment.