src: don't abort when package.json is a directory

PR-URL: https://github.com/nodejs/node/pull/18270
Fixes: https://github.com/nodejs/node/issues/8307
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Ben Noordhuis 2018-01-20 15:11:03 +01:00 committed by Rich Trott
parent 259f62a8e8
commit 8ccd320549
3 changed files with 16 additions and 5 deletions

View File

@ -673,6 +673,12 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
return;
}
std::shared_ptr<void> defer_close(nullptr, [fd, loop] (...) {
uv_fs_t close_req;
CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr));
uv_fs_req_cleanup(&close_req);
});
const size_t kBlockSize = 32 << 10;
std::vector<char> chars;
int64_t offset = 0;
@ -689,14 +695,12 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
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)
return;
offset += numchars;
} while (static_cast<size_t>(numchars) == kBlockSize);
uv_fs_t close_req;
CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr));
uv_fs_req_cleanup(&close_req);
size_t start = 0;
if (offset >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) {
start = 3; // Skip UTF-8 BOM.

View File

@ -78,3 +78,10 @@ common.expectsError(
code: 'ERR_INVALID_ARG_VALUE',
message: 'The argument \'id\' must be a non-empty string. Received \'\''
});
common.expectsError(
() => { require('../fixtures/packages/is-dir'); },
{
code: 'MODULE_NOT_FOUND',
message: 'Cannot find module \'../fixtures/packages/is-dir\''
});