Skip to content

Commit cd3e2d1

Browse files
author
Gabriel Schulhof
committed
add test
1 parent da42a46 commit cd3e2d1

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "test_reference_double_free",
5+
"sources": [
6+
"../entry_point.c",
7+
"test_reference_double_free.c"
8+
]
9+
}
10+
]
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
// This test makes no assertions. It tests a fix without which it will crash
4+
// with a double free.
5+
6+
const { buildType } = require('../../common');
7+
8+
const addon = require(`./build/${buildType}/test_reference_double_free`);
9+
10+
{ const obj = new addon.MyObject(); }
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <stdlib.h>
2+
#include <js_native_api.h>
3+
#include "../common.h"
4+
5+
static void Destructor(napi_env env, void* data, void* nothing) {
6+
napi_ref* ref = data;
7+
NODE_API_CALL_RETURN_VOID(env, napi_delete_reference(env, *ref));
8+
free(ref);
9+
}
10+
11+
static napi_value New(napi_env env, napi_callback_info info) {
12+
size_t argc = 0;
13+
napi_value js_this;
14+
napi_ref* ref = malloc(sizeof(*ref));
15+
16+
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, NULL, &js_this, NULL));
17+
NODE_API_CALL(env, napi_wrap(env, js_this, ref, Destructor, NULL, ref));
18+
NODE_API_CALL(env, napi_reference_ref(env, *ref, NULL));
19+
20+
return js_this;
21+
}
22+
23+
EXTERN_C_START
24+
napi_value Init(napi_env env, napi_value exports) {
25+
napi_value myobj_ctor;
26+
NODE_API_CALL(env,
27+
napi_define_class(
28+
env, "MyObject", NAPI_AUTO_LENGTH, New, NULL, 0, NULL, &myobj_ctor));
29+
NODE_API_CALL(env,
30+
napi_set_named_property(env, exports, "MyObject", myobj_ctor));
31+
return exports;
32+
}
33+
EXTERN_C_END

0 commit comments

Comments
 (0)