Skip to content

Commit

Permalink
src: cover extra load-via-special-symbol scenario
Browse files Browse the repository at this point in the history
We need to look for a special symbol even if the module self-registers
when the module self-registers with the wrong version.

PR-URL: #20186
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
Gabriel Schulhof authored and jasnell committed Apr 23, 2018
1 parent 65e62e5 commit 6949543
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2292,6 +2292,13 @@ static void DLOpen(const FunctionCallbackInfo<Value>& args) {

// -1 is used for N-API modules
if ((mp->nm_version != -1) && (mp->nm_version != NODE_MODULE_VERSION)) {
// Even if the module did self-register, it may have done so with the wrong
// version. We must only give up after having checked to see if it has an
// appropriate initializer callback.
if (auto callback = GetInitializerCallback(&dlib)) {
callback(exports, module, context);
return;
}
char errmsg[1024];
snprintf(errmsg,
sizeof(errmsg),
Expand Down
17 changes: 17 additions & 0 deletions test/addons/hello-world/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,20 @@ extern "C" NODE_MODULE_EXPORT void INITIALIZER(v8::Local<v8::Object> exports,
v8::Local<v8::Context> context) {
NODE_SET_METHOD(exports, "hello", Method);
}

static void FakeInit(v8::Local<v8::Object> exports,
v8::Local<v8::Value> module,
v8::Local<v8::Context> context) {
auto isolate = context->GetIsolate();
auto exception = v8::Exception::Error(v8::String::NewFromUtf8(isolate,
"FakeInit should never run!", v8::NewStringType::kNormal)
.ToLocalChecked());
isolate->ThrowException(exception);
}

// Define a Node.js module, but with the wrong version. Node.js should still be
// able to load this module, multiple times even, because it exposes the
// specially named initializer above.
#undef NODE_MODULE_VERSION
#define NODE_MODULE_VERSION 3
NODE_MODULE(NODE_GYP_MODULE_NAME, FakeInit)

0 comments on commit 6949543

Please sign in to comment.