Skip to content

Commit a9388f6

Browse files
bnoordhuisMayaLekova
authored andcommitted
src: don't abort when package.json is a directory
PR-URL: nodejs#18270 Fixes: nodejs#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>
1 parent 43bdf19 commit a9388f6

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/node_file.cc

+9-5
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,12 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
673673
return;
674674
}
675675

676+
std::shared_ptr<void> defer_close(nullptr, [fd, loop] (...) {
677+
uv_fs_t close_req;
678+
CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr));
679+
uv_fs_req_cleanup(&close_req);
680+
});
681+
676682
const size_t kBlockSize = 32 << 10;
677683
std::vector<char> chars;
678684
int64_t offset = 0;
@@ -689,14 +695,12 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
689695
numchars = uv_fs_read(loop, &read_req, fd, &buf, 1, offset, nullptr);
690696
uv_fs_req_cleanup(&read_req);
691697

692-
CHECK_GE(numchars, 0);
698+
if (numchars < 0)
699+
return;
700+
693701
offset += numchars;
694702
} while (static_cast<size_t>(numchars) == kBlockSize);
695703

696-
uv_fs_t close_req;
697-
CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr));
698-
uv_fs_req_cleanup(&close_req);
699-
700704
size_t start = 0;
701705
if (offset >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) {
702706
start = 3; // Skip UTF-8 BOM.

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

Whitespace-only changes.

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

+7
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,10 @@ common.expectsError(
7878
code: 'ERR_INVALID_ARG_VALUE',
7979
message: 'The argument \'id\' must be a non-empty string. Received \'\''
8080
});
81+
82+
common.expectsError(
83+
() => { require('../fixtures/packages/is-dir'); },
84+
{
85+
code: 'MODULE_NOT_FOUND',
86+
message: 'Cannot find module \'../fixtures/packages/is-dir\''
87+
});

0 commit comments

Comments
 (0)