Skip to content

Commit a9d8ec7

Browse files
rvaggsaper
authored andcommitted
fs: return undefined on module read uv fs error
PR-URL: nodejs/node#8277
1 parent 82aa34e commit a9d8ec7

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

src/node_file.cc

+25-11
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
516516
int64_t offset = 0;
517517
ssize_t numchars;
518518
do {
519+
const size_t kBlockSize = 32 << 10;
519520
const size_t start = chars.size();
520521
chars.resize(start + kBlockSize);
521522

@@ -527,25 +528,38 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
527528
numchars = uv_fs_read(loop, &read_req, fd, &buf, 1, offset, nullptr);
528529
uv_fs_req_cleanup(&read_req);
529530

530-
CHECK_GE(numchars, 0);
531+
if (numchars < 0) {
532+
break;
533+
}
534+
535+
if (static_cast<size_t>(numchars) < kBlockSize) {
536+
chars.resize(start + numchars);
537+
}
538+
if (numchars == 0) {
539+
break;
540+
}
531541
offset += numchars;
532542
} while (static_cast<size_t>(numchars) == kBlockSize);
533543

534544
uv_fs_t close_req;
535545
CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr));
536546
uv_fs_req_cleanup(&close_req);
537547

538-
size_t start = 0;
539-
if (offset >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) {
540-
start = 3; // Skip UTF-8 BOM.
541-
}
548+
if (numchars < 0) {
549+
args.GetReturnValue().Set(Undefined(env->isolate()));
550+
} else {
551+
size_t start = 0;
552+
if (chars.size() >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) {
553+
start = 3; // Skip UTF-8 BOM.
554+
}
542555

543-
Local<String> chars_string =
544-
String::NewFromUtf8(env->isolate(),
545-
&chars[start],
546-
String::kNormalString,
547-
offset - start);
548-
args.GetReturnValue().Set(chars_string);
556+
Local<String> chars_string =
557+
String::NewFromUtf8(env->isolate(),
558+
&chars[start],
559+
String::kNormalString,
560+
chars.size() - start);
561+
args.GetReturnValue().Set(chars_string);
562+
}
549563
}
550564

551565
// Used to speed up module loading. Returns 0 if the path refers to

0 commit comments

Comments
 (0)