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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Jason Ginchereau 2017-05-05 11:38:21 -07:00 committed by Michael Dawson
parent 4cb5f3daa3
commit 2bbabb1f85
3 changed files with 17 additions and 4 deletions

View File

@ -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();

View File

@ -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);

View File

@ -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;