@@ -538,6 +538,7 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
538
538
539
539
std::vector<char > chars;
540
540
int64_t offset = 0 ;
541
+ ssize_t numchars;
541
542
for (;;) {
542
543
const size_t kBlockSize = 32 << 10 ;
543
544
const size_t start = chars.size ();
@@ -548,11 +549,13 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
548
549
buf.len = kBlockSize ;
549
550
550
551
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 );
553
553
uv_fs_req_cleanup (&read_req);
554
554
555
- CHECK_GE (numchars, 0 );
555
+ if (numchars < 0 ) {
556
+ break ;
557
+ }
558
+
556
559
if (static_cast <size_t >(numchars) < kBlockSize ) {
557
560
chars.resize (start + numchars);
558
561
}
@@ -566,17 +569,21 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
566
569
CHECK_EQ (0 , uv_fs_close (loop, &close_req, fd, nullptr ));
567
570
uv_fs_req_cleanup (&close_req);
568
571
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
+ }
573
579
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
+ }
580
587
}
581
588
582
589
// Used to speed up module loading. Returns 0 if the path refers to
0 commit comments