From 4a70cf222716399a72ad1647c5461c90939b051f Mon Sep 17 00:00:00 2001 From: hemenghao Date: Sat, 24 May 2025 19:52:57 +0800 Subject: [PATCH] =?UTF-8?q?ai=E5=91=8A=E8=AD=A6=E4=BB=A5=E5=8F=8Apr?= =?UTF-8?q?=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hemenghao --- .../include/input_method_utils.h | 2 +- .../src/input_method_utils.cpp | 2 +- .../window_adapter/src/window_adapter.cpp | 4 +-- services/file/src/file_operator.cpp | 11 ++------ .../src/identity_checker_impl.cpp | 4 ++- services/include/client_group.h | 8 +++--- services/src/client_group.cpp | 26 ++++++++++++++----- services/src/im_common_event_manager.cpp | 4 ++- services/src/input_method_system_ability.cpp | 4 +-- services/src/input_type_manager.cpp | 2 +- services/src/peruser_session.cpp | 2 +- test/unittest/cpp_test/src/ime_proxy_test.cpp | 12 ++++----- .../src/input_method_private_member_test.cpp | 2 +- 13 files changed, 47 insertions(+), 36 deletions(-) diff --git a/frameworks/native/inputmethod_controller/include/input_method_utils.h b/frameworks/native/inputmethod_controller/include/input_method_utils.h index ab912a427..dc1cafff9 100644 --- a/frameworks/native/inputmethod_controller/include/input_method_utils.h +++ b/frameworks/native/inputmethod_controller/include/input_method_utils.h @@ -33,7 +33,7 @@ constexpr size_t MAX_PRIVATE_COMMAND_COUNT = 5; constexpr size_t MAX_VALUE_MAP_COUNT = 256; constexpr size_t MAX_ARRAY_BUFFER_MSG_ID_SIZE = 256; // 256B constexpr size_t MAX_ARRAY_BUFFER_MSG_PARAM_SIZE = 128 * 1024; // 128KB -static constexpr uint64_t INVALID_DISPLAY_ID = -1ULL; +static constexpr uint64_t INVALID_DISPLAY_ID = UINT64_MAX; const constexpr char *SYSTEM_CMD_KEY = "sys_cmd"; enum class EnterKeyType { UNSPECIFIED = 0, diff --git a/frameworks/native/inputmethod_controller/src/input_method_utils.cpp b/frameworks/native/inputmethod_controller/src/input_method_utils.cpp index 9f0a48449..5c74f8209 100644 --- a/frameworks/native/inputmethod_controller/src/input_method_utils.cpp +++ b/frameworks/native/inputmethod_controller/src/input_method_utils.cpp @@ -47,7 +47,7 @@ bool Value::ReadFromParcel(Parcel &in) valueMap.clear(); if (size == 0) { - IMSA_HILOGE("size is zero!"); + IMSA_HILOGD("size is zero!"); return true; } diff --git a/services/adapter/window_adapter/src/window_adapter.cpp b/services/adapter/window_adapter/src/window_adapter.cpp index b68881b9f..03a5854d8 100644 --- a/services/adapter/window_adapter/src/window_adapter.cpp +++ b/services/adapter/window_adapter/src/window_adapter.cpp @@ -107,7 +107,7 @@ uint64_t WindowAdapter::GetDisplayIdByWindowId(int32_t callingWindowId) if (!ListWindowInfo(windowInfos)) { return DEFAULT_DISPLAY_ID; } - auto iter = std::find_if(windowInfos.begin(), windowInfos.end(), [&callingWindowId](const auto &windowInfo) { + auto iter = std::find_if(windowInfos.begin(), windowInfos.end(), [callingWindowId = callingWindowId](const auto &windowInfo) { if (windowInfo == nullptr) { return false; } @@ -133,7 +133,7 @@ uint64_t WindowAdapter::GetDisplayIdByPid(int64_t callingPid) if (!ListWindowInfo(windowInfos)) { return DEFAULT_DISPLAY_ID; } - auto iter = std::find_if(windowInfos.begin(), windowInfos.end(), [&callingPid](const auto &windowInfo) { + auto iter = std::find_if(windowInfos.begin(), windowInfos.end(), [callingPid = callingPid](const auto &windowInfo) { if (windowInfo == nullptr) { return false; } diff --git a/services/file/src/file_operator.cpp b/services/file/src/file_operator.cpp index 7ad5c055a..a0317af9c 100644 --- a/services/file/src/file_operator.cpp +++ b/services/file/src/file_operator.cpp @@ -54,15 +54,8 @@ bool FileOperator::Read(const std::string &path, std::string &content) bool FileOperator::IsValidPath(const std::string &filePath) { - if (filePath.find("../") != std::string::npos) { - IMSA_HILOGE("FilePath contains '../'"); - return false; - } - if (filePath.find("./") != std::string::npos) { - IMSA_HILOGE("FilePath contains './'"); - return false; - } - if (filePath[0] != '/') { + std::filesystem::path path { filePath }; + if (path.empty() || !path.is_absolute()) { IMSA_HILOGE("FilePath is not an absolute path"); return false; } diff --git a/services/identity_checker/src/identity_checker_impl.cpp b/services/identity_checker/src/identity_checker_impl.cpp index 2ee8a0724..40362b627 100644 --- a/services/identity_checker/src/identity_checker_impl.cpp +++ b/services/identity_checker/src/identity_checker_impl.cpp @@ -93,7 +93,9 @@ bool IdentityCheckerImpl::IsBroker(AccessTokenID tokenId) return false; } NativeTokenInfo nativeTokenInfoRes; - AccessTokenKit::GetNativeTokenInfo(tokenId, nativeTokenInfoRes); + if (AccessTokenKit::GetNativeTokenInfo(tokenId, nativeTokenInfoRes) != 0) { + return false; + } return nativeTokenInfoRes.processName == "broker"; } diff --git a/services/include/client_group.h b/services/include/client_group.h index ad3918935..9f68c4674 100644 --- a/services/include/client_group.h +++ b/services/include/client_group.h @@ -54,13 +54,13 @@ public: bool IsClientExist(sptr inputClient); bool IsNotifyInputStop(const sptr &client); - sptr GetCurrentClient(); + const sptr GetCurrentClient(); void SetCurrentClient(sptr client); - sptr GetInactiveClient(); + const sptr GetInactiveClient(); void SetInactiveClient(sptr client); bool IsCurClientFocused(int32_t pid, int32_t uid); - bool IsCurClientUnFocused(int32_t pid, int32_t uid); + bool IsCurClientUnfocused(int32_t pid, int32_t uid); // from service notify clients int32_t NotifyInputStartToClients(uint32_t callingWndId, int32_t requestKeyboardReason = 0); @@ -69,7 +69,7 @@ public: int32_t NotifyImeChangeToClients(const Property &property, const SubProperty &subProperty); private: - std::map, std::shared_ptr> GetClientMap(); + const std::map, std::shared_ptr>& GetClientMap(); bool IsSameClient(sptr source, sptr dest); void OnClientDied(sptr remote); uint64_t displayGroupId_{ DEFAULT_DISPLAY_ID }; diff --git a/services/src/client_group.cpp b/services/src/client_group.cpp index 026c039ab..80f907826 100644 --- a/services/src/client_group.cpp +++ b/services/src/client_group.cpp @@ -49,6 +49,10 @@ int32_t ClientGroup::AddClientInfo( } auto info = std::make_shared(clientInfo); std::weak_ptr weakClientInfo = info; + if (info->deathRecipient == nullptr) { + IMSA_HILOGE("deathRecipient is nullptr!"); + return ErrorCode::ERROR_CLIENT_NULL_POINTER; + } info->deathRecipient->SetDeathRecipient([this, weakClientInfo](const wptr &) { auto clientInfo = weakClientInfo.lock(); if (clientInfo == nullptr) { @@ -57,6 +61,10 @@ int32_t ClientGroup::AddClientInfo( } this->OnClientDied(clientInfo->client); }); + if (info->client == nullptr) { + IMSA_HILOGE("client is nullptr!"); + return ErrorCode::ERROR_CLIENT_NULL_POINTER; + } auto obj = info->client->AsObject(); if (obj == nullptr) { IMSA_HILOGE("client obj is nullptr!"); @@ -149,13 +157,13 @@ void ClientGroup::UpdateClientInfo(const sptr &client, const std: std::shared_ptr ClientGroup::GetClientInfo(pid_t pid) { + std::lock_guard lock(mtx_); auto iter = std::find_if( mapClients_.begin(), mapClients_.end(), [pid](const auto &mapClient) { return mapClient.second->pid == pid; }); if (iter == mapClients_.end()) { IMSA_HILOGD("not found."); return nullptr; } - std::lock_guard lock(mtx_); return iter->second; } @@ -211,7 +219,7 @@ bool ClientGroup::IsNotifyInputStop(const sptr &client) return false; } -sptr ClientGroup::GetCurrentClient() +const sptr ClientGroup::GetCurrentClient() { IMSA_HILOGD("get current client."); std::lock_guard lock(currentClientLock_); @@ -225,7 +233,7 @@ void ClientGroup::SetCurrentClient(sptr client) currentClient_ = client; } -sptr ClientGroup::GetInactiveClient() +const sptr ClientGroup::GetInactiveClient() { std::lock_guard lock(inactiveClientLock_); return inactiveClient_; @@ -311,6 +319,9 @@ int32_t ClientGroup::NotifyPanelStatusChange(const InputWindowStatus &status, co IMSA_HILOGD("has not imeHide callback"); continue; } + if (clientInfo->client == nullptr) { + continue; + } int32_t ret = clientInfo->client->OnPanelStatusChange(static_cast(status), info); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("failed to NotifyPanelStatusChange, ret: %{public}d", ret); @@ -330,6 +341,9 @@ int32_t ClientGroup::NotifyImeChangeToClients(const Property &property, const Su continue; } IMSA_HILOGD("notify client: [%{public}d]", static_cast(clientInfo->pid)); + if (clientInfo->client == nullptr) { + continue; + } int32_t ret = clientInfo->client->OnSwitchInput(property, subProperty); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("notify failed, ret: %{public}d, uid: %{public}d!", ret, static_cast(clientInfo->uid)); @@ -339,7 +353,7 @@ int32_t ClientGroup::NotifyImeChangeToClients(const Property &property, const Su return ErrorCode::NO_ERROR; } -bool ClientGroup::IsCurClientUnFocused(int32_t pid, int32_t uid) +bool ClientGroup::IsCurClientUnfocused(int32_t pid, int32_t uid) { auto clientInfo = GetCurrentClientInfo(); if (clientInfo == nullptr) { @@ -349,7 +363,7 @@ bool ClientGroup::IsCurClientUnFocused(int32_t pid, int32_t uid) auto identityChecker = std::make_shared(); if (clientInfo->uiExtensionTokenId != IMF_INVALID_TOKENID && !identityChecker->IsFocusedUIExtension(clientInfo->uiExtensionTokenId)) { - IMSA_HILOGI("UIExtension UnFocused."); + IMSA_HILOGI("UIExtension Unfocused."); return true; } return clientInfo->pid == pid && clientInfo->uid == uid; @@ -370,7 +384,7 @@ std::shared_ptr ClientGroup::GetClientInfo(sptr return it->second; } -std::map, std::shared_ptr> ClientGroup::GetClientMap() +const std::map, std::shared_ptr>& ClientGroup::GetClientMap() { std::lock_guard lock(mtx_); return mapClients_; diff --git a/services/src/im_common_event_manager.cpp b/services/src/im_common_event_manager.cpp index a48aabcca..b27d58505 100644 --- a/services/src/im_common_event_manager.cpp +++ b/services/src/im_common_event_manager.cpp @@ -215,7 +215,9 @@ void ImCommonEventManager::EventSubscriber::HandleUserEvent(int32_t messageId, c return; } IMSA_HILOGD("userId:%{public}d, messageId:%{public}d", userId, messageId); - parcel->WriteInt32(userId); + if (!parcel->WriteInt32(userId)) { + return; + } Message *msg = new (std::nothrow) Message(messageId, parcel); if (msg == nullptr) { delete parcel; diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index 48c40e054..27bfc8158 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -850,7 +850,7 @@ int32_t InputMethodSystemAbility::RegisterProxyIme( IMSA_HILOGE("current device does not support app agent"); return ErrorCode::ERROR_DEVICE_UNSUPPORTED; } - if (!identityChecker_->IsValidVirtualIme(IPCSkeleton::GetCallingUid())) { + if (identityChecker_ != nullptr && !identityChecker_->IsValidVirtualIme(IPCSkeleton::GetCallingUid())) { IMSA_HILOGE("not agent sa"); return ErrorCode::ERROR_NOT_AI_APP_IME; } @@ -869,7 +869,7 @@ int32_t InputMethodSystemAbility::UnregisterProxyIme(uint64_t displayId) IMSA_HILOGE("current device does not support app agent"); return ErrorCode::ERROR_DEVICE_UNSUPPORTED; } - if (!identityChecker_->IsValidVirtualIme(IPCSkeleton::GetCallingUid())) { + if (identityChecker_ != nullptr && !identityChecker_->IsValidVirtualIme(IPCSkeleton::GetCallingUid())) { IMSA_HILOGE("not agent sa"); return ErrorCode::ERROR_NOT_AI_APP_IME; } diff --git a/services/src/input_type_manager.cpp b/services/src/input_type_manager.cpp index 039dec749..e56ca2a81 100644 --- a/services/src/input_type_manager.cpp +++ b/services/src/input_type_manager.cpp @@ -76,9 +76,9 @@ bool InputTypeManager::IsStarted() bool InputTypeManager::IsSecurityImeStarted() { + std::lock_guard lock(typesLock_); InputType type = InputType::SECURITY_INPUT; return IsInputTypeImeStarted(type); - std::lock_guard lock(typesLock_); } bool InputTypeManager::IsCameraImeStarted() diff --git a/services/src/peruser_session.cpp b/services/src/peruser_session.cpp index 96d16bd72..1aad2e983 100644 --- a/services/src/peruser_session.cpp +++ b/services/src/peruser_session.cpp @@ -992,7 +992,7 @@ void PerUserSession::OnUnfocused(uint64_t displayId, int32_t pid, int32_t uid) if (clientGroup->GetCurrentClient() == nullptr) { return; } - if (clientGroup->IsCurClientUnFocused(pid, uid)) { + if (clientGroup->IsCurClientUnfocused(pid, uid)) { IMSA_HILOGD("current client Unfocused, unFocusedPid: %{public}d", pid); return; } diff --git a/test/unittest/cpp_test/src/ime_proxy_test.cpp b/test/unittest/cpp_test/src/ime_proxy_test.cpp index c5b91f4c7..ee3977b1b 100644 --- a/test/unittest/cpp_test/src/ime_proxy_test.cpp +++ b/test/unittest/cpp_test/src/ime_proxy_test.cpp @@ -429,13 +429,13 @@ HWTEST_F(ImeProxyTest, UnRegisteredProxyWithErrorType_011, TestSize.Level1) } /** - * @tc.name: AppUnFocusInProxyBindInPe_012 + * @tc.name: AppUnfocusInProxyBindInPe_012 * @tc.desc: * @tc.type: FUNC */ -HWTEST_F(ImeProxyTest, AppUnFocusInProxyBindInPe_012, TestSize.Level1) +HWTEST_F(ImeProxyTest, AppUnfocusInProxyBindInPe_012, TestSize.Level1) { - IMSA_HILOGI("ImeProxyTest::AppUnFocusInProxyBindInPe_012"); + IMSA_HILOGI("ImeProxyTest::AppUnfocusInProxyBindInPe_012"); TddUtil::GetFocused(); auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); EXPECT_EQ(ret, ErrorCode::NO_ERROR); @@ -457,13 +457,13 @@ HWTEST_F(ImeProxyTest, AppUnFocusInProxyBindInPe_012, TestSize.Level1) } /** - * @tc.name: AppUnFocusInProxyBindInPc_013 + * @tc.name: AppUnfocusInProxyBindInPc_013 * @tc.desc: * @tc.type: FUNC */ -HWTEST_F(ImeProxyTest, AppUnFocusInProxyBindInPc_013, TestSize.Level1) +HWTEST_F(ImeProxyTest, AppUnfocusInProxyBindInPc_013, TestSize.Level1) { - IMSA_HILOGI("ImeProxyTest::AppUnFocusInProxyBindInPc_013"); + IMSA_HILOGI("ImeProxyTest::AppUnfocusInProxyBindInPc_013"); TddUtil::GetFocused(); auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy(); EXPECT_EQ(ret, ErrorCode::NO_ERROR); diff --git a/test/unittest/cpp_test/src/input_method_private_member_test.cpp b/test/unittest/cpp_test/src/input_method_private_member_test.cpp index 931ef0e9a..2e0cce016 100644 --- a/test/unittest/cpp_test/src/input_method_private_member_test.cpp +++ b/test/unittest/cpp_test/src/input_method_private_member_test.cpp @@ -1428,7 +1428,7 @@ HWTEST_F(InputMethodPrivateMemberTest, BranchCoverage004, TestSize.Level0) EXPECT_FALSE(ret2); ret2 = clientGroup->IsCurClientFocused(-1, -1); EXPECT_FALSE(ret2); - ret2 = clientGroup->IsCurClientUnFocused(-1, -1); + ret2 = clientGroup->IsCurClientUnfocused(-1, -1); EXPECT_FALSE(ret2); auto startRet = userSession->StartInputService(nullptr); EXPECT_EQ(startRet, ErrorCode::ERROR_IMSA_IME_TO_START_NULLPTR); -- Gitee