From 045490023080be42e2c1f3cab34632cfadac092d Mon Sep 17 00:00:00 2001 From: baidu_38995579 Date: Sat, 6 Sep 2025 17:17:34 +0800 Subject: [PATCH] Fix array access out of bounds issue 0728 Issue:https://gitee.com/openharmony/arkcompiler_runtime_core/issues/ICWL7S?from=project-issue Signed-off-by: baidu_38995579 --- .../plugins/ets/sdk/native/api/ani_textencoder_helper.cpp | 8 ++++++-- .../@ohos/util/TextEncoder/TextEncoderConstructorTest.ets | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/static_core/plugins/ets/sdk/native/api/ani_textencoder_helper.cpp b/static_core/plugins/ets/sdk/native/api/ani_textencoder_helper.cpp index ce6631ef61..87cd5a6f89 100644 --- a/static_core/plugins/ets/sdk/native/api/ani_textencoder_helper.cpp +++ b/static_core/plugins/ets/sdk/native/api/ani_textencoder_helper.cpp @@ -77,8 +77,6 @@ bool Utf8ToUtf16LEByteCheck(const unsigned char *data, std::u16string &u16Str, s for (size_t i = 0; i < inputSizeBytes;) { // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic) uint8_t c1 = data[i]; - uint8_t c2 = data[i + 1]; - uint8_t c3 = data[i + 2]; // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic) if (IsOneByte(c1)) { i += 1; // 1 : Proceeds 1 byte @@ -101,6 +99,10 @@ bool Utf8ToUtf16LEByteCheck(const unsigned char *data, std::u16string &u16Str, s if (i + TWO_MORE_BYTES_TO_CONSUME >= inputSizeBytes) { return false; } + // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic) + uint8_t c2 = data[i + 1]; + uint8_t c3 = data[i + 2]; + // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic) i += TWO_MORE_BYTES_TO_CONSUME + 1; // CC-OFFNXT(G.FMT.02-CPP) project code style // NOLINTBEGIN(hicpp-signed-bitwise) @@ -115,6 +117,8 @@ bool Utf8ToUtf16LEByteCheck(const unsigned char *data, std::u16string &u16Str, s if (i + ONE_MORE_BYTE_TO_CONSUME >= inputSizeBytes) { return false; } + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) + uint8_t c2 = data[i + 1]; i += ONE_MORE_BYTE_TO_CONSUME + 1; // NOLINTNEXTLINE(hicpp-signed-bitwise) uint32_t codePoint = ((c1 & LOWER_5_BITS_MASK) << UTF8_VALID_BITS) | (c2 & LOWER_6_BITS_MASK); diff --git a/static_core/plugins/ets/tests/ets_sdk/api/@ohos/util/TextEncoder/TextEncoderConstructorTest.ets b/static_core/plugins/ets/tests/ets_sdk/api/@ohos/util/TextEncoder/TextEncoderConstructorTest.ets index bd7a4f8988..0b8af4d0e5 100644 --- a/static_core/plugins/ets/tests/ets_sdk/api/@ohos/util/TextEncoder/TextEncoderConstructorTest.ets +++ b/static_core/plugins/ets/tests/ets_sdk/api/@ohos/util/TextEncoder/TextEncoderConstructorTest.ets @@ -42,6 +42,7 @@ function main() { suite.addTest("test_encoding_gb2312", test_encoding_gb2312); suite.addTest("test_encoding_utf16le", test_encoding_utf16le); suite.addTest("test_encoding_iso88593", test_encoding_iso88593); + suite.addTest("test_encoding_u16le_h", test_encoding_u16le_h); return suite.run() } @@ -230,3 +231,10 @@ function test_encoding_iso88593() { str = enc.encoding; arktest.assertEQ(str, 'IBM866'); } + +function test_encoding_u16le_h() { + let enc = new util.TextEncoder('utf-16le'); + const bytes = enc.encodeInto('H'); + arktest.assertEQ(bytes[0], 72); + arktest.assertEQ(bytes[1], 0); +} \ No newline at end of file -- Gitee