From 65a10bfe768a7d7a52997d1da91d191b9dc8e759 Mon Sep 17 00:00:00 2001 From: blc Date: Sat, 26 Jul 2025 20:49:00 +0800 Subject: [PATCH] :wqfix nullptr Signed-off-by: blc --- .../cj_input_method_textchanged_listener.cpp | 71 +++++++++++++--- frameworks/cj/src/input_method_ffi.cpp | 2 +- frameworks/cj/src/utils.cpp | 8 ++ .../js/napi/common/js_callback_handler.cpp | 4 + .../js/napi/common/js_callback_handler.h | 3 + .../js_input_method_engine_setting.cpp | 3 + .../js_keyboard_delegate_setting.cpp | 3 + .../js_text_input_client_engine.cpp | 3 + .../js/napi/inputmethodclient/async_call.cpp | 8 ++ .../js_get_input_method_controller.cpp | 3 + .../js_get_input_method_setting.cpp | 3 + ...s_get_input_method_textchange_listener.cpp | 85 ++++++++++++++++--- .../inputmethodclient/js_input_method.cpp | 8 ++ .../js_keyboard_panel_manager.cpp | 46 +++++++++- .../src/inputmethod_extension_context.cpp | 37 ++++++-- .../src/js_inputmethod_extension.cpp | 19 ++++- .../src/system_cmd_channel_service_impl.cpp | 12 ++- .../keyboard/src/input_event_callback.cpp | 4 + .../adapter/keyboard/src/keyboard_event.cpp | 13 ++- .../src/identity_checker_impl.cpp | 7 +- services/src/input_method_system_ability.cpp | 77 ++++++++++++++--- services/src/peruser_session.cpp | 24 +++++- .../cpp_test/src/ime_system_channel_test.cpp | 22 +++++ .../src/input_method_ability_test.cpp | 34 ++++++++ .../src/input_method_controller_test.cpp | 40 +++++++++ 25 files changed, 486 insertions(+), 53 deletions(-) diff --git a/frameworks/cj/src/cj_input_method_textchanged_listener.cpp b/frameworks/cj/src/cj_input_method_textchanged_listener.cpp index 0e7d605ec..b80f339d5 100644 --- a/frameworks/cj/src/cj_input_method_textchanged_listener.cpp +++ b/frameworks/cj/src/cj_input_method_textchanged_listener.cpp @@ -32,52 +32,103 @@ sptr CjInputMethodTextChangedListener::GetInst void CjInputMethodTextChangedListener::InsertText(const std::u16string &text) { - CjInputMethodController::GetInstance()->InsertText(text); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->InsertText(text); } void CjInputMethodTextChangedListener::DeleteForward(int32_t length) { - CjInputMethodController::GetInstance()->DeleteRight(length); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->DeleteRight(length); } void CjInputMethodTextChangedListener::DeleteBackward(int32_t length) { - CjInputMethodController::GetInstance()->DeleteLeft(length); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->DeleteLeft(length); } void CjInputMethodTextChangedListener::SendKeyboardStatus(const KeyboardStatus &status) { - CjInputMethodController::GetInstance()->SendKeyboardStatus(status); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->SendKeyboardStatus(status); } void CjInputMethodTextChangedListener::SendFunctionKey(const FunctionKey &functionKey) { - CjInputMethodController::GetInstance()->SendFunctionKey(functionKey); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->SendFunctionKey(functionKey); } void CjInputMethodTextChangedListener::MoveCursor(const Direction direction) { - CjInputMethodController::GetInstance()->MoveCursor(direction); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->MoveCursor(direction); } void CjInputMethodTextChangedListener::HandleExtendAction(int32_t action) { - CjInputMethodController::GetInstance()->HandleExtendAction(action); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->HandleExtendAction(action); } std::u16string CjInputMethodTextChangedListener::GetLeftTextOfCursor(int32_t number) { - return CjInputMethodController::GetInstance()->GetLeftText(number); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return std::u16string(); + } + return controller->GetLeftText(number); } std::u16string CjInputMethodTextChangedListener::GetRightTextOfCursor(int32_t number) { - return CjInputMethodController::GetInstance()->GetRightText(number); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return std::u16string(); + } + return controller->GetRightText(number); } int32_t CjInputMethodTextChangedListener::GetTextIndexAtCursor() { - return CjInputMethodController::GetInstance()->GetTextIndexAtCursor(); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + int32_t index = -1; + return index; + } + return controller->GetTextIndexAtCursor(); } int32_t CjInputMethodTextChangedListener::ReceivePrivateCommand( diff --git a/frameworks/cj/src/input_method_ffi.cpp b/frameworks/cj/src/input_method_ffi.cpp index 9016c2878..46cfe7a8e 100644 --- a/frameworks/cj/src/input_method_ffi.cpp +++ b/frameworks/cj/src/input_method_ffi.cpp @@ -125,7 +125,7 @@ int32_t FfiInputMethodGetSystemInputMethodConfigAbility(CElementName *elem) return ERR_NO_MEMORY; } int32_t ret = ctrl->GetInputMethodConfig(inputMethodConfig); - if (ret == ErrorCode::NO_ERROR) { + if (ret == ErrorCode::NO_ERROR && elem != nullptr) { elem->deviceId = Utils::MallocCString(inputMethodConfig.GetDeviceID()); elem->bundleName = Utils::MallocCString(inputMethodConfig.GetBundleName()); elem->abilityName = Utils::MallocCString(inputMethodConfig.GetAbilityName()); diff --git a/frameworks/cj/src/utils.cpp b/frameworks/cj/src/utils.cpp index 7eccb76a0..76dd91e43 100644 --- a/frameworks/cj/src/utils.cpp +++ b/frameworks/cj/src/utils.cpp @@ -103,6 +103,10 @@ char* Utils::MallocCString(const std::string &origin) void Utils::InputMethodProperty2C(CInputMethodProperty *props, const Property &property) { + if (props == nullptr) { + IMSA_HILOGE("props is nullptr."); + return; + } props->name = Utils::MallocCString(property.name); props->id = Utils::MallocCString(property.id); props->label = Utils::MallocCString(property.label); @@ -125,6 +129,10 @@ Property Utils::C2InputMethodProperty(CInputMethodProperty props) void Utils::InputMethodSubProperty2C(CInputMethodSubtype *props, const SubProperty &property) { + if (props == nullptr) { + IMSA_HILOGE("props is nullptr."); + return; + } props->name = Utils::MallocCString(property.name); props->id = Utils::MallocCString(property.id); props->label = Utils::MallocCString(property.label); diff --git a/frameworks/js/napi/common/js_callback_handler.cpp b/frameworks/js/napi/common/js_callback_handler.cpp index 94b81e861..6fd95364b 100644 --- a/frameworks/js/napi/common/js_callback_handler.cpp +++ b/frameworks/js/napi/common/js_callback_handler.cpp @@ -23,6 +23,10 @@ constexpr size_t MAX_ARGV_COUNT = 10; void JsCallbackHandler::Execute(const std::shared_ptr &object, const ArgContainer &argContainer, napi_value &output) { + if (object == nullptr) { + IMSA_HILOGE("object is nullptr!"); + return; + } if (object->threadId_ != std::this_thread::get_id()) { IMSA_HILOGW("threadId not same!"); return; diff --git a/frameworks/js/napi/common/js_callback_handler.h b/frameworks/js/napi/common/js_callback_handler.h index 95a245a8e..241e2d911 100644 --- a/frameworks/js/napi/common/js_callback_handler.h +++ b/frameworks/js/napi/common/js_callback_handler.h @@ -51,6 +51,9 @@ public: { InputMethodSyncTrace tracer("Traverse callback with output"); for (const auto &object : objects) { + if (object == nullptr) { + continue; + } JsUtil::ScopeGuard scopeGuard(object->env_); napi_value jsOutput = nullptr; Execute(object, argContainer, jsOutput); diff --git a/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp index 49224328d..bd56f0025 100644 --- a/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp @@ -375,6 +375,9 @@ void JsInputMethodEngineSetting::RegisterListener(napi_value callback, std::stri } auto callbacks = jsCbMap_[type]; bool ret = std::any_of(callbacks.begin(), callbacks.end(), [&callback](std::shared_ptr cb) { + if (cb == nullptr) { + return false; + } return JsUtils::Equals(cb->env_, callback, cb->callback_, cb->threadId_); }); if (ret) { diff --git a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp index 6a5da5bcf..df4a5d71c 100644 --- a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp @@ -177,6 +177,9 @@ void JsKeyboardDelegateSetting::RegisterListener(napi_value callback, std::strin } auto callbacks = jsCbMap_[type]; bool ret = std::any_of(callbacks.begin(), callbacks.end(), [&callback](std::shared_ptr cb) { + if (cb == nullptr) { + return false; + } return JsUtils::Equals(cb->env_, callback, cb->callback_, cb->threadId_); }); if (ret) { diff --git a/frameworks/js/napi/inputmethodability/js_text_input_client_engine.cpp b/frameworks/js/napi/inputmethodability/js_text_input_client_engine.cpp index f10a65a73..0bac05f5a 100644 --- a/frameworks/js/napi/inputmethodability/js_text_input_client_engine.cpp +++ b/frameworks/js/napi/inputmethodability/js_text_input_client_engine.cpp @@ -1062,6 +1062,9 @@ void JsTextInputClientEngine::RegisterListener(napi_value callback, std::string } auto callbacks = jsCbMap_[type]; bool ret = std::any_of(callbacks.begin(), callbacks.end(), [&callback](std::shared_ptr cb) { + if (cb == nullptr) { + return false; + } return JsUtils::Equals(cb->env_, callback, cb->callback_, cb->threadId_); }); if (ret) { diff --git a/frameworks/js/napi/inputmethodclient/async_call.cpp b/frameworks/js/napi/inputmethodclient/async_call.cpp index 9cb216c17..a29ad8b41 100644 --- a/frameworks/js/napi/inputmethodclient/async_call.cpp +++ b/frameworks/js/napi/inputmethodclient/async_call.cpp @@ -47,6 +47,10 @@ AsyncCall::AsyncCall(napi_env env, napi_callback_info info, std::shared_ptrctx = std::move(context); napi_create_reference(env, self, 1, &context_->self); @@ -203,6 +207,10 @@ void AsyncCall::OnComplete(napi_env env, napi_status status, void *data) void AsyncCall::DeleteContext(napi_env env, AsyncContext *context) { + if (context == nullptr) { + IMSA_HILOGE("context is nullptr!"); + return; + } if (env != nullptr) { napi_delete_reference(env, context->callback); napi_delete_reference(env, context->self); diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp index abb5dba04..58e99f28c 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -383,6 +383,9 @@ void JsGetInputMethodController::RegisterListener(napi_value callback, std::stri auto callbacks = jsCbMap_[type]; bool ret = std::any_of(callbacks.begin(), callbacks.end(), [&callback](std::shared_ptr cb) { + if (cb == nullptr) { + return false; + } return JsUtils::Equals(cb->env_, callback, cb->callback_, cb->threadId_); }); if (ret) { diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp index 4b6701856..010f3e381 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -574,6 +574,9 @@ int32_t JsGetInputMethodSetting::RegisterListener(napi_value callback, std::stri auto callbacks = jsCbMap_[type]; bool ret = std::any_of(callbacks.begin(), callbacks.end(), [&callback](std::shared_ptr cb) { + if (cb == nullptr) { + return false; + } return JsUtils::Equals(cb->env_, callback, cb->callback_, cb->threadId_); }); if (ret) { diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_textchange_listener.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_textchange_listener.cpp index b55beb6d7..59742babf 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_textchange_listener.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_textchange_listener.cpp @@ -39,52 +39,103 @@ sptr JsGetInputMethodTextChangedListener::G void JsGetInputMethodTextChangedListener::InsertText(const std::u16string &text) { - JsGetInputMethodController::GetInstance()->InsertText(text); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->InsertText(text); } void JsGetInputMethodTextChangedListener::DeleteForward(int32_t length) { - JsGetInputMethodController::GetInstance()->DeleteRight(length); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->DeleteRight(length); } void JsGetInputMethodTextChangedListener::DeleteBackward(int32_t length) { - JsGetInputMethodController::GetInstance()->DeleteLeft(length); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->DeleteLeft(length); } void JsGetInputMethodTextChangedListener::SendKeyboardStatus(const KeyboardStatus &status) { - JsGetInputMethodController::GetInstance()->SendKeyboardStatus(status); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->SendKeyboardStatus(status); } void JsGetInputMethodTextChangedListener::SendFunctionKey(const FunctionKey &functionKey) { - JsGetInputMethodController::GetInstance()->SendFunctionKey(functionKey); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->SendFunctionKey(functionKey); } void JsGetInputMethodTextChangedListener::MoveCursor(const Direction direction) { - JsGetInputMethodController::GetInstance()->MoveCursor(direction); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->MoveCursor(direction); } void JsGetInputMethodTextChangedListener::HandleExtendAction(int32_t action) { - JsGetInputMethodController::GetInstance()->HandleExtendAction(action); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->HandleExtendAction(action); } std::u16string JsGetInputMethodTextChangedListener::GetLeftTextOfCursor(int32_t number) { - return JsGetInputMethodController::GetInstance()->GetText("getLeftTextOfCursor", number); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return std::u16string(); + } + return controller->GetText("getLeftTextOfCursor", number); } std::u16string JsGetInputMethodTextChangedListener::GetRightTextOfCursor(int32_t number) { - return JsGetInputMethodController::GetInstance()->GetText("getRightTextOfCursor", number); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return std::u16string(); + } + return controller->GetText("getRightTextOfCursor", number); } int32_t JsGetInputMethodTextChangedListener::GetTextIndexAtCursor() { - return JsGetInputMethodController::GetInstance()->GetTextIndexAtCursor(); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + int32_t index = -1; + return index; + } + return controller->GetTextIndexAtCursor(); } int32_t JsGetInputMethodTextChangedListener::ReceivePrivateCommand( @@ -100,12 +151,22 @@ bool JsGetInputMethodTextChangedListener::IsFromTs() int32_t JsGetInputMethodTextChangedListener::SetPreviewText(const std::u16string &text, const Range &range) { - return JsGetInputMethodController::GetInstance()->SetPreviewText(text, range); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return ErrorCode::ERROR_NULL_POINTER; + } + return controller->SetPreviewText(text, range); } void JsGetInputMethodTextChangedListener::FinishTextPreview() { - return JsGetInputMethodController::GetInstance()->FinishTextPreview(); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + return controller->FinishTextPreview(); } std::shared_ptr JsGetInputMethodTextChangedListener::GetEventHandler() diff --git a/frameworks/js/napi/inputmethodclient/js_input_method.cpp b/frameworks/js/napi/inputmethodclient/js_input_method.cpp index 50ba1a670..1a97537fd 100644 --- a/frameworks/js/napi/inputmethodclient/js_input_method.cpp +++ b/frameworks/js/napi/inputmethodclient/js_input_method.cpp @@ -56,6 +56,10 @@ napi_status JsInputMethod::GetInputMethodProperty(napi_env env, napi_value argv, } napi_value result = nullptr; napi_get_named_property(env, argv, "name", &result); + if (ctxt == nullptr) { + IMSA_HILOGE("ctxt is nullptr!"); + return status; + } status = JsUtils::GetValue(env, result, ctxt->packageName); CHECK_RETURN(status == napi_ok, "get name failed!", status); result = nullptr; @@ -89,6 +93,10 @@ napi_status JsInputMethod::GetInputMethodSubProperty(napi_env env, napi_value ar napi_value result = nullptr; status = napi_get_named_property(env, argv, "name", &result); PARAM_CHECK_RETURN(env, status == napi_ok, " name ", TYPE_STRING, status); + if (ctxt == nullptr) { + IMSA_HILOGE("ctxt is nullptr!"); + return status; + } status = JsUtils::GetValue(env, result, ctxt->name); CHECK_RETURN(status == napi_ok, "get name failed!", status); result = nullptr; diff --git a/frameworks/js/napi/keyboardpanelmanager/js_keyboard_panel_manager.cpp b/frameworks/js/napi/keyboardpanelmanager/js_keyboard_panel_manager.cpp index 1cb5c03ba..fc786bf16 100644 --- a/frameworks/js/napi/keyboardpanelmanager/js_keyboard_panel_manager.cpp +++ b/frameworks/js/napi/keyboardpanelmanager/js_keyboard_panel_manager.cpp @@ -76,8 +76,19 @@ napi_value JsKeyboardPanelManager::ConnectSystemCmd(napi_env env, napi_callback_ { auto ctxt = std::make_shared(); auto manager = JsKeyboardPanelManager::GetInstance(); + if (manager == nullptr) { + IMSA_HILOGE("manager is nullptr!"); + return nullptr; + } auto exec = [ctxt, env, manager](AsyncCall::Context *ctx) { - auto ret = ImeSystemCmdChannel::GetInstance()->ConnectSystemCmd(manager); + auto channel = ImeSystemCmdChannel::GetInstance(); + if (channel == nullptr) { + ctxt->SetErrorCode(ErrorCode::ERROR_NULL_POINTER); + ctxt->SetState(napi_generic_failure); + IMSA_HILOGE("channel is nullptr!"); + return; + } + auto ret = channel->ConnectSystemCmd(manager); ctxt->SetErrorCode(ret); CHECK_RETURN_VOID(ret == ErrorCode::NO_ERROR, "ConnectSystemCmd return error!"); ctxt->SetState(napi_ok); @@ -103,6 +114,10 @@ napi_value JsKeyboardPanelManager::Subscribe(napi_env env, napi_callback_info in return nullptr; } auto manager = JsKeyboardPanelManager::GetInstance(); + if (manager == nullptr) { + IMSA_HILOGE("manager is nullptr!"); + return nullptr; + } IMSA_HILOGD("subscribe type: %{public}s.", type.c_str()); std::shared_ptr callback = std::make_shared(env, argv[1], std::this_thread::get_id(), @@ -126,6 +141,10 @@ napi_value JsKeyboardPanelManager::UnSubscribe(napi_env env, napi_callback_info return nullptr; } auto manager = JsKeyboardPanelManager::GetInstance(); + if (manager == nullptr) { + IMSA_HILOGE("manager is nullptr!"); + return nullptr; + } // if the second param is not napi_function/napi_null/napi_undefined, return auto paramType = JsUtil::GetType(env, argv[1]); if (paramType != napi_function && paramType != napi_null && paramType != napi_undefined) { @@ -149,6 +168,9 @@ void JsKeyboardPanelManager::RegisterListener(napi_value callback, std::string t } auto callbacks = jsCbMap_[type]; bool ret = std::any_of(callbacks.begin(), callbacks.end(), [&callback](std::shared_ptr cb) { + if (cb == nullptr) { + return false; + } return JsUtils::Equals(cb->env_, callback, cb->callback_, cb->threadId_); }); if (ret) { @@ -195,7 +217,14 @@ napi_value JsKeyboardPanelManager::GetSmartMenuCfg(napi_env env, napi_callback_i return napi_ok; }; auto exec = [ctxt](AsyncCall::Context *ctx) { - ctxt->smartMenu = ImeSystemCmdChannel::GetInstance()->GetSmartMenuCfg(); + auto channel = ImeSystemCmdChannel::GetInstance(); + if (channel == nullptr) { + ctxt->SetState(napi_generic_failure); + ctxt->smartMenu = {}; + ctxt->SetErrorCode(ErrorCode::ERROR_NULL_POINTER); + return; + } + ctxt->smartMenu = channel->GetSmartMenuCfg(); ctxt->SetState(napi_ok); }; ctxt->SetAction(nullptr, std::move(output)); @@ -222,7 +251,12 @@ napi_value JsKeyboardPanelManager::SendPrivateCommand(napi_env env, napi_callbac auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { return napi_ok; }; auto exec = [ctxt](AsyncCall::Context *ctx) { privateCommandQueue_.Wait(ctxt->info); - int32_t code = ImeSystemCmdChannel::GetInstance()->SendPrivateCommand(ctxt->privateCommand); + auto channel = ImeSystemCmdChannel::GetInstance(); + if (channel == nullptr) { + ctxt->SetState(napi_generic_failure); + return; + } + int32_t code = channel->SendPrivateCommand(ctxt->privateCommand); privateCommandQueue_.Pop(); if (code == ErrorCode::NO_ERROR) { ctxt->SetState(napi_ok); @@ -239,7 +273,11 @@ napi_value JsKeyboardPanelManager::SendPrivateCommand(napi_env env, napi_callbac napi_value JsKeyboardPanelManager::GetDefaultInputMethod(napi_env env, napi_callback_info info) { std::shared_ptr property; - int32_t ret = ImeSystemCmdChannel::GetInstance()->GetDefaultImeCfg(property); + auto channel = ImeSystemCmdChannel::GetInstance(); + if (channel == nullptr) { + return nullptr; + } + int32_t ret = channel->GetDefaultImeCfg(property); if (ret != ErrorCode::NO_ERROR || property == nullptr) { IMSA_HILOGE("GetDefaultImeCfg failed or property is nullptr ret: %{public}d!", ret); return nullptr; diff --git a/frameworks/kits/extension/src/inputmethod_extension_context.cpp b/frameworks/kits/extension/src/inputmethod_extension_context.cpp index d63e67e9f..6c87e4e89 100644 --- a/frameworks/kits/extension/src/inputmethod_extension_context.cpp +++ b/frameworks/kits/extension/src/inputmethod_extension_context.cpp @@ -27,7 +27,12 @@ int InputMethodExtensionContext::ILLEGAL_REQUEST_CODE(-1); ErrCode InputMethodExtensionContext::StartAbility(const AAFwk::Want &want) const { IMSA_HILOGD("%{public}s begin.", __func__); - ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, token_, ILLEGAL_REQUEST_CODE); + auto client = AAFwk::AbilityManagerClient::GetInstance(); + if (client == nullptr) { + IMSA_HILOGE("client is nullptr."); + return ERR_NO_INIT; + } + ErrCode err = client->StartAbility(want, token_, ILLEGAL_REQUEST_CODE); IMSA_HILOGD("%{public}s ret=%{public}d", __func__, err); if (err != ERR_OK) { IMSA_HILOGE("InputMethodExtensionContext::StartAbility failed: %{public}d", err); @@ -39,8 +44,12 @@ ErrCode InputMethodExtensionContext::StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions) const { IMSA_HILOGD("%{public}s start.", __func__); - ErrCode err = - AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, startOptions, token_, ILLEGAL_REQUEST_CODE); + auto client = AAFwk::AbilityManagerClient::GetInstance(); + if (client == nullptr) { + IMSA_HILOGE("client is nullptr."); + return ERR_NO_INIT; + } + ErrCode err = client->StartAbility(want, startOptions, token_, ILLEGAL_REQUEST_CODE); IMSA_HILOGD("%{public}s ret: %{public}d.", __func__, err); if (err != ERR_OK) { IMSA_HILOGE("InputMethodExtensionContext::StartAbility failed: %{public}d", err); @@ -60,8 +69,12 @@ bool InputMethodExtensionContext::ConnectAbility(const AAFwk::Want &want, ErrCode InputMethodExtensionContext::StartAbilityWithAccount(const AAFwk::Want &want, int accountId) const { IMSA_HILOGI("%{public}s start, accountId: %{public}d.", __func__, accountId); - ErrCode err = - AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, token_, ILLEGAL_REQUEST_CODE, accountId); + auto client = AAFwk::AbilityManagerClient::GetInstance(); + if (client == nullptr) { + IMSA_HILOGE("client is nullptr."); + return ERR_NO_INIT; + } + ErrCode err = client->StartAbility(want, token_, ILLEGAL_REQUEST_CODE, accountId); IMSA_HILOGD("%{public}s ret: %{public}d.", __func__, err); if (err != ERR_OK) { IMSA_HILOGE("InputMethodExtensionContext::StartAbilityWithAccount failed: %{public}d!", err); @@ -73,7 +86,12 @@ ErrCode InputMethodExtensionContext::StartAbilityWithAccount(const AAFwk::Want & const AAFwk::StartOptions &startOptions) const { IMSA_HILOGD("%{public}s start.", __func__); - ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, startOptions, token_, + auto client = AAFwk::AbilityManagerClient::GetInstance(); + if (client == nullptr) { + IMSA_HILOGE("client is nullptr."); + return ERR_NO_INIT; + } + ErrCode err = client->StartAbility(want, startOptions, token_, ILLEGAL_REQUEST_CODE, accountId); IMSA_HILOGD("%{public}s ret: %{public}d", __func__, err); if (err != ERR_OK) { @@ -106,7 +124,12 @@ ErrCode InputMethodExtensionContext::DisconnectAbility(const AAFwk::Want &want, ErrCode InputMethodExtensionContext::TerminateAbility() { IMSA_HILOGI("%{public}s start.", __func__); - ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->TerminateAbility(token_, -1, nullptr); + auto client = AAFwk::AbilityManagerClient::GetInstance(); + if (client == nullptr) { + IMSA_HILOGE("client is nullptr."); + return ERR_NO_INIT; + } + ErrCode err = client->TerminateAbility(token_, -1, nullptr); if (err != ERR_OK) { IMSA_HILOGE("InputMethodExtensionContext::TerminateAbility failed: %{public}d!", err); } diff --git a/frameworks/kits/extension/src/js_inputmethod_extension.cpp b/frameworks/kits/extension/src/js_inputmethod_extension.cpp index 2ecf91cbc..ae1e17911 100644 --- a/frameworks/kits/extension/src/js_inputmethod_extension.cpp +++ b/frameworks/kits/extension/src/js_inputmethod_extension.cpp @@ -89,6 +89,10 @@ napi_value AttachInputMethodExtensionContext(napi_env env, void *value, void *) JsInputMethodExtension *JsInputMethodExtension::Create(const std::unique_ptr &runtime) { IMSA_HILOGI("JsInputMethodExtension Create."); + if (runtime == nullptr) { + IMSA_HILOGE("runtime is nullptr."); + return nullptr; + } jsInputMethodExtension = new JsInputMethodExtension(static_cast(*runtime)); return jsInputMethodExtension; } @@ -115,6 +119,10 @@ void JsInputMethodExtension::Init(const std::shared_ptr &rec return; } + if (abilityInfo_ == nullptr) { + IMSA_HILOGE("abilityInfo_ is nullptr!"); + return; + } std::string moduleName(Extension::abilityInfo_->moduleName); moduleName.append("::").append(abilityInfo_->name); IMSA_HILOGI("JsInputMethodExtension, module: %{public}s, srcPath:%{public}s.", moduleName.c_str(), srcPath.c_str()); @@ -279,7 +287,12 @@ void JsInputMethodExtension::OnStop() InputMethodExtension::OnStop(); IMSA_HILOGI("JsInputMethodExtension OnStop start."); CallObjectMethod("onDestroy"); - bool ret = ConnectionManager::GetInstance().DisconnectCaller(GetContext()->GetToken()); + auto context = GetContext(); + if (context == nullptr) { + IMSA_HILOGE("context is nullptr."); + return; + } + bool ret = ConnectionManager::GetInstance().DisconnectCaller(context->GetToken()); if (ret) { IMSA_HILOGI("the input method extension connection is not disconnected."); } @@ -380,6 +393,10 @@ napi_value JsInputMethodExtension::CallObjectMethod(const char *name, const napi void JsInputMethodExtension::GetSrcPath(std::string &srcPath) { IMSA_HILOGD("JsInputMethodExtension GetSrcPath start."); + if (abilityInfo_ == nullptr) { + IMSA_HILOGE("abilityInfo_ is nullptr!"); + return; + } if (!Extension::abilityInfo_->isModuleJson) { /* temporary compatibility api8 + config.json */ srcPath.append(Extension::abilityInfo_->package); diff --git a/frameworks/native/inputmethod_controller/src/system_cmd_channel_service_impl.cpp b/frameworks/native/inputmethod_controller/src/system_cmd_channel_service_impl.cpp index 74ac1deb1..930c987a3 100644 --- a/frameworks/native/inputmethod_controller/src/system_cmd_channel_service_impl.cpp +++ b/frameworks/native/inputmethod_controller/src/system_cmd_channel_service_impl.cpp @@ -33,12 +33,20 @@ ErrCode SystemCmdChannelServiceImpl::SendPrivateCommand(const Value &value) { std::unordered_map privateCommand; privateCommand = value.valueMap; - return ImeSystemCmdChannel::GetInstance()->ReceivePrivateCommand(privateCommand); + auto channel = ImeSystemCmdChannel::GetInstance(); + if (channel == nullptr) { + return ErrorCode::ERROR_NULL_POINTER; + } + return channel->ReceivePrivateCommand(privateCommand); } ErrCode SystemCmdChannelServiceImpl::NotifyPanelStatus(const SysPanelStatus &sysPanelStatus) { - return ImeSystemCmdChannel::GetInstance()->NotifyPanelStatus(sysPanelStatus); + auto channel = ImeSystemCmdChannel::GetInstance(); + if (channel == nullptr) { + return ErrorCode::ERROR_NULL_POINTER; + } + return channel->NotifyPanelStatus(sysPanelStatus); } } // namespace MiscServices } // namespace OHOS \ No newline at end of file diff --git a/services/adapter/keyboard/src/input_event_callback.cpp b/services/adapter/keyboard/src/input_event_callback.cpp index 5d092a5fd..310772808 100644 --- a/services/adapter/keyboard/src/input_event_callback.cpp +++ b/services/adapter/keyboard/src/input_event_callback.cpp @@ -25,6 +25,10 @@ const std::map MASK_MAP{ void InputEventCallback::OnInputEvent(std::shared_ptr keyEvent) const { + if (keyEvent == nullptr) { + IMSA_HILOGE("keyEvent is nullptr!"); + return; + } auto keyCode = keyEvent->GetKeyCode(); auto keyAction = keyEvent->GetKeyAction(); auto currKey = MASK_MAP.find(keyCode); diff --git a/services/adapter/keyboard/src/keyboard_event.cpp b/services/adapter/keyboard/src/keyboard_event.cpp index 0240fa072..02b06c12f 100644 --- a/services/adapter/keyboard/src/keyboard_event.cpp +++ b/services/adapter/keyboard/src/keyboard_event.cpp @@ -30,7 +30,11 @@ int32_t KeyboardEvent::AddKeyEventMonitor(KeyHandle handle) IMSA_HILOGI("KeyboardEvent::AddKeyEventMonitor start."); std::shared_ptr callback = std::make_shared(); callback->SetKeyHandle(handle); - int32_t monitorId = InputManager::GetInstance()->AddMonitor([callback](std::shared_ptr keyEvent) { + auto manager = InputManager::GetInstance(); + if (manager == nullptr) { + return ErrorCode::ERROR_NULL_POINTER; + } + int32_t monitorId = manager->AddMonitor([callback](std::shared_ptr keyEvent) { if (callback == nullptr) { IMSA_HILOGE("callback is nullptr!"); return; @@ -87,7 +91,12 @@ void KeyboardEvent::SubscribeCombinationKey( keyOption->SetFinalKeyDown(setFinalKeyDown); // 0 means press delay 0 ms keyOption->SetFinalKeyDownDuration(0); - int32_t subscribeId = InputManager::GetInstance()->SubscribeKeyEvent(keyOption, callback); + auto manager = InputManager::GetInstance(); + if (manager == nullptr) { + IMSA_HILOGE("manager is nullptr"); + return; + } + int32_t subscribeId = manager->SubscribeKeyEvent(keyOption, callback); if (subscribeId < 0) { IMSA_HILOGE("failed to SubscribeKeyEvent, id: %{public}d preKey: %{public}d.", subscribeId, preKey); } diff --git a/services/identity_checker/src/identity_checker_impl.cpp b/services/identity_checker/src/identity_checker_impl.cpp index eb62d9de1..02b2b47d9 100644 --- a/services/identity_checker/src/identity_checker_impl.cpp +++ b/services/identity_checker/src/identity_checker_impl.cpp @@ -121,7 +121,12 @@ bool IdentityCheckerImpl::IsFocusedUIExtension(uint32_t callingTokenId, sptrCheckUIExtensionIsFocused(callingTokenId, isFocused); + auto client = AbilityManagerClient::GetInstance(); + if (client == nullptr) { + IMSA_HILOGE("AbilityManagerClient is nullptr"); + return false; + } + auto ret = client->CheckUIExtensionIsFocused(callingTokenId, isFocused); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("failed to CheckUIExtensionIsFocused, ret: %{public}d", ret); return false; diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index 36508d4e2..928d81a77 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -85,8 +85,13 @@ InputMethodSystemAbility::InputMethodSystemAbility() : state_(ServiceRunningStat InputMethodSystemAbility::~InputMethodSystemAbility() { stop_ = true; - Message *msg = new Message(MessageID::MSG_ID_QUIT_WORKER_THREAD, nullptr); - MessageHandler::Instance()->SendMessage(msg); + Message *msg = new (std::nothrow) Message(MessageID::MSG_ID_QUIT_WORKER_THREAD, nullptr); + auto handler = MessageHandler::Instance(); + if (handler == nullptr) { + IMSA_HILOGE("handler is nullptr"); + return; + } + handler->SendMessage(msg); if (workThreadHandler.joinable()) { workThreadHandler.join(); } @@ -198,7 +203,11 @@ void InputMethodSystemAbility::OnStart() if (ret != ErrorCode::NO_ERROR) { InputMethodSysEvent::GetInstance().ServiceFaultReporter("imf", ret); auto callback = [=]() { Init(); }; - serviceHandler_->PostTask(callback, INIT_INTERVAL); + if (serviceHandler_ == nullptr) { + IMSA_HILOGE("serviceHandler_ is nullptr!"); + } else { + serviceHandler_->PostTask(callback, INIT_INTERVAL); + } IMSA_HILOGE("init failed. try again 10s later!"); } InitHiTrace(); @@ -501,6 +510,10 @@ int32_t InputMethodSystemAbility::PrepareForOperateKeyboard(std::shared_ptr &info) { + if (info == nullptr) { + IMSA_HILOGE("info is nullptr!"); + return ErrorCode::ERROR_NULL_POINTER; + } auto target = ImeInfoInquirer::GetInstance().FindTargetSubtypeByCondition(info->subProps, condition); if (target == nullptr) { IMSA_HILOGE("target is empty!"); @@ -712,7 +725,8 @@ int32_t InputMethodSystemAbility::CheckInputTypeOption(int32_t userId, InputClie session->RestoreCurrentImeSubType(DEFAULT_DISPLAY_ID); } #ifdef IMF_SCREENLOCK_MGR_ENABLE - if (ScreenLock::ScreenLockManager::GetInstance()->IsScreenLocked()) { + auto screenLockMgr = ScreenLock::ScreenLockManager::GetInstance(); + if (screenLockMgr != nullptr && screenLockMgr->IsScreenLocked()) { std::string ime; if (GetScreenLockIme(userId, ime) != ErrorCode::NO_ERROR) { IMSA_HILOGE("not ime screenlocked"); @@ -930,7 +944,7 @@ ErrCode InputMethodSystemAbility::PanelStatusChange(uint32_t status, const ImeWi } auto commonEventManager = ImCommonEventManager::GetInstance(); if (commonEventManager != nullptr) { - auto ret = ImCommonEventManager::GetInstance()->PublishPanelStatusChangeEvent( + auto ret = commonEventManager->PublishPanelStatusChangeEvent( userId, static_cast(status), info); IMSA_HILOGD("public panel status change event: %{public}d", ret); } @@ -1237,6 +1251,10 @@ int32_t InputMethodSystemAbility::OnSwitchInputMethod(int32_t userId, const Swit void InputMethodSystemAbility::GetValidSubtype(const std::string &subName, const std::shared_ptr &info) { + if (info == nullptr) { + IMSA_HILOGE("info is nullptr!"); + return; + } if (subName.empty()) { IMSA_HILOGW("undefined subtype"); info->subProp.id = UNDEFINED; @@ -1310,6 +1328,10 @@ int32_t InputMethodSystemAbility::Switch(int32_t userId, const std::string &bund // Switch the current InputMethodExtension to the new InputMethodExtension int32_t InputMethodSystemAbility::SwitchExtension(int32_t userId, const std::shared_ptr &info) { + if (info == nullptr) { + IMSA_HILOGE("info is nullptr!"); + return ErrorCode::ERROR_NULL_POINTER; + } auto session = UserSessionManager::GetInstance().GetUserSession(userId); if (session == nullptr) { IMSA_HILOGE("%{public}d session is nullptr!", userId); @@ -1431,7 +1453,7 @@ ErrCode InputMethodSystemAbility::GetDefaultInputMethod(Property &prop, bool isB { std::shared_ptr property = std::make_shared(prop); auto ret = ImeInfoInquirer::GetInstance().GetDefaultInputMethod(GetCallingUserId(), property, isBrief); - if (ret == ErrorCode::NO_ERROR) { + if (property != nullptr && ret == ErrorCode::NO_ERROR) { prop = *property; } return ret; @@ -1468,6 +1490,10 @@ void InputMethodSystemAbility::WorkThread() pthread_setname_np(pthread_self(), "OS_IMSAWorkThread"); while (!stop_) { Message *msg = MessageHandler::Instance()->GetMessage(); + if (msg == nullptr) { + IMSA_HILOGE("msg is nullptr!"); + break; + } switch (msg->msgId_) { case MSG_ID_USER_START: { OnUserStarted(msg); @@ -1544,6 +1570,10 @@ void InputMethodSystemAbility::WorkThread() */ int32_t InputMethodSystemAbility::OnUserStarted(const Message *msg) { + if (msg == nullptr) { + IMSA_HILOGE("msg is nullptr!"); + return ErrorCode::ERROR_NULL_POINTER; + } if (msg->msgContent_ == nullptr) { IMSA_HILOGE("msgContent_ is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; @@ -1897,7 +1927,12 @@ void InputMethodSystemAbility::HandleDataShareReady() int32_t InputMethodSystemAbility::InitAccountMonitor() { IMSA_HILOGI("InputMethodSystemAbility::InitAccountMonitor start."); - return ImCommonEventManager::GetInstance()->SubscribeAccountManagerService([this]() { HandleOsAccountStarted(); }); + auto imCommonEventManager = ImCommonEventManager::GetInstance(); + if (imCommonEventManager == nullptr) { + IMSA_HILOGE("imCommonEventManager is nullptr!"); + return ErrorCode::ERROR_NULL_POINTER; + } + return imCommonEventManager->SubscribeAccountManagerService([this]() { HandleOsAccountStarted(); }); } int32_t InputMethodSystemAbility::InitKeyEventMonitor() @@ -1913,18 +1948,33 @@ int32_t InputMethodSystemAbility::InitKeyEventMonitor() // Check device capslock status and ime cfg corrent, when device power-up. HandleImeCfgCapsState(); }; - bool ret = ImCommonEventManager::GetInstance()->SubscribeKeyboardEvent(handler); + auto imCommonEventManager = ImCommonEventManager::GetInstance(); + if (imCommonEventManager == nullptr) { + IMSA_HILOGE("imCommonEventManager is nullptr!"); + return ErrorCode::ERROR_NULL_POINTER; + } + bool ret = imCommonEventManager->SubscribeKeyboardEvent(handler); return ret ? ErrorCode::NO_ERROR : ErrorCode::ERROR_SERVICE_START_FAILED; } bool InputMethodSystemAbility::InitWmsMonitor() { - return ImCommonEventManager::GetInstance()->SubscribeWindowManagerService([this]() { HandleWmsStarted(); }); + auto imCommonEventManager = ImCommonEventManager::GetInstance(); + if (imCommonEventManager == nullptr) { + IMSA_HILOGE("imCommonEventManager is nullptr!"); + return false; + } + return imCommonEventManager->SubscribeWindowManagerService([this]() { HandleWmsStarted(); }); } // LCOV_EXCL_STOP bool InputMethodSystemAbility::InitMemMgrMonitor() { - return ImCommonEventManager::GetInstance()->SubscribeMemMgrService([this]() { HandleMemStarted(); }); + auto imCommonEventManager = ImCommonEventManager::GetInstance(); + if (imCommonEventManager == nullptr) { + IMSA_HILOGE("imCommonEventManager is nullptr!"); + return false; + } + return imCommonEventManager->SubscribeMemMgrService([this]() { HandleMemStarted(); }); } void InputMethodSystemAbility::InitWmsConnectionMonitor() @@ -2309,7 +2359,12 @@ void InputMethodSystemAbility::HandleOsAccountStarted() if (msg == nullptr) { return; } - MessageHandler::Instance()->SendMessage(msg); + auto handler = MessageHandler::Instance(); + if (handler == nullptr) { + IMSA_HILOGE("handler is nullptr"); + return; + } + handler->SendMessage(msg); } void InputMethodSystemAbility::StopImeInBackground() diff --git a/services/src/peruser_session.cpp b/services/src/peruser_session.cpp index c67651e7e..0a739d693 100644 --- a/services/src/peruser_session.cpp +++ b/services/src/peruser_session.cpp @@ -1232,7 +1232,8 @@ int32_t PerUserSession::ChangeToDefaultImeIfNeed( IMSA_HILOGD("no need"); return ErrorCode::NO_ERROR; #endif - if (!ScreenLock::ScreenLockManager::GetInstance()->IsScreenLocked()) { + auto screenLockMgr = ScreenLock::ScreenLockManager::GetInstance(); + if (screenLockMgr != nullptr && !screenLockMgr->IsScreenLocked()) { IMSA_HILOGD("no need"); imeToStart = targetIme; return ErrorCode::NO_ERROR; @@ -1257,6 +1258,11 @@ AAFwk::Want PerUserSession::GetWant(const std::shared_ptr &ime) { bool isolatedSandBox = true; EnabledStatus status = EnabledStatus::BASIC_MODE; + AAFwk::Want want; + if (ime == nullptr) { + IMSA_HILOGE("ime is null"); + return want; + } if (ImeEnabledInfoManager::GetInstance().IsDefaultFullMode(userId_, ime->bundleName)) { status = EnabledStatus::FULL_EXPERIENCE_MODE; isolatedSandBox = false; @@ -1266,7 +1272,6 @@ AAFwk::Want PerUserSession::GetWant(const std::shared_ptr &ime) IMSA_HILOGE("%{public}d/%{public}s GetEnabledState failed.", userId_, ime->imeId.c_str()); } } - AAFwk::Want want; want.SetElementName(ime->bundleName, ime->extName); want.SetParam(STRICT_MODE, !(status == EnabledStatus::FULL_EXPERIENCE_MODE)); want.SetParam(ISOLATED_SANDBOX, isolatedSandBox); @@ -1284,6 +1289,9 @@ int32_t PerUserSession::StartInputService(const std::shared_ptr &i IMSA_HILOGI("run in %{public}s", ime->imeId.c_str()); auto imeToStart = std::make_shared(); auto ret = ChangeToDefaultImeIfNeed(ime, imeToStart); + if (imeToStart == nullptr) { + return ErrorCode::ERROR_IMSA_IME_TO_START_NULLPTR; + } if (ret != ErrorCode::NO_ERROR) { return ret; } @@ -1343,6 +1351,10 @@ int32_t PerUserSession::OnPanelStatusChange( int32_t PerUserSession::OnUpdateListenEventFlag(const InputClientInfo &clientInfo) { + if (clientInfo.client == nullptr) { + IMSA_HILOGE("clientInfo is nullptr!"); + return ErrorCode::ERROR_NULL_POINTER; + } auto remoteClient = clientInfo.client->AsObject(); auto ret = AddClientInfo(remoteClient, clientInfo, START_LISTENING); if (ret != ErrorCode::NO_ERROR) { @@ -2114,6 +2126,10 @@ void PerUserSession::HandleImeBindTypeChanged( /* isClientInactive: true: represent the oldClientInfo is inactiveClient's false: represent the oldClientInfo is currentClient's */ std::shared_ptr oldClientInfo = nullptr; + if (clientGroup == nullptr) { + IMSA_HILOGE("clientGroup is nullptr!"); + return; + } bool isClientInactive = false; { std::lock_guard lock(focusedClientLock_); @@ -2403,6 +2419,10 @@ bool PerUserSession::IsDefaultDisplayGroup(uint64_t displayId) void PerUserSession::ClearRequestKeyboardReason(std::shared_ptr &clientInfo) { + if (clientInfo == nullptr) { + IMSA_HILOGE("clientGroup is nullptr!"); + return; + } clientInfo->requestKeyboardReason = RequestKeyboardReason::NONE; } diff --git a/test/unittest/cpp_test/src/ime_system_channel_test.cpp b/test/unittest/cpp_test/src/ime_system_channel_test.cpp index 5f0522e97..85305ec12 100644 --- a/test/unittest/cpp_test/src/ime_system_channel_test.cpp +++ b/test/unittest/cpp_test/src/ime_system_channel_test.cpp @@ -16,6 +16,7 @@ #define private public #define protected public #include "ime_system_channel.h" +#include "system_cmd_channel_service_impl.h" #undef private #include @@ -36,17 +37,20 @@ public: void SetUp(); void TearDown(); static sptr imeSystemChannel_; + static sptr systemCmdChannelServiceImpl_; static sptr sysCmdListener_; static uint64_t permissionTokenId_; }; sptr ImeSystemChannelTest::imeSystemChannel_; sptr ImeSystemChannelTest::sysCmdListener_; +sptr ImeSystemChannelTest::systemCmdChannelServiceImpl_; uint64_t ImeSystemChannelTest::permissionTokenId_ = 0; void ImeSystemChannelTest::SetUpTestCase(void) { TddUtil::StorageSelfTokenID(); imeSystemChannel_ = ImeSystemCmdChannel::GetInstance(); + systemCmdChannelServiceImpl_ = new (std::nothrow) SystemCmdChannelServiceImpl(); sysCmdListener_ = new (std::nothrow) OnSystemCmdListenerImpl(); permissionTokenId_ = TddUtil::AllocTestTokenID(true, "ohos.inputMethod.test", { "ohos.permission.CONNECT_IME_ABILITY" }); @@ -132,5 +136,23 @@ HWTEST_F(ImeSystemChannelTest, testImeSystemChannel_nullptr, TestSize.Level1) ret = imeSystemChannel_->NotifyPanelStatus({ InputType::NONE, 0, 0, 0 }); EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER); } + +/** + * @tc.name: testSystemCmdChannelServiceImpl + * @tc.desc: SystemCmdChannel test SystemCmdChannelServiceImpl is nullptr. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ImeSystemChannelTest, testSystemCmdChannelServiceImpl_nullptr, TestSize.Level1) +{ + IMSA_HILOGI("ImeSystemChannelTest testSystemCmdChannelServiceImpl_nullptr Test START"); + std::unordered_map privateCommand; + PrivateDataValue privateDataValue1 = std::string("stringValue"); + privateCommand.emplace("value1", privateDataValue1); + int32_t ret = systemCmdChannelServiceImpl_->SendPrivateCommand(privateCommand); + EXPECT_EQ(ret, ErrorCode::ERROR_EX_NULL_POINTER); + ret = systemCmdChannelServiceImpl_->NotifyPanelStatus({ InputType::NONE, 0, 0, 0 }); + EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER); +} } // namespace MiscServices } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/cpp_test/src/input_method_ability_test.cpp b/test/unittest/cpp_test/src/input_method_ability_test.cpp index b95bef728..fbc4e0091 100644 --- a/test/unittest/cpp_test/src/input_method_ability_test.cpp +++ b/test/unittest/cpp_test/src/input_method_ability_test.cpp @@ -2162,5 +2162,39 @@ HWTEST_F(InputMethodAbilityTest, testClearBindInfo, TestSize.Level0) EXPECT_TRUE(inputMethodAbility_.dataChannelObject_ == nullptr); InputMethodAbilityTest::GetIMCDetachIMA(); } + +/** + * @tc.name: testServiceHandler_ + * @tc.desc: testServiceHandler_ is nullptr + * @tc.type: FUNC + */ +HWTEST_F(InputMethodAbilityTest, TestServiceHandler_, TestSize.Level0) +{ + IMSA_HILOGI("InputMethodAbilityTest testServiceHandler_ START"); + int32_t userId = 100; + string subName = ""; + auto temp = imsa_->serviceHandler_; + imsa_->serviceHandler_ = nullptr; + + imsa_->SubscribeCommonEvent(); + + imsa_->GetValidSubtype(subName, nullptr); + auto info = std::make_shared(); + EXPECT_NE(info, nullptr); + imsa_->GetValidSubtype(subName, info); + + EXPECT_EQ(imsa_->serviceHandler_, nullptr); + auto ret = imsa_->SwitchExtension(userId, nullptr); + EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER); + ret = imsa_->SwitchExtension(userId, info); + MessageParcel *parcel1 = new (std::nothrow) MessageParcel(); + auto msg = std::make_shared(MessageID::MSG_ID_USER_START, parcel1); + ret = imsa_->OnUserStarted(msg.get()); + msg.reset(); + ret = imsa_->OnUserStarted(msg.get()); + EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER); + imsa_->serviceHandler_ = temp; +} + } // namespace MiscServices } // namespace OHOS diff --git a/test/unittest/cpp_test/src/input_method_controller_test.cpp b/test/unittest/cpp_test/src/input_method_controller_test.cpp index 88f8f24c1..ebdc529c5 100644 --- a/test/unittest/cpp_test/src/input_method_controller_test.cpp +++ b/test/unittest/cpp_test/src/input_method_controller_test.cpp @@ -50,6 +50,7 @@ #include "input_client_stub.h" #include "input_client_service_impl.h" #include "input_death_recipient.h" +#include "input_event_callback.h" #include "input_method_ability.h" #include "input_method_engine_listener_impl.h" #include "input_data_channel_service_impl.h" @@ -2148,5 +2149,44 @@ HWTEST_F(InputMethodControllerTest, TestEditorContentLock, TestSize.Level0) GTEST_RUN_TASK(InputMethodControllerTest::EditorContentMultiTest); EXPECT_EQ(multiThreadExecTotalNum_, THREAD_NUM * EACH_THREAD_CIRCULATION_TIME); } + +/** + * @tc.name: TestClientNullptr + * @tc.desc: Test clientInfo.client is nullptr + * @tc.type: FUNC + */ +HWTEST_F(InputMethodControllerTest, TestClientNullptr, TestSize.Level0) +{ + IMSA_HILOGI("TestClientNullptr START"); + auto sessionTemp = std::make_shared(0, nullptr); + sptr client = new (std::nothrow) InputClientServiceImpl(); + InputClientInfo clientInfo = { .client = nullptr }; + + sessionTemp->GetWant(nullptr); + auto ret = sessionTemp->OnUpdateListenEventFlag(clientInfo); + EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER); + + sessionTemp->HandleImeBindTypeChanged(clientInfo, nullptr); + std::shared_ptr ptr = nullptr; + sessionTemp->ClearRequestKeyboardReason(ptr); + auto info = std::make_shared(); + EXPECT_NE(info, nullptr); + sessionTemp->ClearRequestKeyboardReason(info); +} + +/** + * @tc.name: TestEventCallback + * @tc.desc: Test TestEventCallback + * @tc.type: FUNC + */ +HWTEST_F(InputMethodControllerTest, TestEventCallback, TestSize.Level0) +{ + IMSA_HILOGI("TestEventCallback START"); + auto eventcallback = std::make_shared(); + std::shared_ptr keyevent = nullptr; + eventcallback->OnInputEvent(keyevent); + EXPECT_EQ(keyevent, nullptr); + eventcallback->OnInputEvent(keyEvent_); +} } // namespace MiscServices } // namespace OHOS -- Gitee