Skip to content

Commit

Permalink
fs: return undefined on module read uv fs error
Browse files Browse the repository at this point in the history
PR-URL: #8277
  • Loading branch information
rvagg committed Aug 27, 2016
1 parent 063d14e commit de95b66
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {

std::vector<char> chars;
int64_t offset = 0;
ssize_t numchars;
for (;;) {
const size_t kBlockSize = 32 << 10;
const size_t start = chars.size();
Expand All @@ -548,11 +549,13 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& 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<size_t>(numchars) < kBlockSize) {
chars.resize(start + numchars);
}
Expand All @@ -566,17 +569,21 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& 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<String> chars_string =
String::NewFromUtf8(env->isolate(),
&chars[start],
String::kNormalString,
chars.size() - start);
args.GetReturnValue().Set(chars_string);
Local<String> 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
Expand Down

0 comments on commit de95b66

Please sign in to comment.