Skip to content

Commit 2e36365

Browse files
jasonginMylesBorins
authored andcommittedApr 16, 2018
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 Backport-PR-URL: #19447 PR-URL: #12863 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent ce03977 commit 2e36365

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
@@ -1526,7 +1526,7 @@ napi_status napi_get_cb_info(
15261526

15271527
if (argv != nullptr) {
15281528
CHECK_ARG(env, argc);
1529-
info->Args(argv, std::min(*argc, info->ArgsLength()));
1529+
info->Args(argv, *argc);
15301530
}
15311531
if (argc != nullptr) {
15321532
*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)
Please sign in to comment.