Skip to content
Permalink
Browse files

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 <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information...
jasongin authored and mhdawson committed May 5, 2017
1 parent 4cb5f3d commit 2bbabb1f8552b684cb9ae294807c6acc0e3170dc
Showing with 17 additions and 4 deletions.
  1. +1 −1 src/node_api.cc
  2. +15 −2 test/addons-napi/3_callbacks/binding.c
  3. +1 −1 test/addons-napi/test_function/test_function.c
@@ -1524,7 +1524,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();
@@ -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);
@@ -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;

0 comments on commit 2bbabb1

Please sign in to comment.
You can’t perform that action at this time.