From ca1575096d263eb8b68b9aec63c815ec0d08211d Mon Sep 17 00:00:00 2001 From: wuchengwen Date: Wed, 16 Oct 2024 10:46:38 +0800 Subject: [PATCH 1/2] fix:check null pointer before access Signed-off-by: wuchengwen --- .../ndk/src/inputmethod_controller_capi.cpp | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/frameworks/ndk/src/inputmethod_controller_capi.cpp b/frameworks/ndk/src/inputmethod_controller_capi.cpp index 0c0e161d..fd852f84 100644 --- a/frameworks/ndk/src/inputmethod_controller_capi.cpp +++ b/frameworks/ndk/src/inputmethod_controller_capi.cpp @@ -53,7 +53,7 @@ InputMethod_ErrorCode IsValidInputMethodProxy(InputMethod_InputMethodProxy *inpu return IME_ERR_PARAMCHECK; } - if (g_inputMethodProxy->attached == false) { + if (!(g_inputMethodProxy->attached)) { IMSA_HILOGE("g_inputMethodProxy is not attached"); return IME_ERR_DETACHED; } @@ -118,22 +118,8 @@ static int32_t IsValidTextEditorProxy(InputMethod_TextEditorProxy *textEditor) return IME_ERR_OK; } -InputMethod_ErrorCode OH_InputMethodController_Attach(InputMethod_TextEditorProxy *textEditor, - InputMethod_AttachOptions *options, InputMethod_InputMethodProxy **inputMethodProxy) +static TextConfig ConstructTextConfig(const InputMethod_TextConfig& config) { - if ((IsValidTextEditorProxy(textEditor) != IME_ERR_OK) || options == nullptr || inputMethodProxy == nullptr) { - IMSA_HILOGE("invalid parameter"); - return IME_ERR_NULL_POINTER; - } - - InputMethod_ErrorCode errCode = GetInputMethodProxy(textEditor); - if (errCode != IME_ERR_OK) { - return errCode; - } - - InputMethod_TextConfig config; - textEditor->getTextConfigFunc(textEditor, &config); - TextConfig textConfig = { .inputAttribute = { .inputPattern = static_cast(config.inputType), @@ -155,11 +141,34 @@ InputMethod_ErrorCode OH_InputMethodController_Attach(InputMethod_TextEditorProx .height = config.avoidInfo.height, }; + return textConfig; +} + +InputMethod_ErrorCode OH_InputMethodController_Attach(InputMethod_TextEditorProxy *textEditor, + InputMethod_AttachOptions *options, InputMethod_InputMethodProxy **inputMethodProxy) +{ + if ((IsValidTextEditorProxy(textEditor) != IME_ERR_OK) || options == nullptr || inputMethodProxy == nullptr) { + IMSA_HILOGE("invalid parameter"); + return IME_ERR_NULL_POINTER; + } + + InputMethod_ErrorCode errCode = GetInputMethodProxy(textEditor); + if (errCode != IME_ERR_OK) { + return errCode; + } + + InputMethod_TextConfig config; + textEditor->getTextConfigFunc(textEditor, &config); + + auto textConfig = ConstructTextConfig(config); + auto controller = InputMethodController::GetInstance(); OHOS::sptr listener = nullptr; { std::lock_guard guard(g_textEditorProxyMapMutex); - listener = g_inputMethodProxy->listener; + if (g_inputMethodProxy != nullptr) { + listener = g_inputMethodProxy->listener; + } } int32_t err = controller->Attach(listener, options->showKeyboard, textConfig); From 261deee0d8f73fc1e9d8b708b5bc7fc179511543 Mon Sep 17 00:00:00 2001 From: wuchengwen Date: Wed, 16 Oct 2024 16:45:05 +0800 Subject: [PATCH 2/2] fix:warning Signed-off-by: wuchengwen --- frameworks/ndk/include/native_inputmethod_utils.h | 2 +- frameworks/ndk/src/inputmethod_controller_capi.cpp | 6 +++--- frameworks/ndk/src/native_text_changed_listener.cpp | 2 +- .../cpp_test/src/inputmethod_controller_capi_test.cpp | 4 +++- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/frameworks/ndk/include/native_inputmethod_utils.h b/frameworks/ndk/include/native_inputmethod_utils.h index ce8e81f7..44e647f0 100644 --- a/frameworks/ndk/include/native_inputmethod_utils.h +++ b/frameworks/ndk/include/native_inputmethod_utils.h @@ -21,7 +21,7 @@ InputMethod_ErrorCode ErrorCodeConvert(int32_t code); #ifdef __cplusplus extern "C"{ #endif /* __cplusplus */ -InputMethod_ErrorCode IsValidInputMethodProxy(InputMethod_InputMethodProxy *inputMethodProxy); +InputMethod_ErrorCode IsValidInputMethodProxy(const InputMethod_InputMethodProxy *inputMethodProxy); void ClearInputMethodProxy(void); #ifdef __cplusplus } diff --git a/frameworks/ndk/src/inputmethod_controller_capi.cpp b/frameworks/ndk/src/inputmethod_controller_capi.cpp index fd852f84..92022081 100644 --- a/frameworks/ndk/src/inputmethod_controller_capi.cpp +++ b/frameworks/ndk/src/inputmethod_controller_capi.cpp @@ -36,7 +36,7 @@ struct InputMethod_InputMethodProxy { InputMethod_InputMethodProxy *g_inputMethodProxy = nullptr; std::mutex g_textEditorProxyMapMutex; -InputMethod_ErrorCode IsValidInputMethodProxy(InputMethod_InputMethodProxy *inputMethodProxy) +InputMethod_ErrorCode IsValidInputMethodProxy(const InputMethod_InputMethodProxy *inputMethodProxy) { if (inputMethodProxy == nullptr) { IMSA_HILOGE("inputMethodProxy is nullptr"); @@ -118,7 +118,7 @@ static int32_t IsValidTextEditorProxy(InputMethod_TextEditorProxy *textEditor) return IME_ERR_OK; } -static TextConfig ConstructTextConfig(const InputMethod_TextConfig& config) +static TextConfig ConstructTextConfig(const InputMethod_TextConfig &config) { TextConfig textConfig = { .inputAttribute = { @@ -160,7 +160,7 @@ InputMethod_ErrorCode OH_InputMethodController_Attach(InputMethod_TextEditorProx InputMethod_TextConfig config; textEditor->getTextConfigFunc(textEditor, &config); - auto textConfig = ConstructTextConfig(config); + auto textConfig = ConstructTextConfig(config); auto controller = InputMethodController::GetInstance(); OHOS::sptr listener = nullptr; diff --git a/frameworks/ndk/src/native_text_changed_listener.cpp b/frameworks/ndk/src/native_text_changed_listener.cpp index 70ead99c..fdf8c125 100644 --- a/frameworks/ndk/src/native_text_changed_listener.cpp +++ b/frameworks/ndk/src/native_text_changed_listener.cpp @@ -236,7 +236,7 @@ int32_t NativeTextChangedListener::ReceivePrivateCommand( } size_t index = 0; - for (auto &item : privateCommand) { + for (const auto &item : privateCommand) { command[index] = new InputMethod_PrivateCommand(); command[index]->key = item.first; command[index]->value = item.second; 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 4c2ef931..dba8e66a 100644 --- a/test/unittest/cpp_test/src/inputmethod_controller_capi_test.cpp +++ b/test/unittest/cpp_test/src/inputmethod_controller_capi_test.cpp @@ -17,7 +17,7 @@ using namespace testing::ext; class InputMethodControllerCapiTest : public testing::Test { }; - +namespace { /** * @tc.name: TestCursorInfo_001 * @tc.desc: create and destroy TestCursorInfo success @@ -557,6 +557,7 @@ HWTEST_F(InputMethodControllerCapiTest, OH_TextConfig_GetSelection_001, TestSize InputMethod_TextConfig *config = OH_TextConfig_Create(); ASSERT_NE(config, nullptr); ret = OH_TextConfig_GetSelection(config, nullptr, nullptr); + EXPECT_EQ(IME_ERR_NULL_POINTER, ret); int32_t start = 0; ret = OH_TextConfig_GetSelection(config, &start, nullptr); EXPECT_EQ(ret, IME_ERR_NULL_POINTER); @@ -1512,3 +1513,4 @@ HWTEST_F(InputMethodControllerCapiTest, TestAttachWithNorrmalParam_001, TestSize OH_AttachOptions_Destroy(options); OH_TextEditorProxy_Destroy(textEditorProxy); } +} \ No newline at end of file