Skip to content
Permalink
Browse files
test: verify napi_remove_wrap with napi_delete_reference
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 daf63d2 commit 3112d5dae07f79bac86d22495f274ff9d661655c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
@@ -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;
@@ -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
@@ -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 3112d5d

Please sign in to comment.