3838#include " req_wrap-inl.h"
3939#include " stream_base-inl.h"
4040#include " string_bytes.h"
41+ #include " util.h"
4142
4243#include < fcntl.h>
4344#include < sys/types.h>
@@ -1405,13 +1406,14 @@ static void FTruncate(const FunctionCallbackInfo<Value>& args) {
14051406 const int argc = args.Length ();
14061407 CHECK_GE (argc, 2 );
14071408
1408- CHECK (args[0 ]->IsInt32 ());
1409+ const bool is_async_call = argc > 2 ;
1410+ VALIDATE_FD (env, args[0 ], is_async_call);
14091411 const int fd = args[0 ].As <Int32>()->Value ();
14101412
14111413 CHECK (IsSafeJsInt (args[1 ]));
14121414 const int64_t len = args[1 ].As <Integer>()->Value ();
14131415
1414- if (argc > 2 ) {
1416+ if (is_async_call ) {
14151417 FSReqBase* req_wrap_async = GetReqWrap (args, 2 );
14161418 FS_ASYNC_TRACE_BEGIN0 (UV_FS_FTRUNCATE, req_wrap_async)
14171419 AsyncCall (env, req_wrap_async, args, " ftruncate" , UTF8, AfterNoArgs,
@@ -1430,10 +1432,11 @@ static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
14301432 const int argc = args.Length ();
14311433 CHECK_GE (argc, 1 );
14321434
1433- CHECK (args[0 ]->IsInt32 ());
1435+ const bool is_async_call = argc > 1 ;
1436+ VALIDATE_FD (env, args[0 ], is_async_call);
14341437 const int fd = args[0 ].As <Int32>()->Value ();
14351438
1436- if (argc > 1 ) { // fdatasync(fd, req)
1439+ if (is_async_call ) { // fdatasync(fd, req)
14371440 FSReqBase* req_wrap_async = GetReqWrap (args, 1 );
14381441 CHECK_NOT_NULL (req_wrap_async);
14391442 FS_ASYNC_TRACE_BEGIN0 (UV_FS_FDATASYNC, req_wrap_async)
@@ -1453,10 +1456,11 @@ static void Fsync(const FunctionCallbackInfo<Value>& args) {
14531456 const int argc = args.Length ();
14541457 CHECK_GE (argc, 1 );
14551458
1456- CHECK (args[0 ]->IsInt32 ());
1459+ const bool is_async_call = argc > 1 ;
1460+ VALIDATE_FD (env, args[0 ], is_async_call);
14571461 const int fd = args[0 ].As <Int32>()->Value ();
14581462
1459- if (argc > 1 ) {
1463+ if (is_async_call ) {
14601464 FSReqBase* req_wrap_async = GetReqWrap (args, 1 );
14611465 CHECK_NOT_NULL (req_wrap_async);
14621466 FS_ASYNC_TRACE_BEGIN0 (UV_FS_FSYNC, req_wrap_async)
@@ -2033,7 +2037,8 @@ static void WriteBuffer(const FunctionCallbackInfo<Value>& args) {
20332037 const int argc = args.Length ();
20342038 CHECK_GE (argc, 4 );
20352039
2036- CHECK (args[0 ]->IsInt32 ());
2040+ const bool is_async_call = !args[5 ]->IsUndefined ();
2041+ VALIDATE_FD (env, args[0 ], is_async_call);
20372042 const int fd = args[0 ].As <Int32>()->Value ();
20382043
20392044 CHECK (Buffer::HasInstance (args[1 ]));
@@ -2088,7 +2093,8 @@ static void WriteBuffers(const FunctionCallbackInfo<Value>& args) {
20882093 const int argc = args.Length ();
20892094 CHECK_GE (argc, 3 );
20902095
2091- CHECK (args[0 ]->IsInt32 ());
2096+ const bool is_async_call = argc > 3 ;
2097+ VALIDATE_FD (env, args[0 ], is_async_call);
20922098 const int fd = args[0 ].As <Int32>()->Value ();
20932099
20942100 CHECK (args[1 ]->IsArray ());
@@ -2104,7 +2110,7 @@ static void WriteBuffers(const FunctionCallbackInfo<Value>& args) {
21042110 iovs[i] = uv_buf_init (Buffer::Data (chunk), Buffer::Length (chunk));
21052111 }
21062112
2107- if (argc > 3 ) { // writeBuffers(fd, chunks, pos, req)
2113+ if (is_async_call ) { // writeBuffers(fd, chunks, pos, req)
21082114 FSReqBase* req_wrap_async = GetReqWrap (args, 3 );
21092115 FS_ASYNC_TRACE_BEGIN0 (UV_FS_WRITE, req_wrap_async)
21102116 AsyncCall (env,
@@ -2146,7 +2152,9 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
21462152
21472153 const int argc = args.Length ();
21482154 CHECK_GE (argc, 4 );
2149- CHECK (args[0 ]->IsInt32 ());
2155+
2156+ const bool is_async_call = !args[4 ]->IsUndefined ();
2157+ VALIDATE_FD (env, args[0 ], is_async_call);
21502158 const int fd = args[0 ].As <Int32>()->Value ();
21512159
21522160 const int64_t pos = GetOffset (args[2 ]);
@@ -2326,7 +2334,8 @@ static void Read(const FunctionCallbackInfo<Value>& args) {
23262334 const int argc = args.Length ();
23272335 CHECK_GE (argc, 5 );
23282336
2329- CHECK (args[0 ]->IsInt32 ());
2337+ const bool is_async_call = argc > 5 ;
2338+ VALIDATE_FD (env, args[0 ], is_async_call);
23302339 const int fd = args[0 ].As <Int32>()->Value ();
23312340
23322341 CHECK (Buffer::HasInstance (args[1 ]));
@@ -2352,7 +2361,7 @@ static void Read(const FunctionCallbackInfo<Value>& args) {
23522361 char * buf = buffer_data + off;
23532362 uv_buf_t uvbuf = uv_buf_init (buf, len);
23542363
2355- if (argc > 5 ) { // read(fd, buffer, offset, len, pos, req)
2364+ if (is_async_call ) { // read(fd, buffer, offset, len, pos, req)
23562365 FSReqBase* req_wrap_async = GetReqWrap (args, 5 );
23572366 CHECK_NOT_NULL (req_wrap_async);
23582367 FS_ASYNC_TRACE_BEGIN0 (UV_FS_READ, req_wrap_async)
@@ -2450,7 +2459,8 @@ static void ReadBuffers(const FunctionCallbackInfo<Value>& args) {
24502459 const int argc = args.Length ();
24512460 CHECK_GE (argc, 3 );
24522461
2453- CHECK (args[0 ]->IsInt32 ());
2462+ const bool is_async_call = !args[3 ]->IsUndefined ();
2463+ VALIDATE_FD (env, args[0 ], is_async_call);
24542464 const int fd = args[0 ].As <Int32>()->Value ();
24552465
24562466 CHECK (args[1 ]->IsArray ());
@@ -2467,8 +2477,8 @@ static void ReadBuffers(const FunctionCallbackInfo<Value>& args) {
24672477 iovs[i] = uv_buf_init (Buffer::Data (buffer), Buffer::Length (buffer));
24682478 }
24692479
2470- FSReqBase* req_wrap_async = GetReqWrap (args, 3 );
2471- if ( req_wrap_async != nullptr ) { // readBuffers(fd, buffers, pos, req)
2480+ if (is_async_call) { // readBuffers(fd, buffers, pos, req)
2481+ FSReqBase* req_wrap_async = GetReqWrap (args, 3 );
24722482 FS_ASYNC_TRACE_BEGIN0 (UV_FS_READ, req_wrap_async)
24732483 AsyncCall (env, req_wrap_async, args, " read" , UTF8, AfterInteger,
24742484 uv_fs_read, fd, *iovs, iovs.length (), pos);
@@ -2525,13 +2535,14 @@ static void FChmod(const FunctionCallbackInfo<Value>& args) {
25252535 const int argc = args.Length ();
25262536 CHECK_GE (argc, 2 );
25272537
2528- CHECK (args[0 ]->IsInt32 ());
2538+ const bool is_async_call = argc > 2 ;
2539+ VALIDATE_FD (env, args[0 ], is_async_call);
25292540 const int fd = args[0 ].As <Int32>()->Value ();
25302541
25312542 CHECK (args[1 ]->IsInt32 ());
25322543 const int mode = args[1 ].As <Int32>()->Value ();
25332544
2534- if (argc > 2 ) { // fchmod(fd, mode, req)
2545+ if (is_async_call ) { // fchmod(fd, mode, req)
25352546 FSReqBase* req_wrap_async = GetReqWrap (args, 2 );
25362547 FS_ASYNC_TRACE_BEGIN0 (UV_FS_FCHMOD, req_wrap_async)
25372548 AsyncCall (env, req_wrap_async, args, " fchmod" , UTF8, AfterNoArgs,
@@ -2588,7 +2599,8 @@ static void FChown(const FunctionCallbackInfo<Value>& args) {
25882599 const int argc = args.Length ();
25892600 CHECK_GE (argc, 3 );
25902601
2591- CHECK (args[0 ]->IsInt32 ());
2602+ const bool is_async_call = !args[3 ]->IsUndefined ();
2603+ VALIDATE_FD (env, args[0 ], is_async_call);
25922604 const int fd = args[0 ].As <Int32>()->Value ();
25932605
25942606 CHECK (IsSafeJsInt (args[1 ]));
@@ -2597,8 +2609,8 @@ static void FChown(const FunctionCallbackInfo<Value>& args) {
25972609 CHECK (IsSafeJsInt (args[2 ]));
25982610 const uv_gid_t gid = static_cast <uv_gid_t >(args[2 ].As <Integer>()->Value ());
25992611
2600- FSReqBase* req_wrap_async = GetReqWrap (args, 3 );
2601- if ( req_wrap_async != nullptr ) { // fchown(fd, uid, gid, req)
2612+ if (is_async_call) { // fchown(fd, uid, gid, req)
2613+ FSReqBase* req_wrap_async = GetReqWrap (args, 3 );
26022614 FS_ASYNC_TRACE_BEGIN0 (UV_FS_FCHOWN, req_wrap_async)
26032615 AsyncCall (env, req_wrap_async, args, " fchown" , UTF8, AfterNoArgs,
26042616 uv_fs_fchown, fd, uid, gid);
@@ -2683,7 +2695,8 @@ static void FUTimes(const FunctionCallbackInfo<Value>& args) {
26832695 const int argc = args.Length ();
26842696 CHECK_GE (argc, 3 );
26852697
2686- CHECK (args[0 ]->IsInt32 ());
2698+ const bool is_async_call = argc > 3 ;
2699+ VALIDATE_FD (env, args[0 ], is_async_call);
26872700 const int fd = args[0 ].As <Int32>()->Value ();
26882701
26892702 CHECK (args[1 ]->IsNumber ());
@@ -2692,7 +2705,7 @@ static void FUTimes(const FunctionCallbackInfo<Value>& args) {
26922705 CHECK (args[2 ]->IsNumber ());
26932706 const double mtime = args[2 ].As <Number>()->Value ();
26942707
2695- if (argc > 3 ) { // futimes(fd, atime, mtime, req)
2708+ if (is_async_call ) { // futimes(fd, atime, mtime, req)
26962709 FSReqBase* req_wrap_async = GetReqWrap (args, 3 );
26972710 FS_ASYNC_TRACE_BEGIN0 (UV_FS_FUTIME, req_wrap_async)
26982711 AsyncCall (env, req_wrap_async, args, " futime" , UTF8, AfterNoArgs,
0 commit comments