| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3f2ea53 commit 5f469a2
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,7 @@ | |||
| 10 | 10 | ||
| 11 | 11 | #include <node_buffer.h> | |
| 12 | 12 | #include <node_object_wrap.h> | |
| 13 | + #include <limits.h> // INT_MAX | ||
| 13 | 14 | #include <string.h> | |
| 14 | 15 | #include <algorithm> | |
| 15 | 16 | #include <cmath> | |
@@ -125,6 +126,9 @@ struct napi_env__ { | |||
| 125 | 126 | do { \ | |
| 126 | 127 | static_assert(static_cast<int>(NAPI_AUTO_LENGTH) == -1, \ | |
| 127 | 128 | "Casting NAPI_AUTO_LENGTH to int must result in -1"); \ | |
| 129 | + RETURN_STATUS_IF_FALSE((env), \ | ||
| 130 | + (len == NAPI_AUTO_LENGTH) || len <= INT_MAX, \ | ||
| 131 | + napi_invalid_arg); \ | ||
| 128 | 132 | auto str_maybe = v8::String::NewFromUtf8( \ | |
| 129 | 133 | (env)->isolate, (str), v8::NewStringType::kInternalized, \ | |
| 130 | 134 | static_cast<int>(len)); \ | |
@@ -866,7 +870,7 @@ void napi_module_register(napi_module* mod) { | |||
| 866 | 870 | ||
| 867 | 871 | // Warning: Keep in-sync with napi_status enum | |
| 868 | 872 | const char* error_messages[] = {nullptr, | |
| 869 | - "Invalid pointer passed as argument", | ||
| 873 | + "Invalid argument", | ||
| 870 | 874 | "An object was expected", | |
| 871 | 875 | "A string was expected", | |
| 872 | 876 | "A string or symbol was expected", | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -69,3 +69,7 @@ assert.strictEqual(test_string.TestUtf8Insufficient(str6), str6.slice(0, 1)); | |||
| 69 | 69 | assert.strictEqual(test_string.TestUtf16Insufficient(str6), str6.slice(0, 3)); | |
| 70 | 70 | assert.strictEqual(test_string.Utf16Length(str6), 5); | |
| 71 | 71 | assert.strictEqual(test_string.Utf8Length(str6), 14); | |
| 72 | + | ||
| 73 | + assert.throws(() => { | ||
| 74 | + test_string.TestLargeUtf8(); | ||
| 75 | + }, /^Error: Invalid argument$/); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,4 @@ | |||
| 1 | + #include <limits.h> // INT_MAX | ||
| 1 | 2 | #include <node_api.h> | |
| 2 | 3 | #include "../common.h" | |
| 3 | 4 | ||
@@ -201,6 +202,19 @@ napi_value Utf8Length(napi_env env, napi_callback_info info) { | |||
| 201 | 202 | return output; | |
| 202 | 203 | } | |
| 203 | 204 | ||
| 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 | + | ||
| 204 | 218 | napi_value Init(napi_env env, napi_value exports) { | |
| 205 | 219 | napi_property_descriptor properties[] = { | |
| 206 | 220 | DECLARE_NAPI_PROPERTY("TestLatin1", TestLatin1), | |
@@ -211,6 +225,7 @@ napi_value Init(napi_env env, napi_value exports) { | |||
| 211 | 225 | DECLARE_NAPI_PROPERTY("TestUtf16Insufficient", TestUtf16Insufficient), | |
| 212 | 226 | DECLARE_NAPI_PROPERTY("Utf16Length", Utf16Length), | |
| 213 | 227 | DECLARE_NAPI_PROPERTY("Utf8Length", Utf8Length), | |
| 228 | + DECLARE_NAPI_PROPERTY("TestLargeUtf8", TestLargeUtf8), | ||
| 214 | 229 | }; | |
| 215 | 230 | ||
| 216 | 231 | NAPI_CALL(env, napi_define_properties( | |
| Back | FazBrowse Home | New Git URL |
0 commit comments