From ec93af3bd79c89088f832f72d788225abb106910 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 20 Jan 2018 15:11:03 +0100 Subject: [PATCH] src: don't abort when package.json is a directory Fixes: https://github.com/nodejs/node/issues/8307 --- src/node_file.cc | 15 ++++++++++----- .../packages/is-dir/package.json/.placeholder | 0 test/parallel/test-module-loading-error.js | 7 +++++++ 3 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/packages/is-dir/package.json/.placeholder diff --git a/src/node_file.cc b/src/node_file.cc index c721c4ba88d0e0..1d6296a4f57241 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -39,6 +39,7 @@ # include #endif +#include #include namespace node { @@ -458,6 +459,12 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo& args) { return; } + std::shared_ptr 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 chars; int64_t offset = 0; @@ -474,14 +481,12 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo& 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(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. diff --git a/test/fixtures/packages/is-dir/package.json/.placeholder b/test/fixtures/packages/is-dir/package.json/.placeholder new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/test/parallel/test-module-loading-error.js b/test/parallel/test-module-loading-error.js index f1c457af2b2c9e..11af0225830257 100644 --- a/test/parallel/test-module-loading-error.js +++ b/test/parallel/test-module-loading-error.js @@ -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\'' + });