@@ -538,6 +538,7 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
538538
539539 std::vector<char > chars;
540540 int64_t offset = 0 ;
541+ ssize_t numchars;
541542 for (;;) {
542543 const size_t kBlockSize = 32 << 10 ;
543544 const size_t start = chars.size ();
@@ -548,11 +549,13 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
548549 buf.len = kBlockSize ;
549550
550551 uv_fs_t read_req;
551- const ssize_t numchars =
552- uv_fs_read (loop, &read_req, fd, &buf, 1 , offset, nullptr );
552+ numchars = uv_fs_read (loop, &read_req, fd, &buf, 1 , offset, nullptr );
553553 uv_fs_req_cleanup (&read_req);
554554
555- CHECK_GE (numchars, 0 );
555+ if (numchars < 0 ) {
556+ break ;
557+ }
558+
556559 if (static_cast <size_t >(numchars) < kBlockSize ) {
557560 chars.resize (start + numchars);
558561 }
@@ -566,17 +569,21 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
566569 CHECK_EQ (0 , uv_fs_close (loop, &close_req, fd, nullptr ));
567570 uv_fs_req_cleanup (&close_req);
568571
569- size_t start = 0 ;
570- if (chars.size () >= 3 && 0 == memcmp (&chars[0 ], " \xEF\xBB\xBF " , 3 )) {
571- start = 3 ; // Skip UTF-8 BOM.
572- }
572+ if (numchars < 0 ) {
573+ args.GetReturnValue ().Set (Undefined (env->isolate ()));
574+ } else {
575+ size_t start = 0 ;
576+ if (chars.size () >= 3 && 0 == memcmp (&chars[0 ], " \xEF\xBB\xBF " , 3 )) {
577+ start = 3 ; // Skip UTF-8 BOM.
578+ }
573579
574- Local<String> chars_string =
575- String::NewFromUtf8 (env->isolate (),
576- &chars[start],
577- String::kNormalString ,
578- chars.size () - start);
579- args.GetReturnValue ().Set (chars_string);
580+ Local<String> chars_string =
581+ String::NewFromUtf8 (env->isolate (),
582+ &chars[start],
583+ String::kNormalString ,
584+ chars.size () - start);
585+ args.GetReturnValue ().Set (chars_string);
586+ }
580587}
581588
582589// Used to speed up module loading. Returns 0 if the path refers to
0 commit comments