diff --git a/src/node_file.cc b/src/node_file.cc index 77d11756a2dbe5..7244644fa354f2 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -538,6 +538,7 @@ static void InternalModuleReadFile(const FunctionCallbackInfo& args) { std::vector chars; int64_t offset = 0; + ssize_t numchars; for (;;) { const size_t kBlockSize = 32 << 10; const size_t start = chars.size(); @@ -548,11 +549,13 @@ static void InternalModuleReadFile(const FunctionCallbackInfo& args) { buf.len = kBlockSize; uv_fs_t read_req; - const ssize_t numchars = - uv_fs_read(loop, &read_req, fd, &buf, 1, offset, nullptr); + numchars = uv_fs_read(loop, &read_req, fd, &buf, 1, offset, nullptr); uv_fs_req_cleanup(&read_req); - CHECK_GE(numchars, 0); + if (numchars < 0) { + break; + } + if (static_cast(numchars) < kBlockSize) { chars.resize(start + numchars); } @@ -566,17 +569,21 @@ static void InternalModuleReadFile(const FunctionCallbackInfo& args) { CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr)); uv_fs_req_cleanup(&close_req); - size_t start = 0; - if (chars.size() >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) { - start = 3; // Skip UTF-8 BOM. - } + if (numchars < 0) { + args.GetReturnValue().Set(Undefined(env->isolate())); + } else { + size_t start = 0; + if (chars.size() >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) { + start = 3; // Skip UTF-8 BOM. + } - Local chars_string = - String::NewFromUtf8(env->isolate(), - &chars[start], - String::kNormalString, - chars.size() - start); - args.GetReturnValue().Set(chars_string); + Local chars_string = + String::NewFromUtf8(env->isolate(), + &chars[start], + String::kNormalString, + chars.size() - start); + args.GetReturnValue().Set(chars_string); + } } // Used to speed up module loading. Returns 0 if the path refers to