Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,8 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> exports,
// Create a new napi_env for this specific module.
napi_env env =
node_napi_env__::New(context, module_filename, module_api_version);
// `module_api_version` is not supported.
if (env == nullptr) return;

napi_value _exports = nullptr;
env->CallIntoModule([&](napi_env env) {
Expand Down
12 changes: 12 additions & 0 deletions test/node-api/test_module_version_mismatch/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
'targets': [
{
'target_name': 'test_module_version_mismatch',
'sources': [ 'test_module_version_mismatch.c' ],
# One below NAPI_VERSION_EXPERIMENTAL, so it is always above
# NODE_API_SUPPORTED_VERSION_MAX and never becomes a real version, but is
# not the experimental value the version check deliberately allows.
'defines': [ 'NAPI_VERSION=2147483646' ]
}
]
}
12 changes: 12 additions & 0 deletions test/node-api/test_module_version_mismatch/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
const common = require('../../common');
const assert = require('assert');

// An add-on that requires a newer Node-API version than this binary supports
// must be rejected with an error that `require()` can catch. The version check
// in `node_napi_env__::New()` already produces that error, but its nullptr

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, please refrain from including impl internals in the test comments.

// return used to be dereferenced by `napi_module_register_by_symbol()`, so the
// process segfaulted before the error could surface.
assert.throws(
() => require(`./build/${common.buildType}/test_module_version_mismatch`),
/requires Node-API version 2147483646, but this version of Node\.js only supports version \d+ add-ons\./);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <node_api.h>

// This add-on declares a Node-API version that no build supports, so loading it
// must fail with an error -- not a crash.
NAPI_MODULE_INIT() {
return exports;
}