Skip to content

Commit

Permalink
n-api: napi_get_cb_info should fill array
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
  • Loading branch information
jasongin authored and MylesBorins committed Apr 16, 2018
1 parent ce03977 commit 2e36365
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,7 @@ napi_status napi_get_cb_info(

if (argv != nullptr) {
CHECK_ARG(env, argc);
info->Args(argv, std::min(*argc, info->ArgsLength()));
info->Args(argv, *argc);
}
if (argc != nullptr) {
*argc = info->ArgsLength();
Expand Down
17 changes: 15 additions & 2 deletions test/addons-napi/3_callbacks/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,23 @@
#include <string.h>

napi_value RunCallback(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
size_t argc = 2;
napi_value args[2];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NAPI_ASSERT(env, argc == 1,
"Wrong number of arguments. Expects a single argument.");

napi_valuetype valuetype0;
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
NAPI_ASSERT(env, valuetype0 == napi_function,
"Wrong type of arguments. Expects a function as first argument.");

napi_valuetype valuetype1;
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
NAPI_ASSERT(env, valuetype1 == napi_undefined,
"Additional arguments should be undefined.");

napi_value argv[1];
const char* str = "hello world";
size_t str_len = strlen(str);
Expand Down
2 changes: 1 addition & 1 deletion test/addons-napi/test_function/test_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ napi_value Test(napi_env env, napi_callback_info info) {
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));

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

napi_value* argv = args + 1;
argc = argc - 1;
Expand Down

0 comments on commit 2e36365

Please sign in to comment.