Skip to content

Commit 00d094f

Browse files
mhdawsonMylesBorins
authored andcommittedApr 16, 2018
n-api: add check for large strings
n-api uses size_t for the size of strings when specifying string lengths. V8 only supports a size of int. Add a check so that an error will be returned if the user passes in a string with a size larger than will fit into an int. Backport-PR-URL: #19447 PR-URL: #15611 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
1 parent e9a6dff commit 00d094f

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed
 

‎src/node_api.cc

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#include <node_buffer.h>
1212
#include <node_object_wrap.h>
13-
13+
#include <limits.h> // INT_MAX
1414
#include <string.h>
1515
#include <algorithm>
1616
#include <cmath>
@@ -129,6 +129,9 @@ struct napi_env__ {
129129
do { \
130130
static_assert(static_cast<int>(NAPI_AUTO_LENGTH) == -1, \
131131
"Casting NAPI_AUTO_LENGTH to int must result in -1"); \
132+
RETURN_STATUS_IF_FALSE((env), \
133+
(len == NAPI_AUTO_LENGTH) || len <= INT_MAX, \
134+
napi_invalid_arg); \
132135
auto str_maybe = v8::String::NewFromUtf8( \
133136
(env)->isolate, (str), v8::NewStringType::kInternalized, \
134137
static_cast<int>(len)); \
@@ -871,7 +874,7 @@ void napi_module_register(napi_module* mod) {
871874

872875
// Warning: Keep in-sync with napi_status enum
873876
const char* error_messages[] = {nullptr,
874-
"Invalid pointer passed as argument",
877+
"Invalid argument",
875878
"An object was expected",
876879
"A string was expected",
877880
"A string or symbol was expected",

‎test/addons-napi/test_string/test.js

+4
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@ assert.strictEqual(test_string.TestUtf8Insufficient(str6), str6.slice(0, 1));
6969
assert.strictEqual(test_string.TestUtf16Insufficient(str6), str6.slice(0, 3));
7070
assert.strictEqual(test_string.Utf16Length(str6), 5);
7171
assert.strictEqual(test_string.Utf8Length(str6), 14);
72+
73+
assert.throws(() => {
74+
test_string.TestLargeUtf8();
75+
}, /^Error: Invalid argument$/);

‎test/addons-napi/test_string/test_string.c

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <limits.h> // INT_MAX
12
#include <node_api.h>
23
#include "../common.h"
34

@@ -201,6 +202,19 @@ napi_value Utf8Length(napi_env env, napi_callback_info info) {
201202
return output;
202203
}
203204

205+
napi_value TestLargeUtf8(napi_env env, napi_callback_info info) {
206+
napi_value output;
207+
if (SIZE_MAX > INT_MAX) {
208+
NAPI_CALL(env, napi_create_string_utf8(env, "", ((size_t)INT_MAX) + 1, &output));
209+
} else {
210+
// just throw the expected error as there is nothing to test
211+
// in this case since we can't overflow
212+
NAPI_CALL(env, napi_throw_error(env, NULL, "Invalid argument"));
213+
}
214+
215+
return output;
216+
}
217+
204218
napi_value Init(napi_env env, napi_value exports) {
205219
napi_property_descriptor properties[] = {
206220
DECLARE_NAPI_PROPERTY("TestLatin1", TestLatin1),
@@ -211,6 +225,7 @@ napi_value Init(napi_env env, napi_value exports) {
211225
DECLARE_NAPI_PROPERTY("TestUtf16Insufficient", TestUtf16Insufficient),
212226
DECLARE_NAPI_PROPERTY("Utf16Length", Utf16Length),
213227
DECLARE_NAPI_PROPERTY("Utf8Length", Utf8Length),
228+
DECLARE_NAPI_PROPERTY("TestLargeUtf8", TestLargeUtf8),
214229
};
215230

216231
NAPI_CALL(env, napi_define_properties(

0 commit comments

Comments
 (0)