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 ce6631ef61c23e2cb35a71d050aac9a55854757d..87cd5a6f8959477e0f226abed1785d9aa4e31b62 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 bd7a4f89882371aa0ae7e5f4e2cff7b0c17e3a4d..0b8af4d0e548bdb34b42585b334865e5ddc476ba 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