Skip to content

Commit b5dc717

Browse files
node-api: avoid crashing on passed-in null string
When `napi_create_string_*` receives a null pointer as its second argument, it must null-check it before passing it into V8, otherwise a crash will occur. Signed-off-by: Gabriel Schulhof <[email protected]>
1 parent 4c508f5 commit b5dc717

File tree

6 files changed

+101
-1
lines changed

6 files changed

+101
-1
lines changed

src/js_native_api_v8.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,7 @@ napi_status napi_create_string_latin1(napi_env env,
14831483
size_t length,
14841484
napi_value* result) {
14851485
CHECK_ENV(env);
1486+
CHECK_ARG(env, str);
14861487
CHECK_ARG(env, result);
14871488
RETURN_STATUS_IF_FALSE(env,
14881489
(length == NAPI_AUTO_LENGTH) || length <= INT_MAX,
@@ -1505,6 +1506,7 @@ napi_status napi_create_string_utf8(napi_env env,
15051506
size_t length,
15061507
napi_value* result) {
15071508
CHECK_ENV(env);
1509+
CHECK_ARG(env, str);
15081510
CHECK_ARG(env, result);
15091511
RETURN_STATUS_IF_FALSE(env,
15101512
(length == NAPI_AUTO_LENGTH) || length <= INT_MAX,
@@ -1526,6 +1528,7 @@ napi_status napi_create_string_utf16(napi_env env,
15261528
size_t length,
15271529
napi_value* result) {
15281530
CHECK_ENV(env);
1531+
CHECK_ARG(env, str);
15291532
CHECK_ARG(env, result);
15301533
RETURN_STATUS_IF_FALSE(env,
15311534
(length == NAPI_AUTO_LENGTH) || length <= INT_MAX,

test/js-native-api/test_string/binding.gyp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"target_name": "test_string",
55
"sources": [
66
"../entry_point.c",
7-
"test_string.c"
7+
"test_string.c",
8+
"test_null.c",
9+
"../common.c",
810
]
911
}
1012
]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <js_native_api.h>
2+
3+
#include "../common.h"
4+
#include "test_null.h"
5+
6+
#define DECLARE_TEST(charset, str_arg) \
7+
static napi_value \
8+
test_create_##charset(napi_env env, napi_callback_info info) { \
9+
napi_value return_value, result; \
10+
NODE_API_CALL(env, napi_create_object(env, &return_value)); \
11+
\
12+
add_returned_status(env, \
13+
"envIsNull", \
14+
return_value, \
15+
"Invalid argument", \
16+
napi_invalid_arg, \
17+
napi_create_string_##charset(NULL, \
18+
(str_arg), \
19+
NAPI_AUTO_LENGTH, \
20+
&result)); \
21+
\
22+
napi_create_string_##charset(env, NULL, NAPI_AUTO_LENGTH, &result); \
23+
add_last_status(env, "stringIsNull", return_value); \
24+
\
25+
napi_create_string_##charset(env, (str_arg), NAPI_AUTO_LENGTH, NULL); \
26+
add_last_status(env, "resultIsNull", return_value); \
27+
\
28+
return return_value; \
29+
}
30+
31+
static const char16_t something[] = {
32+
(char16_t)'s',
33+
(char16_t)'o',
34+
(char16_t)'m',
35+
(char16_t)'e',
36+
(char16_t)'t',
37+
(char16_t)'h',
38+
(char16_t)'i',
39+
(char16_t)'n',
40+
(char16_t)'g',
41+
(char16_t)'\0'
42+
};
43+
44+
DECLARE_TEST(utf8, "something")
45+
DECLARE_TEST(latin1, "something")
46+
DECLARE_TEST(utf16, something)
47+
48+
void init_test_null(napi_env env, napi_value exports) {
49+
napi_value test_null;
50+
51+
const napi_property_descriptor test_null_props[] = {
52+
DECLARE_NODE_API_PROPERTY("test_create_utf8", test_create_utf8),
53+
DECLARE_NODE_API_PROPERTY("test_create_latin1", test_create_latin1),
54+
DECLARE_NODE_API_PROPERTY("test_create_utf16", test_create_utf16),
55+
};
56+
57+
NODE_API_CALL_RETURN_VOID(env, napi_create_object(env, &test_null));
58+
NODE_API_CALL_RETURN_VOID(env, napi_define_properties(
59+
env, test_null, sizeof(test_null_props) / sizeof(*test_null_props),
60+
test_null_props));
61+
62+
const napi_property_descriptor test_null_set = {
63+
"testNull", NULL, NULL, NULL, NULL, test_null, napi_enumerable, NULL
64+
};
65+
66+
NODE_API_CALL_RETURN_VOID(env,
67+
napi_define_properties(env, exports, 1, &test_null_set));
68+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef TEST_JS_NATIVE_API_TEST_STRING_TEST_NULL_H_
2+
#define TEST_JS_NATIVE_API_TEST_STRING_TEST_NULL_H_
3+
4+
#include <js_native_api.h>
5+
6+
void init_test_null(napi_env env, napi_value exports);
7+
8+
#endif // TEST_JS_NATIVE_API_TEST_STRING_TEST_NULL_H_
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
const common = require('../../common');
3+
const assert = require('assert');
4+
5+
// Test passing NULL to object-related N-APIs.
6+
const { testNull } = require(`./build/${common.buildType}/test_string`);
7+
8+
const expectedResult = {
9+
envIsNull: 'Invalid argument',
10+
stringIsNull: 'Invalid argument',
11+
resultIsNull: 'Invalid argument',
12+
};
13+
14+
assert.deepStrictEqual(expectedResult, testNull.test_create_latin1());
15+
assert.deepStrictEqual(expectedResult, testNull.test_create_utf8());
16+
assert.deepStrictEqual(expectedResult, testNull.test_create_utf16());

test/js-native-api/test_string/test_string.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <string.h>
33
#include <js_native_api.h>
44
#include "../common.h"
5+
#include "test_null.h"
56

67
static napi_value TestLatin1(napi_env env, napi_callback_info info) {
78
size_t argc = 1;
@@ -283,6 +284,8 @@ napi_value Init(napi_env env, napi_value exports) {
283284
DECLARE_NODE_API_PROPERTY("TestMemoryCorruption", TestMemoryCorruption),
284285
};
285286

287+
init_test_null(env, exports);
288+
286289
NODE_API_CALL(env, napi_define_properties(
287290
env, exports, sizeof(properties) / sizeof(*properties), properties));
288291

0 commit comments

Comments
 (0)