Skip to content

Commit 127487a

Browse files
bnoordhuisMylesBorins
authored andcommitted
src: don't abort when package.json is a directory
PR-URL: #18270 Fixes: #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]>
1 parent 67bd099 commit 127487a

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/node_file.cc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,12 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
486486
return;
487487
}
488488

489+
std::shared_ptr<void> defer_close(nullptr, [fd, loop] (...) {
490+
uv_fs_t close_req;
491+
CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr));
492+
uv_fs_req_cleanup(&close_req);
493+
});
494+
489495
const size_t kBlockSize = 32 << 10;
490496
std::vector<char> chars;
491497
int64_t offset = 0;
@@ -502,14 +508,12 @@ static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
502508
numchars = uv_fs_read(loop, &read_req, fd, &buf, 1, offset, nullptr);
503509
uv_fs_req_cleanup(&read_req);
504510

505-
CHECK_GE(numchars, 0);
511+
if (numchars < 0)
512+
return;
513+
506514
offset += numchars;
507515
} while (static_cast<size_t>(numchars) == kBlockSize);
508516

509-
uv_fs_t close_req;
510-
CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr));
511-
uv_fs_req_cleanup(&close_req);
512-
513517
size_t start = 0;
514518
if (offset >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) {
515519
start = 3; // Skip UTF-8 BOM.

test/fixtures/packages/is-dir/package.json/.placeholder

Whitespace-only changes.

test/parallel/test-module-loading-error.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,10 @@ common.expectsError(
7272
code: 'ERR_ASSERTION',
7373
message: /^path must be a string$/
7474
});
75+
76+
common.expectsError(
77+
() => { require('../fixtures/packages/is-dir'); },
78+
{
79+
code: 'MODULE_NOT_FOUND',
80+
message: 'Cannot find module \'../fixtures/packages/is-dir\''
81+
});

0 commit comments

Comments
 (0)