Skip to content

Commit

Permalink
test: verify napi_remove_wrap with napi_delete_reference
Browse files Browse the repository at this point in the history
Verify that napi_remove_wrap and napi_delete_reference should be safe
to be called consecutively on the in-out params of napi_wrap.

PR-URL: #44754
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
  • Loading branch information
legendecas authored and RafaelGSS committed Sep 26, 2022
1 parent ce085d7 commit 3474cd8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ static napi_value New(napi_env env, napi_callback_info info) {
return js_this;
}

static void NoopDeleter(napi_env env, void* data, void* hint) {}

static void DeleteImmediately(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value js_obj;
napi_ref ref;

NODE_API_CALL_RETURN_VOID(env,
napi_get_cb_info(env, info, &argc, &js_obj, NULL, NULL));

napi_valuetype type;
NODE_API_CALL_RETURN_VOID(env, napi_typeof(env, js_obj, &type));

NODE_API_CALL_RETURN_VOID(env,
napi_wrap(env, js_obj, NULL, NoopDeleter, NULL, &ref));
NODE_API_CALL_RETURN_VOID(env, napi_delete_reference(env, ref));
NODE_API_CALL_RETURN_VOID(env, napi_remove_wrap(env, js_obj, NULL));
}

EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_value myobj_ctor;
Expand All @@ -50,6 +69,13 @@ napi_value Init(napi_env env, napi_value exports) {
env, "MyObject", NAPI_AUTO_LENGTH, New, NULL, 0, NULL, &myobj_ctor));
NODE_API_CALL(env,
napi_set_named_property(env, exports, "MyObject", myobj_ctor));

napi_property_descriptor descriptors[] = {
DECLARE_NODE_API_PROPERTY("deleteImmediately", DeleteImmediately),
};
NODE_API_CALL(env, napi_define_properties(
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));

return exports;
}
EXTERN_C_END
10 changes: 10 additions & 0 deletions test/js-native-api/test_reference_double_free/test_wrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

// This test makes no assertions. It tests that calling napi_remove_wrap and
// napi_delete_reference consecutively doesn't crash the process.

const { buildType } = require('../../common');

const addon = require(`./build/${buildType}/test_reference_double_free`);

addon.deleteImmediately({});

0 comments on commit 3474cd8

Please sign in to comment.