-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No abort on require() with null bytes #8277
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
@@ -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); | ||
} | ||
|
@@ -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())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would short circuit the require lookup algorithm which may potentially find a working match in a further iteration. We can do it but it's not a small breaking change and IMO would be unnecessarily disruptive for little gain. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See also the comment above the function: // Used to speed up module loading. Returns the contents of the file as
// a string or undefined when the file cannot be opened. The speedup
// comes from not creating Error objects on failure. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 works for me. On Sunday, September 4, 2016, Anna Henningsen notifications@github.com
|
||
} 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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#6413 should land on Monday.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see a follow-up #8292, thanks.