Skip to content

Commit 2bbabb1

Browse files
jasonginmhdawson
authored andcommitted
n-api: napi_get_cb_info should fill array
When the number of args requested is greater than the actual number of args supplied to the function call, the remainder of the args array should be filled in with `undefined` values. Because of this bug, the remainder of the array was left uninitialized, which could cause a crash. Refer to the documentation for the `argv` parameter at https://github.com/nodejs/node/blob/master/doc/api/n-api.md#napi_get_cb_info PR-URL: #12863 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 4cb5f3d commit 2bbabb1

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/node_api.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,7 @@ napi_status napi_get_cb_info(
15241524

15251525
if (argv != nullptr) {
15261526
CHECK_ARG(env, argc);
1527-
info->Args(argv, std::min(*argc, info->ArgsLength()));
1527+
info->Args(argv, *argc);
15281528
}
15291529
if (argc != nullptr) {
15301530
*argc = info->ArgsLength();

test/addons-napi/3_callbacks/binding.c

+15-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,23 @@
33
#include <string.h>
44

55
napi_value RunCallback(napi_env env, napi_callback_info info) {
6-
size_t argc = 1;
7-
napi_value args[1];
6+
size_t argc = 2;
7+
napi_value args[2];
88
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
99

10+
NAPI_ASSERT(env, argc == 1,
11+
"Wrong number of arguments. Expects a single argument.");
12+
13+
napi_valuetype valuetype0;
14+
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
15+
NAPI_ASSERT(env, valuetype0 == napi_function,
16+
"Wrong type of arguments. Expects a function as first argument.");
17+
18+
napi_valuetype valuetype1;
19+
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
20+
NAPI_ASSERT(env, valuetype1 == napi_undefined,
21+
"Additional arguments should be undefined.");
22+
1023
napi_value argv[1];
1124
const char* str = "hello world";
1225
size_t str_len = strlen(str);

test/addons-napi/test_function/test_function.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ napi_value Test(napi_env env, napi_callback_info info) {
1212
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
1313

1414
NAPI_ASSERT(env, valuetype0 == napi_function,
15-
"Wrong type of arguments. Expects a number as first argument.");
15+
"Wrong type of arguments. Expects a function as first argument.");
1616

1717
napi_value* argv = args + 1;
1818
argc = argc - 1;

0 commit comments

Comments
 (0)