Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8934feb

Browse files
legendecasRafaelGSS
authored andcommittedSep 26, 2022
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 <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent e7afbe1 commit 8934feb

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
 

‎test/js-native-api/test_reference_double_free/test_reference_double_free.c

+26
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ static napi_value New(napi_env env, napi_callback_info info) {
4242
return js_this;
4343
}
4444

45+
static void NoopDeleter(napi_env env, void* data, void* hint) {}
46+
47+
static void DeleteImmediately(napi_env env, napi_callback_info info) {
48+
size_t argc = 1;
49+
napi_value js_obj;
50+
napi_ref ref;
51+
52+
NODE_API_CALL_RETURN_VOID(env,
53+
napi_get_cb_info(env, info, &argc, &js_obj, NULL, NULL));
54+
55+
napi_valuetype type;
56+
NODE_API_CALL_RETURN_VOID(env, napi_typeof(env, js_obj, &type));
57+
58+
NODE_API_CALL_RETURN_VOID(env,
59+
napi_wrap(env, js_obj, NULL, NoopDeleter, NULL, &ref));
60+
NODE_API_CALL_RETURN_VOID(env, napi_delete_reference(env, ref));
61+
NODE_API_CALL_RETURN_VOID(env, napi_remove_wrap(env, js_obj, NULL));
62+
}
63+
4564
EXTERN_C_START
4665
napi_value Init(napi_env env, napi_value exports) {
4766
napi_value myobj_ctor;
@@ -50,6 +69,13 @@ napi_value Init(napi_env env, napi_value exports) {
5069
env, "MyObject", NAPI_AUTO_LENGTH, New, NULL, 0, NULL, &myobj_ctor));
5170
NODE_API_CALL(env,
5271
napi_set_named_property(env, exports, "MyObject", myobj_ctor));
72+
73+
napi_property_descriptor descriptors[] = {
74+
DECLARE_NODE_API_PROPERTY("deleteImmediately", DeleteImmediately),
75+
};
76+
NODE_API_CALL(env, napi_define_properties(
77+
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
78+
5379
return exports;
5480
}
5581
EXTERN_C_END
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
// This test makes no assertions. It tests that calling napi_remove_wrap and
4+
// napi_delete_reference consecutively doesn't crash the process.
5+
6+
const { buildType } = require('../../common');
7+
8+
const addon = require(`./build/${buildType}/test_reference_double_free`);
9+
10+
addon.deleteImmediately({});

0 commit comments

Comments
 (0)
Please sign in to comment.