From 11d5229cdb4dba073fc41a4e7b76a83a4acec330 Mon Sep 17 00:00:00 2001 From: linzuoquan Date: Wed, 10 Sep 2025 12:04:35 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4napi=E5=88=9B=E5=BB=BAstring?= =?UTF-8?q?=E7=9A=84=E9=95=BF=E5=BA=A6=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: linzuoquan --- component_ext/ext_common/ext_napi_utils.cpp | 3 +-- .../components_ng/common_napi_utils/common_napi_utils.cpp | 3 +-- test/unittest/interfaces/napi/napi_utils_test.cpp | 4 ++++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/component_ext/ext_common/ext_napi_utils.cpp b/component_ext/ext_common/ext_napi_utils.cpp index e2f9d647819..f14736dd0ff 100644 --- a/component_ext/ext_common/ext_napi_utils.cpp +++ b/component_ext/ext_common/ext_napi_utils.cpp @@ -166,7 +166,6 @@ napi_valuetype ExtNapiUtils::GetValueType(napi_env env, napi_value value) std::string ExtNapiUtils::GetStringFromValueUtf8(napi_env env, napi_value value) { - static constexpr size_t max_length = 2048; if (GetValueType(env, value) != napi_string) { return {}; } @@ -174,7 +173,7 @@ std::string ExtNapiUtils::GetStringFromValueUtf8(napi_env env, napi_value value) std::string result; size_t stringLength = 0; NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, value, nullptr, 0, &stringLength), result); - if (stringLength == 0 || stringLength > max_length) { + if (stringLength == 0) { return result; } diff --git a/frameworks/core/components_ng/common_napi_utils/common_napi_utils.cpp b/frameworks/core/components_ng/common_napi_utils/common_napi_utils.cpp index e5e1febc6c3..8ef1db72b5f 100644 --- a/frameworks/core/components_ng/common_napi_utils/common_napi_utils.cpp +++ b/frameworks/core/components_ng/common_napi_utils/common_napi_utils.cpp @@ -155,7 +155,6 @@ napi_value CommonNapiUtils::CreateStringUtf8(napi_env env, const std::string& st std::string CommonNapiUtils::GetStringFromValueUtf8(napi_env env, napi_value value) { - static constexpr size_t max_length = 2048; if (GetValueType(env, value) != napi_string) { return {}; } @@ -163,7 +162,7 @@ std::string CommonNapiUtils::GetStringFromValueUtf8(napi_env env, napi_value val std::string result; size_t stringLength = 0; NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, value, nullptr, 0, &stringLength), result); - if (stringLength == 0 || stringLength > max_length) { + if (stringLength == 0) { return result; } diff --git a/test/unittest/interfaces/napi/napi_utils_test.cpp b/test/unittest/interfaces/napi/napi_utils_test.cpp index dcd9abd0e3e..1af8636d76f 100644 --- a/test/unittest/interfaces/napi/napi_utils_test.cpp +++ b/test/unittest/interfaces/napi/napi_utils_test.cpp @@ -409,6 +409,10 @@ HWTEST_F(NapiUtilsTest, NapiUtilsTest007, TestSize.Level1) napi_status status = napi_create_string_utf8(napi_env(engine), testStr.c_str(), testStr.length(), &napiTestStr); EXPECT_EQ(status, napi_ok); + // 100000 indicates that the length of a long string is too long + status = napi_create_string_utf8(napi_env(engine), testStr.c_str(), 100000, &napiTestStr); + EXPECT_EQ(status, napi_ok); + /** * @tc.steps: step2. Call GetStringFromValueUtf8 * @tc.expected: Return value equals raw string -- Gitee