From 78a0104c0f8f76edeca96c53bac496b0cf94f74a Mon Sep 17 00:00:00 2001 From: blc Date: Fri, 25 Jul 2025 17:00:15 +0800 Subject: [PATCH] :wqfix nullptr Signed-off-by: blc --- .../cj_input_method_textchanged_listener.cpp | 70 +++++++++++++--- frameworks/cj/src/input_method_ffi.cpp | 2 +- frameworks/cj/src/utils.cpp | 8 ++ .../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_get_input_method_controller.cpp | 3 + .../js_get_input_method_setting.cpp | 3 + ...s_get_input_method_textchange_listener.cpp | 84 ++++++++++++++++--- .../js_keyboard_panel_manager.cpp | 31 ++++++- .../src/system_cmd_channel_service_impl.cpp | 8 +- 12 files changed, 192 insertions(+), 29 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..2afe9f867 100644 --- a/frameworks/cj/src/cj_input_method_textchanged_listener.cpp +++ b/frameworks/cj/src/cj_input_method_textchanged_listener.cpp @@ -32,52 +32,102 @@ 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!"); + return ErrorCode::ERROR_NULL_POINTER; + } + 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.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..b17dbe5ef 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..8417230c8 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..4ee924146 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/js_get_input_method_controller.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp index abb5dba04..572ef3077 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..fa0cee012 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..0fe58a8c0 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,102 @@ 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!"); + return ErrorCode::ERROR_NULL_POINTER; + } + return controller->GetTextIndexAtCursor(); } int32_t JsGetInputMethodTextChangedListener::ReceivePrivateCommand( @@ -100,12 +150,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/keyboardpanelmanager/js_keyboard_panel_manager.cpp b/frameworks/js/napi/keyboardpanelmanager/js_keyboard_panel_manager.cpp index 1cb5c03ba..c2f06211b 100644 --- a/frameworks/js/napi/keyboardpanelmanager/js_keyboard_panel_manager.cpp +++ b/frameworks/js/napi/keyboardpanelmanager/js_keyboard_panel_manager.cpp @@ -77,7 +77,13 @@ napi_value JsKeyboardPanelManager::ConnectSystemCmd(napi_env env, napi_callback_ auto ctxt = std::make_shared(); auto manager = JsKeyboardPanelManager::GetInstance(); 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); + return; + } + auto ret = channel->ConnectSystemCmd(manager); ctxt->SetErrorCode(ret); CHECK_RETURN_VOID(ret == ErrorCode::NO_ERROR, "ConnectSystemCmd return error!"); ctxt->SetState(napi_ok); @@ -149,6 +155,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 +204,12 @@ 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); + return; + } + ctxt->smartMenu = channel->GetSmartMenuCfg(); ctxt->SetState(napi_ok); }; ctxt->SetAction(nullptr, std::move(output)); @@ -222,7 +236,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 +258,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/native/inputmethod_controller/src/system_cmd_channel_service_impl.cpp b/frameworks/native/inputmethod_controller/src/system_cmd_channel_service_impl.cpp index 74ac1deb1..3df25760b 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,16 @@ ErrCode SystemCmdChannelServiceImpl::SendPrivateCommand(const Value &value) { std::unordered_map privateCommand; privateCommand = value.valueMap; - return ImeSystemCmdChannel::GetInstance()->ReceivePrivateCommand(privateCommand); + auto channel = ImeSystemCmdChannel::GetInstance(); + return (channel == nullptr) ? + ErrorCode::ERROR_NULL_POINTER : channel->ReceivePrivateCommand(privateCommand); } ErrCode SystemCmdChannelServiceImpl::NotifyPanelStatus(const SysPanelStatus &sysPanelStatus) { - return ImeSystemCmdChannel::GetInstance()->NotifyPanelStatus(sysPanelStatus); + auto channel = ImeSystemCmdChannel::GetInstance(); + return (channel == nullptr) ? + ErrorCode::ERROR_NULL_POINTER : channel->NotifyPanelStatus(sysPanelStatus); } } // namespace MiscServices } // namespace OHOS \ No newline at end of file -- Gitee