From c805ea83b80360dfd28b82102ec491ea7095226d Mon Sep 17 00:00:00 2001 From: lwk <1076278852@qq.com> Date: Tue, 15 Mar 2022 22:14:44 +0800 Subject: [PATCH] fix: codex review liuwenkai@huawei.com Signed-off-by: lwk <1076278852@qq.com> --- .../kitsimpl/src/auth_executor_registry.cpp | 18 ++++- frameworks/kitsimpl/src/co_auth.cpp | 14 +++- services/src/auth_res_manager.cpp | 65 +++++++++---------- services/src/coauth_manager.cpp | 7 +- 4 files changed, 64 insertions(+), 40 deletions(-) diff --git a/frameworks/kitsimpl/src/auth_executor_registry.cpp b/frameworks/kitsimpl/src/auth_executor_registry.cpp index 807edd6..e550f82 100644 --- a/frameworks/kitsimpl/src/auth_executor_registry.cpp +++ b/frameworks/kitsimpl/src/auth_executor_registry.cpp @@ -43,7 +43,11 @@ sptr AuthExecutorRegistry::GetProxy() COAUTH_HILOGE(MODULE_INNERKIT, "Failed to get coauth service"); return nullptr; } - sptr dr = new AuthExecutorRegistryDeathRecipient(); + sptr dr = new (std::nothrow) AuthExecutorRegistryDeathRecipient(); + if (dr == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "dr is nullptr"); + return nullptr; + } if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) { COAUTH_HILOGE(MODULE_INNERKIT, "Failed to add death recipient"); return nullptr; @@ -81,7 +85,11 @@ uint64_t AuthExecutorRegistry::Register(std::shared_ptr executorIn COAUTH_HILOGE(MODULE_INNERKIT, "GetProxy is nullptr"); return FAIL; } - sptr iExecutorCallback = new ExecutorCallbackStub(callback); + sptr iExecutorCallback = new (std::nothrow) ExecutorCallbackStub(callback); + if (iExecutorCallback == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "iExecutorCallback is nullptr"); + return FAIL; + } return proxy->Register(executorInfo, iExecutorCallback); } @@ -97,7 +105,11 @@ void AuthExecutorRegistry::QueryStatus(AuthExecutor &executorInfo, std::shared_p COAUTH_HILOGE(MODULE_INNERKIT, "GetProxy is nullptr"); return; } - sptr iQueryCallback = new QueryCallbackStub(callback); + sptr iQueryCallback = new (std::nothrow) QueryCallbackStub(callback); + if (iQueryCallback == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "iQueryCallback is nullptr"); + return; + } return proxy->QueryStatus(executorInfo, iQueryCallback); } diff --git a/frameworks/kitsimpl/src/co_auth.cpp b/frameworks/kitsimpl/src/co_auth.cpp index b8a5fb7..d7c13d4 100644 --- a/frameworks/kitsimpl/src/co_auth.cpp +++ b/frameworks/kitsimpl/src/co_auth.cpp @@ -44,7 +44,11 @@ sptr CoAuth::GetProxy() COAUTH_HILOGE(MODULE_INNERKIT, "Failed to get coauth manager service"); return nullptr; } - sptr dr = new CoAuthDeathRecipient(); + sptr dr = new (std::nothrow) CoAuthDeathRecipient(); + if (dr == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "dr is nullptr"); + return nullptr; + } if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) { COAUTH_HILOGE(MODULE_INNERKIT, "Failed to add death recipient"); return nullptr; @@ -83,7 +87,7 @@ void CoAuth::BeginSchedule(uint64_t scheduleId, AuthInfo &authInfo, std::shared_ return; } - sptr icoAuthCallback = new CoAuthCallbackStub(callback); + sptr icoAuthCallback = new (std::nothrow) CoAuthCallbackStub(callback); if (icoAuthCallback == nullptr) { COAUTH_HILOGE(MODULE_INNERKIT, "BeginSchedule failed, icoAuthCallback is nullptr"); return; @@ -125,7 +129,11 @@ void CoAuth::SetExecutorProp(AuthResPool::AuthAttributes &conditions, std::share return; } - sptr iSetExecutorCallback = new SetPropCallbackStub(callback); + sptr iSetExecutorCallback = new (std::nothrow) SetPropCallbackStub(callback); + if (iSetExecutorCallback == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "iSetExecutorCallback is nullptr"); + return; + } return proxy->SetExecutorProp(conditions, iSetExecutorCallback); } diff --git a/services/src/auth_res_manager.cpp b/services/src/auth_res_manager.cpp index cefc687..b02b9fa 100644 --- a/services/src/auth_res_manager.cpp +++ b/services/src/auth_res_manager.cpp @@ -23,53 +23,52 @@ namespace CoAuth { /* Register the executor, pass in the executor information and the callback returns the executor ID. */ uint64_t AuthResManager::Register(std::shared_ptr executorInfo, sptr callback) { - int32_t result = SUCCESS; - uint64_t executorId = INVALID_EXECUTOR_ID; if (executorInfo == nullptr || callback == nullptr) { COAUTH_HILOGE(MODULE_SERVICE, "executorInfo or callback is nullptr"); - return executorId; + return INVALID_EXECUTOR_ID; } ExecutorInfo info; std::vector publicKey; - AuthType authType; - ExecutorSecureLevel esl; - ExecutorType exeType; - executorInfo->GetAuthType(authType); + executorInfo->GetAuthType(info.authType); executorInfo->GetAuthAbility(info.authAbility); - executorInfo->GetExecutorSecLevel(esl); - executorInfo->GetExecutorType(exeType); + executorInfo->GetExecutorSecLevel(info.esl); + executorInfo->GetExecutorType(info.executorType); executorInfo->GetPublicKey(publicKey); - info.authType = authType; - info.esl = esl; - info.executorType = exeType; if (publicKey.size() > PUBLIC_KEY_LEN) { COAUTH_HILOGE(MODULE_SERVICE, "publicKey length too long"); - return executorId; - } else { - for (std::size_t i = 0; i < publicKey.size(); i++) { - info.publicKey[i] = publicKey[i]; - } + return INVALID_EXECUTOR_ID; } - result = ExecutorRegister(info, executorId); - if (result == SUCCESS) { - sptr dr = new ResIExecutorCallbackDeathRecipient(executorId, this); - if (!callback->AsObject()->AddDeathRecipient(dr)) { - COAUTH_HILOGE(MODULE_SERVICE, "Failed to add death recipient ResIExecutorCallbackDeathRecipient"); - return INVALID_EXECUTOR_ID; - } - coAuthResPool_.Insert(executorId, executorInfo, callback); - COAUTH_HILOGI(MODULE_SERVICE, "register is successfull!"); - // Assign messenger - sptr messenger = - new UserIAM::AuthResPool::ExecutorMessenger(&coAuthResPool_); - callback->OnMessengerReady(messenger); - COAUTH_HILOGD(MODULE_SERVICE, "register is successfull,exeID is XXXX%{public}04" PRIx64, executorId); - return executorId; + for (std::size_t i = 0; i < publicKey.size(); i++) { + info.publicKey[i] = publicKey[i]; } - if (result == FAIL) { + + uint64_t executorId = INVALID_EXECUTOR_ID; + int32_t result = ExecutorRegister(info, executorId); + if (result != SUCCESS) { COAUTH_HILOGE(MODULE_SERVICE, "register is failure!"); return INVALID_EXECUTOR_ID; } + sptr dr = + new (std::nothrow) ResIExecutorCallbackDeathRecipient(executorId, this); + if (dr == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "dr is nullptr"); + return INVALID_EXECUTOR_ID; + } + if (!callback->AsObject()->AddDeathRecipient(dr)) { + COAUTH_HILOGE(MODULE_SERVICE, "Failed to add death recipient ResIExecutorCallbackDeathRecipient"); + return INVALID_EXECUTOR_ID; + } + coAuthResPool_.Insert(executorId, executorInfo, callback); + COAUTH_HILOGI(MODULE_SERVICE, "register is successfull!"); + // Assign messenger + sptr messenger = + new (std::nothrow) UserIAM::AuthResPool::ExecutorMessenger(&coAuthResPool_); + if (messenger == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "messenger is nullptr"); + return INVALID_EXECUTOR_ID; + } + callback->OnMessengerReady(messenger); + COAUTH_HILOGD(MODULE_SERVICE, "register is successfull,exeID is XXXX%{public}04" PRIx64, executorId); return executorId; } diff --git a/services/src/coauth_manager.cpp b/services/src/coauth_manager.cpp index ac8af95..f48a059 100644 --- a/services/src/coauth_manager.cpp +++ b/services/src/coauth_manager.cpp @@ -45,7 +45,12 @@ void CoAuthManager::CoAuthHandle(uint64_t scheduleId, AuthInfo &authInfo, sptrOnFinish(FAIL, scheduleToken); } - sptr dr = new ResICoAuthCallbackDeathRecipient(scheduleId, this); + sptr dr = + new (std::noboolalpha) ResICoAuthCallbackDeathRecipient(scheduleId, this); + if (dr == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "dr is nullptr."); + return callback->OnFinish(FAIL, scheduleToken); + } if ((!callback->AsObject()->AddDeathRecipient(dr))) { COAUTH_HILOGE(MODULE_SERVICE, "Failed to add death recipient ResICoAuthCallbackDeathRecipient"); }