diff --git a/common/src/itypes_util.cpp b/common/src/itypes_util.cpp index 994a466dd00e6197726cd7410037234286ac54d4..6df768c01bdfc6b77a1c26d52fa501630150de93 100644 --- a/common/src/itypes_util.cpp +++ b/common/src/itypes_util.cpp @@ -560,8 +560,7 @@ bool ITypesUtil::Unmarshalling(RequestKeyboardReason &output, MessageParcel &dat int32_t ITypesUtil::CountUtf16Chars(const std::u16string &in) { int32_t ret = u_countChar32(in.data(), in.size()); - IMSA_HILOGD("size:%{public}zu,ret:%{public}d, data:[%{public}s]", - in.size(), ret, Str16ToStr8(in).c_str()); + IMSA_HILOGD("size:%{public}zu,ret:%{public}d", in.size(), ret); return ret; } } // namespace MiscServices diff --git a/frameworks/native/inputmethod_controller/include/input_attribute.h b/frameworks/native/inputmethod_controller/include/input_attribute.h index 3ce3769c273946cc9046dd843c2b145dcdd275ef..91dbf35acf3bf1ee37edf2628207cb2098db3012 100644 --- a/frameworks/native/inputmethod_controller/include/input_attribute.h +++ b/frameworks/native/inputmethod_controller/include/input_attribute.h @@ -82,12 +82,6 @@ struct InputAttribute { inputOption == info.inputOption && isTextPreviewSupported == info.isTextPreviewSupported; } - inline std::string ToUtf8(const std::u16string &in) const - { - std::wstring_convert, char16_t> converter; - return converter.to_bytes(in); - } - inline std::string ToString() const { std::stringstream ss; @@ -96,7 +90,6 @@ struct InputAttribute { << "isTextPreviewSupported:" << isTextPreviewSupported << "bundleName:" << bundleName << "immersiveMode:" << immersiveMode << "windowId:" << windowId << "callingDisplayId:" << callingDisplayId - << "placeholder:" << ToUtf8(placeholder) << "abilityName:" << ToUtf8(abilityName) << "]"; return ss.str(); } diff --git a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp index c2175d30e1a00782973adb486b9805e92555c5f0..3adf8c28e3352fd47ad4ce649c13fe06d34db607 100644 --- a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp @@ -253,15 +253,13 @@ int32_t InputMethodController::IsValidTextConfig(const TextConfig &textConfig) } int32_t uft16CharLen = ITypesUtil::CountUtf16Chars(textConfig.inputAttribute.placeholder); if (uft16CharLen > MAX_PLACEHOLDER_SIZE) { - IMSA_HILOGE("invalid placeholder:%{public}s over limit num:%{public}d", - Str16ToStr8(textConfig.inputAttribute.placeholder).c_str(), uft16CharLen); + IMSA_HILOGE("invalid placeholder over limit num:%{public}d", uft16CharLen); return ErrorCode::ERROR_PARAMETER_CHECK_FAILED; } uft16CharLen = ITypesUtil::CountUtf16Chars(textConfig.inputAttribute.abilityName); if (uft16CharLen > MAX_ABILITY_NAME_SIZE) { - IMSA_HILOGE("invalid abilityName:%{public}s, over limit num:%{public}d", - Str16ToStr8(textConfig.inputAttribute.abilityName).c_str(), uft16CharLen); + IMSA_HILOGE("invalid abilityName over limit num:%{public}d", uft16CharLen); return ErrorCode::ERROR_PARAMETER_CHECK_FAILED; } return ErrorCode::NO_ERROR; diff --git a/frameworks/ndk/src/inputmethod_text_config_capi.cpp b/frameworks/ndk/src/inputmethod_text_config_capi.cpp index bfdcc8c6a46b5ed83f3873e9efe20476f617c0b7..f7c9d3ea87734ca7e783e168781662e938ee0022 100644 --- a/frameworks/ndk/src/inputmethod_text_config_capi.cpp +++ b/frameworks/ndk/src/inputmethod_text_config_capi.cpp @@ -355,7 +355,7 @@ InputMethod_ErrorCode OH_TextConfig_GetAbilityName(InputMethod_TextConfig *confi config->abilityNameLength = ENDING_SYMBOL_SIZE; config->abilityName[0] = UTF16_ENDING_SYMBOL; } - IMSA_HILOGD("curLen:%{public}zu,inputLen:%{public}zu", config->placeholderLength, *length); + IMSA_HILOGD("curLen:%{public}zu,inputLen:%{public}zu", config->abilityNameLength, *length); if ((*length) < config->abilityNameLength) { IMSA_HILOGE("input memory is less than the length of the obtained memory. actual length:%{public}zu", config->abilityNameLength); diff --git a/interfaces/kits/c/inputmethod_text_config_capi.h b/interfaces/kits/c/inputmethod_text_config_capi.h index 4044f559c80a360f4d40c1ec28ae1fc47d319932..a7e3ac01763650ca33851b6edb4c7af489b08ea4 100644 --- a/interfaces/kits/c/inputmethod_text_config_capi.h +++ b/interfaces/kits/c/inputmethod_text_config_capi.h @@ -265,10 +265,12 @@ InputMethod_ErrorCode OH_TextConfig_GetWindowId(InputMethod_TextConfig *config, /** * @brief Get placeholder into TextConfig. * - * @param config Represents a pointer to an {@link InputMethod_TextConfig} instance which will be set. + * @param config Represents a pointer to an {@link InputMethod_TextConfig} instance which will be get from. * @param placeholder Returns the placeholder. which is defined in {@link InputMethod_TextConfig}. * The user needs to allocate memory. * @param length Input and output parameters. The input is the size requested by the user (unit: char16_t). + * Check the validity of the memory requested by the user based on the input length. + * If the value is smaller than the actual size, the actual length is returned. * The length, which is defined in {@link InputMethod_TextConfig}. * @return Returns a specific error code. * {@link IME_ERR_OK} - success. @@ -283,11 +285,12 @@ InputMethod_ErrorCode OH_TextConfig_GetPlaceholder(InputMethod_TextConfig *confi /** * @brief Get abilityName into TextConfig. * - * @param config Represents a pointer to an {@link InputMethod_TextConfig} instance which will be set. + * @param config Represents a pointer to an {@link InputMethod_TextConfig} instance which will be get from. * @param abilityName The abilityName, which is defined in {@link InputMethod_TextConfig}. * The free function needs to be called externally to release the internal requested memory. * @param length Input and output parameters. The input is the size requested by the user (unit: char16_t), - * returns the size of the ability name. + * Check the validity of the memory requested by the user based on the input length. + * If the value is smaller than the actual size, the actual length is returned. * The length, which is defined in {@link InputMethod_TextConfig}. * @return Returns a specific error code. * {@link IME_ERR_OK} - success. diff --git a/test/unittest/cpp_test/src/inputmethod_controller_capi_test.cpp b/test/unittest/cpp_test/src/inputmethod_controller_capi_test.cpp index 02086db225f3e5a93e921e11337130fac3382698..9bd3467bcf14c8c3143d519f4545e84b75bc3ef6 100644 --- a/test/unittest/cpp_test/src/inputmethod_controller_capi_test.cpp +++ b/test/unittest/cpp_test/src/inputmethod_controller_capi_test.cpp @@ -1582,10 +1582,7 @@ HWTEST_F(InputMethodControllerCapiTest, OH_TextConfig_SetPlaceholder_001, TestSi ret = OH_TextConfig_GetPlaceholder(config, nullptr, nullptr); EXPECT_EQ(ret, IME_ERR_NULL_POINTER); size_t outLen = 513; - char16_t *pOut = new (std::nothrow) char16_t[outLen]; - if (pOut == nullptr) { - return; - } + char16_t pOut[513] = {}; ret = OH_TextConfig_GetPlaceholder(config, pOut, nullptr); EXPECT_EQ(ret, IME_ERR_NULL_POINTER); outLen = 1; @@ -1600,16 +1597,10 @@ HWTEST_F(InputMethodControllerCapiTest, OH_TextConfig_SetPlaceholder_001, TestSi ret = OH_TextConfig_GetPlaceholder(config, pOut, &outLen); EXPECT_EQ(ret, IME_ERR_PARAMCHECK); std::u16string out(pOut, outLen); - EXPECT_EQ(out.compare(input), 1); - delete []pOut; - outLen = input.size() -1; - pOut = new (std::nothrow) char16_t[outLen]; - if (pOut == nullptr) { - return; - } + EXPECT_GT(out.size(), input.size()); + outLen = 4; ret = OH_TextConfig_GetPlaceholder(config, pOut, &outLen); EXPECT_EQ(ret, IME_ERR_PARAMCHECK); - delete []pOut; OH_TextConfig_Destroy(config); } @@ -1627,14 +1618,10 @@ HWTEST_F(InputMethodControllerCapiTest, OH_TextConfig_SetPlaceholder_002, TestSi ret = OH_TextConfig_SetPlaceholder(config, nullptr, 0); EXPECT_EQ(ret, IME_ERR_OK); size_t outLen = 512; - char16_t *pOut = new (std::nothrow) char16_t[outLen]; - if (pOut == nullptr) { - return; - } + char16_t pOut[512] = {}; ret = OH_TextConfig_GetPlaceholder(config, pOut, &outLen); EXPECT_EQ(ret, IME_ERR_OK); EXPECT_EQ(outLen, 1); - delete []pOut; OH_TextConfig_Destroy(config); } @@ -1659,16 +1646,12 @@ HWTEST_F(InputMethodControllerCapiTest, OH_TextConfig_SetPlaceholder_003, TestSi IMSA_HILOGI("inputLen:%{public}zu,input:%{public}s", input.size(), Str16ToStr8(input).c_str()); ret = OH_TextConfig_SetPlaceholder(config, input.data(), input.size()); EXPECT_EQ(ret, IME_ERR_OK); - size_t outLen = input.size() + 1; - char16_t *pOut = new (std::nothrow) char16_t[outLen]; - if (pOut == nullptr) { - return; - } + size_t outLen = 513; + char16_t pOut[513] = {}; ret = OH_TextConfig_GetPlaceholder(config, pOut, &outLen); EXPECT_EQ(ret, IME_ERR_OK); std::u16string out(pOut, outLen); - EXPECT_EQ(out.compare(input), 1); - delete []pOut; + EXPECT_GT(out.size(), input.size()); input.append(u"a"); IMSA_HILOGI("inputLen:%{public}zu,input:%{public}s", input.size(), Str16ToStr8(input).c_str()); ret = OH_TextConfig_SetPlaceholder(config, input.data(), input.size()); @@ -1693,10 +1676,7 @@ HWTEST_F(InputMethodControllerCapiTest, OH_TextConfig_SetAbilityName_001, TestSi ret = OH_TextConfig_GetAbilityName(config, nullptr, nullptr); EXPECT_EQ(ret, IME_ERR_NULL_POINTER); size_t outLen = 66; - char16_t *pOut = new (std::nothrow) char16_t[outLen]; - if (pOut == nullptr) { - return; - } + char16_t pOut[66] = {}; ret = OH_TextConfig_GetAbilityName(config, pOut, nullptr); EXPECT_EQ(ret, IME_ERR_NULL_POINTER); outLen = 1; @@ -1713,7 +1693,7 @@ HWTEST_F(InputMethodControllerCapiTest, OH_TextConfig_SetAbilityName_001, TestSi std::u16string out(pOut, outLen); IMSA_HILOGI("outLen:%{public}zu,out:%{public}s,outSize:%{public}zu", outLen, Str16ToStr8(out).c_str(), out.size()); - EXPECT_EQ(out.compare(input), 1); + EXPECT_GT(out.size(), input.size()); outLen = input.size(); ret = OH_TextConfig_GetAbilityName(config, pOut, &outLen); EXPECT_EQ(ret, IME_ERR_PARAMCHECK); @@ -1754,27 +1734,39 @@ HWTEST_F(InputMethodControllerCapiTest, OH_TextConfig_SetAbilityName_003, TestSi EXPECT_EQ(ret, IME_ERR_OK); ret = OH_TextConfig_SetAbilityName(config, nullptr, 1); EXPECT_EQ(ret, IME_ERR_OK); + char16_t input[] = u"0"; + ret = OH_TextConfig_SetAbilityName(config, input, 0); + EXPECT_EQ(ret, IME_ERR_OK); + ret = OH_TextConfig_SetAbilityName(config, input, 1); + EXPECT_EQ(ret, IME_ERR_OK); ret = OH_TextConfig_SetAbilityName(nullptr, nullptr, 1); EXPECT_EQ(ret, IME_ERR_NULL_POINTER); + OH_TextConfig_Destroy(config); +} + +/** + * @tc.name: OH_TextConfig_SetAbilityName_004 + * @tc.desc: Invalid test input parameter + * @tc.type: FUNC + */ +HWTEST_F(InputMethodControllerCapiTest, OH_TextConfig_SetAbilityName_004, TestSize.Level0) { + auto config = OH_TextConfig_Create(); + ASSERT_NE(nullptr, config); std::u16string input = u""; for (int i = 0; i < 32; ++i) { input.append(u"𪛊"); } IMSA_HILOGI("inputLen:%{public}zu,input:%{public}s", input.size(), Str16ToStr8(input).c_str()); - ret = OH_TextConfig_SetAbilityName(config, input.data(), input.size()); + auto ret = OH_TextConfig_SetAbilityName(config, input.data(), input.size()); EXPECT_EQ(ret, IME_ERR_OK); - size_t outLen = input.size() + 1; - char16_t *pOut = new (std::nothrow) char16_t[outLen]; - if (pOut == nullptr) { - return; - } + size_t outLen = 65; + char16_t pOut[65] = {}; ret = OH_TextConfig_GetAbilityName(config, pOut, &outLen); EXPECT_EQ(ret, IME_ERR_OK); std::u16string out(pOut, outLen); IMSA_HILOGI("outLen:%{public}zu,input:%{public}s,outSize:%{public}zu,inputSize:%{public}zu", outLen, Str16ToStr8(input).c_str(), out.size(), input.size()); - EXPECT_EQ(out.compare(input), 1); - delete []pOut; + EXPECT_GT(out.size(), input.size()); input.append(u"a"); IMSA_HILOGI("inputLen:%{public}zu,input:%{public}s", input.size(), Str16ToStr8(input).c_str()); ret = OH_TextConfig_SetAbilityName(config, input.data(), input.size()); @@ -1784,17 +1776,13 @@ HWTEST_F(InputMethodControllerCapiTest, OH_TextConfig_SetAbilityName_003, TestSi IMSA_HILOGI("inputLen:%{public}zu,input:%{public}s", charInputLen, Str16ToStr8(input).c_str()); ret = OH_TextConfig_SetAbilityName(config, charInput, charInputLen); EXPECT_EQ(ret, IME_ERR_OK); - pOut = new char16_t[66]; - if (pOut == nullptr) { - return; - } + char16_t outChar[66] = {}; outLen = 33; - ret = OH_TextConfig_GetAbilityName(config, pOut, &outLen); + ret = OH_TextConfig_GetAbilityName(config, outChar, &outLen); EXPECT_EQ(ret, IME_ERR_OK); - out = std::u16string(pOut, outLen); - auto utf8Out = Str16ToStr8(pOut); + out = std::u16string(outChar, outLen); + auto utf8Out = Str16ToStr8(outChar); IMSA_HILOGI("outLen:%{public}zu,out:%{public}s, utf8len:%{public}zu", outLen, utf8Out.c_str(), utf8Out.size()); - delete []pOut; OH_TextConfig_Destroy(config); } } // namespace \ No newline at end of file