GeezerVison is showing github.com — node-api: do not crash on module version mismatch · nodejs/node@8d904b4
File tree
Expand file treeCollapse file tree
Open diff view settings
test/node-api/test_module_version_mismatch
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: src/node_api.cc +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -790,6 +790,8 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> exports, |
790 | 790 | // Create a new napi_env for this specific module. |
791 | 791 | napi_env env = |
792 | 792 | node_napi_env__::New(context, module_filename, module_api_version); |
| 793 | + // `module_api_version` is not supported. |
| 794 | + if (env == nullptr) return; |
793 | 795 |
|
794 | 796 | napi_value _exports = nullptr; |
795 | 797 | env->CallIntoModule([&](napi_env env) { |
|
Collapse file
Copy file name to clipboard +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +{ |
| 2 | + 'targets': [ |
| 3 | + { |
| 4 | + 'target_name': 'test_module_version_mismatch', |
| 5 | + 'sources': [ 'test_module_version_mismatch.c' ], |
| 6 | + # One below NAPI_VERSION_EXPERIMENTAL, so it is always above |
| 7 | + # NODE_API_SUPPORTED_VERSION_MAX and never becomes a real version, but is |
| 8 | + # not the experimental value the version check deliberately allows. |
| 9 | + 'defines': [ 'NAPI_VERSION=2147483646' ] |
| 10 | + } |
| 11 | + ] |
| 12 | +} |
Collapse file
Copy file name to clipboard +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +'use strict'; |
| 2 | +const common = require('../../common'); |
| 3 | +const assert = require('assert'); |
| 4 | + |
| 5 | +// An add-on that requires a newer Node-API version than this binary supports |
| 6 | +// must be rejected with an error that `require()` can catch, rather than |
| 7 | +// crashing the process. |
| 8 | +assert.throws( |
| 9 | + () => require(`./build/${common.buildType}/test_module_version_mismatch`), |
| 10 | + /requires Node-API version 2147483646, but this version of Node\.js only supports version \d+ add-ons\./); |
Collapse file
Copy file name to clipboard +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +#include <node_api.h> |
| 2 | + |
| 3 | +// This add-on declares a Node-API version that no build supports, so loading it |
| 4 | +// must fail with an error -- not a crash. |
| 5 | +NAPI_MODULE_INIT() { |
| 6 | + return exports; |
| 7 | +} |