diff --git a/frameworks/ets/ani/include/sts_common.h b/frameworks/ets/ani/include/sts_common.h index f803583f7f38c691e7da76662d926867637011d9..6809c1111745c5cc97c3a85a1951ecbc5c25c9a5 100644 --- a/frameworks/ets/ani/include/sts_common.h +++ b/frameworks/ets/ani/include/sts_common.h @@ -27,7 +27,7 @@ namespace OHOS { namespace NotificationSts { using namespace arkts::ani_signature; -constexpr int32_t STR_MAX_SIZE = 204; +constexpr int32_t STR_MAX_SIZE = 202; constexpr int32_t PROFILE_NAME_SIZE = 202; constexpr int32_t LONG_STR_MAX_SIZE = 1028; constexpr int32_t COMMON_TEXT_SIZE = 3074; diff --git a/frameworks/ets/ani/src/sts_common.cpp b/frameworks/ets/ani/src/sts_common.cpp index bab76c95153dc5b09ef04d87474af5e017af76cb..32d79303e70efcf5569d0f05229f64c6b0a9af48 100644 --- a/frameworks/ets/ani/src/sts_common.cpp +++ b/frameworks/ets/ani/src/sts_common.cpp @@ -25,7 +25,31 @@ constexpr const char* CLASSNAME_INT = "std.core.Int"; constexpr const char* CLASSNAME_LONG = "std.core.Long"; std::string GetResizeStr(std::string instr, int32_t length) { - return instr.length() <= length ? instr : instr.substr(0, length); + if (instr.length() <= length) { + return instr; + } else { + int32_t end_pos = 0; + for (int32_t i = 0; i < instr.size();) { + if ((instr[i] & 0x80) == 0) { // ASCII字符 + i += 1; + } else if ((instr[i] & 0xE0) == 0xC0) { // 2字节字符 + i += 2; + } else if ((instr[i] & 0xF0) == 0xE0) { // 3字节字符 + i += 3; + } else if ((instr[i] & 0xF8) == 0xF0) { // 4字节字符 + i += 4; + } else { + // 无效的UTF-8序列 + i += 1; + } + if (i > length) { + break; + } + end_pos = i; + } + + return instr.substr(0, end_pos); + } } bool IsUndefine(ani_env *env, const ani_object &obj)