Skip to content

Commit

Permalink
src: don't abort when package.json is a directory
Browse files Browse the repository at this point in the history
PR-URL: nodejs#18270
Fixes: nodejs#8307
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Bradley Farias <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Guy Bedford <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
bnoordhuis authored and yhwang committed Feb 22, 2018
1 parent 927c1b1 commit fce175b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,12 @@ static void InternalModuleReadFile(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;
Expand All @@ -502,14 +508,12 @@ static void InternalModuleReadFile(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.
Expand Down
Empty file.
7 changes: 7 additions & 0 deletions test/parallel/test-module-loading-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,10 @@ common.expectsError(
code: 'ERR_ASSERTION',
message: /^path must be a string$/
});

common.expectsError(
() => { require('../fixtures/packages/is-dir'); },
{
code: 'MODULE_NOT_FOUND',
message: 'Cannot find module \'../fixtures/packages/is-dir\''
});

0 comments on commit fce175b

Please sign in to comment.