Skip to content

Commit ed908e6

Browse files
author
pluris
committed
src: add GetValidatedFd() in node_file
1 parent 783f64b commit ed908e6

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/node_file.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,24 @@ 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(), "Invalid argument. The fd must be int32."));
123+
return 1 << 30;
124+
}
125+
126+
const int fd = value.As<Int32>()->Value();
127+
128+
if (fd < 0 || fd > INT32_MAX) {
129+
env->isolate()->ThrowException(ERR_OUT_OF_RANGE(
130+
env->isolate(), "It must be >= 0 && <= INT32_MAX. Received %d", fd));
131+
return 1 << 30;
132+
}
133+
134+
return fd;
135+
}
136+
119137
static const char* get_fs_func_name_by_type(uv_fs_type req_type) {
120138
switch (req_type) {
121139
#define FS_TYPE_TO_NAME(type, name) \

0 commit comments

Comments
 (0)