Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

n-api: add check for large strings #15611

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <node_buffer.h>
#include <node_object_wrap.h>
#include <limits.h> // INT_MAX
#include <string.h>
#include <algorithm>
#include <cmath>
Expand Down Expand Up @@ -125,6 +126,9 @@ struct napi_env__ {
do { \
static_assert(static_cast<int>(NAPI_AUTO_LENGTH) == -1, \
"Casting NAPI_AUTO_LENGTH to int must result in -1"); \
RETURN_STATUS_IF_FALSE((env), \
(len == NAPI_AUTO_LENGTH) || len <= INT_MAX, \
napi_generic_failure); \
auto str_maybe = v8::String::NewFromUtf8( \
(env)->isolate, (str), v8::NewStringType::kInternalized, \
static_cast<int>(len)); \
Expand Down
4 changes: 4 additions & 0 deletions test/addons-napi/test_string/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@ assert.strictEqual(test_string.TestUtf8Insufficient(str6), str6.slice(0, 1));
assert.strictEqual(test_string.TestUtf16Insufficient(str6), str6.slice(0, 3));
assert.strictEqual(test_string.Utf16Length(str6), 5);
assert.strictEqual(test_string.Utf8Length(str6), 14);

assert.throws(() => {
test_string.TestLargeUtf8()
}, /^Error: Unknown failure$/);
8 changes: 8 additions & 0 deletions test/addons-napi/test_string/test_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ napi_value Utf8Length(napi_env env, napi_callback_info info) {
return output;
}

napi_value TestLargeUtf8(napi_env env, napi_callback_info info) {
napi_value output;
NAPI_CALL(env, napi_create_string_utf8(env, "", 4294967296 + 1, &output));

return output;
}

napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor properties[] = {
DECLARE_NAPI_PROPERTY("TestLatin1", TestLatin1),
Expand All @@ -211,6 +218,7 @@ napi_value Init(napi_env env, napi_value exports) {
DECLARE_NAPI_PROPERTY("TestUtf16Insufficient", TestUtf16Insufficient),
DECLARE_NAPI_PROPERTY("Utf16Length", Utf16Length),
DECLARE_NAPI_PROPERTY("Utf8Length", Utf8Length),
DECLARE_NAPI_PROPERTY("TestLargeUtf8", TestLargeUtf8),
};

NAPI_CALL(env, napi_define_properties(
Expand Down