From 072ccfe1dedc8cc4e02cc732b81b0212b9ac7478 Mon Sep 17 00:00:00 2001 From: wangxu43 Date: Sat, 5 Mar 2022 16:43:18 +0800 Subject: [PATCH] Review fix Signed-off-by: wangxu43 --- .../innerkits/napi/src/authapi_callback.cpp | 9 +++ .../innerkits/napi/src/result_convert.cpp | 8 +-- .../innerkits/napi/src/user_auth_helper.cpp | 62 +++++++++++++++---- .../innerkits/napi/src/user_auth_impl.cpp | 21 ++++--- services/src/userauth_service.cpp | 26 +++----- 5 files changed, 82 insertions(+), 44 deletions(-) diff --git a/interfaces/innerkits/napi/src/authapi_callback.cpp b/interfaces/innerkits/napi/src/authapi_callback.cpp index 5726121..5f3190a 100644 --- a/interfaces/innerkits/napi/src/authapi_callback.cpp +++ b/interfaces/innerkits/napi/src/authapi_callback.cpp @@ -45,6 +45,15 @@ AuthApiCallback::AuthApiCallback(ExecuteInfo *executeInfo) AuthApiCallback::~AuthApiCallback() { + if (authInfo_ != nullptr) { + delete authInfo_; + } + if (userInfo_ != nullptr) { + delete userInfo_; + } + if (executeInfo_ != nullptr) { + delete executeInfo_; + } } napi_value AuthApiCallback::Uint8ArrayToNapi(napi_env env, std::vector value) diff --git a/interfaces/innerkits/napi/src/result_convert.cpp b/interfaces/innerkits/napi/src/result_convert.cpp index 170dd6b..b3a359d 100644 --- a/interfaces/innerkits/napi/src/result_convert.cpp +++ b/interfaces/innerkits/napi/src/result_convert.cpp @@ -32,14 +32,12 @@ ResultConvert::~ResultConvert() napi_value ResultConvert::Uint64ToUint8Napi(napi_env env, uint64_t value) { USERAUTH_HILOGI(MODULE_JS_NAPI, "ResultConvert Uint64ToUint8Napi uint64_t %{public}" PRIu64 "", value); - size_t length = sizeof(value); void *data = nullptr; napi_value arrayBuffer = nullptr; - size_t bufferSize = length; - NAPI_CALL(env, napi_create_arraybuffer(env, bufferSize, &data, &arrayBuffer)); - memcpy_s(data, bufferSize, reinterpret_cast(&value), bufferSize); + NAPI_CALL(env, napi_create_arraybuffer(env, sizeof(value), &data, &arrayBuffer)); + (void)memcpy_s(data, sizeof(value), reinterpret_cast(&value), sizeof(value)); napi_value result = nullptr; - NAPI_CALL(env, napi_create_typedarray(env, napi_uint8_array, bufferSize, arrayBuffer, 0, &result)); + NAPI_CALL(env, napi_create_typedarray(env, napi_uint8_array, sizeof(value), arrayBuffer, 0, &result)); return result; } diff --git a/interfaces/innerkits/napi/src/user_auth_helper.cpp b/interfaces/innerkits/napi/src/user_auth_helper.cpp index 4a8edc9..977b101 100644 --- a/interfaces/innerkits/napi/src/user_auth_helper.cpp +++ b/interfaces/innerkits/napi/src/user_auth_helper.cpp @@ -31,13 +31,16 @@ namespace UserAuth { napi_value UserAuthServiceConstructor(napi_env env, napi_callback_info info) { USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); - std::shared_ptr userAuthImpl; - userAuthImpl.reset(new UserAuthImpl()); + UserAuthImpl *userAuthImpl = new (std::nothrow) UserAuthImpl(); + if (userAuthImpl == nullptr) { + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, get nullptr", __func__); + return nullptr; + } napi_value thisVar = nullptr; size_t argc = ARGS_ONE; napi_value argv[ARGS_ONE] = {nullptr}; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); - NAPI_CALL(env, napi_wrap(env, thisVar, userAuthImpl.get(), + NAPI_CALL(env, napi_wrap(env, thisVar, userAuthImpl, [](napi_env env, void *data, void *hint) { UserAuthImpl *userAuthImpl = static_cast(data); if (userAuthImpl != nullptr) { @@ -58,13 +61,17 @@ napi_value UserAuthServiceConstructor(napi_env env, napi_callback_info info) */ napi_value GetVersion(napi_env env, napi_callback_info info) { + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, GetVersion"); napi_value thisVar = nullptr; size_t argcAsync = 0; napi_value args[ARGS_MAX_COUNT] = {nullptr}; NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); UserAuthImpl *userAuthImpl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); - USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, GetVersion"); + if (userAuthImpl == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "UserAuthHelper, GetVersion get nullptr"); + return nullptr; + } return userAuthImpl->GetVersion(env, info); } @@ -77,13 +84,17 @@ napi_value GetVersion(napi_env env, napi_callback_info info) */ napi_value GetAvailableStatus(napi_env env, napi_callback_info info) { + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, getAvailableStatus"); napi_value thisVar = nullptr; size_t argcAsync = 0; napi_value args[ARGS_MAX_COUNT] = {nullptr}; NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); UserAuthImpl *userAuthImpl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); - USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, getAvailableStatus"); + if (userAuthImpl == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "UserAuthHelper, getAvailableStatus get nullptr"); + return nullptr; + } return userAuthImpl->GetAvailableStatus(env, info); } @@ -96,13 +107,17 @@ napi_value GetAvailableStatus(napi_env env, napi_callback_info info) */ napi_value GetProperty(napi_env env, napi_callback_info info) { + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, GetProperty"); napi_value thisVar = nullptr; size_t argcAsync = 0; napi_value args[ARGS_MAX_COUNT] = {nullptr}; NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); UserAuthImpl *userAuthImpl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); - USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, GetProperty"); + if (userAuthImpl == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "UserAuthHelper, GetProperty get nullptr"); + return nullptr; + } return userAuthImpl->GetProperty(env, info); } @@ -115,13 +130,17 @@ napi_value GetProperty(napi_env env, napi_callback_info info) */ napi_value SetProperty(napi_env env, napi_callback_info info) { + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, SetProperty"); napi_value thisVar = nullptr; size_t argcAsync = 0; napi_value args[ARGS_MAX_COUNT] = {nullptr}; NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); UserAuthImpl *userAuthImpl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); - USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, SetProperty"); + if (userAuthImpl == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "UserAuthHelper, SetProperty get nullptr"); + return nullptr; + } return userAuthImpl->SetProperty(env, info); } @@ -135,25 +154,33 @@ napi_value SetProperty(napi_env env, napi_callback_info info) */ napi_value Auth(napi_env env, napi_callback_info info) { + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, Auth"); napi_value thisVar = nullptr; size_t argcAsync = 0; napi_value args[ARGS_MAX_COUNT] = {nullptr}; NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); UserAuthImpl *userAuthImpl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); - USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, Auth"); + if (userAuthImpl == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "UserAuthHelper, Auth get nullptr"); + return nullptr; + } return userAuthImpl->Auth(env, info); } napi_value Execute(napi_env env, napi_callback_info info) { + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, Execute"); napi_value thisVar = nullptr; size_t argcAsync = 0; napi_value args[ARGS_MAX_COUNT] = {nullptr}; NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); UserAuthImpl *userAuthImpl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); - USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, Execute"); + if (userAuthImpl == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "UserAuthHelper, Execute get nullptr"); + return nullptr; + } return userAuthImpl->Execute(env, info); } @@ -167,13 +194,17 @@ napi_value Execute(napi_env env, napi_callback_info info) */ napi_value AuthUser(napi_env env, napi_callback_info info) { + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, AuthUser"); napi_value thisVar = nullptr; size_t argcAsync = 0; napi_value args[ARGS_MAX_COUNT] = {nullptr}; NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); UserAuthImpl *userAuthImpl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); - USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, AuthUser"); + if (userAuthImpl == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "UserAuthHelper, AuthUser get nullptr"); + return nullptr; + } return userAuthImpl->AuthUser(env, info); } @@ -186,13 +217,17 @@ napi_value AuthUser(napi_env env, napi_callback_info info) */ napi_value CancelAuth(napi_env env, napi_callback_info info) { + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, CancelAuth"); napi_value thisVar = nullptr; size_t argcAsync = 0; napi_value args[ARGS_MAX_COUNT] = {nullptr}; NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); UserAuthImpl *userAuthImpl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); - USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, CancelAuth"); + if (userAuthImpl == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "UserAuthHelper, CancelAuth get nullptr"); + return nullptr; + } return userAuthImpl->CancelAuth(env, info); } @@ -421,7 +456,8 @@ napi_value UserAuthInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("constructor", UserAuth::Constructor), DECLARE_NAPI_FUNCTION("getAuthenticator", UserAuth::ConstructorForAPI6), }; - status = napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs); + status = napi_define_properties(env, exports, + sizeof(exportFuncs) / sizeof(napi_property_descriptor), exportFuncs); if (status != napi_ok) { USERAUTH_HILOGE(MODULE_JS_NAPI, "napi_define_properties faild"); } @@ -446,7 +482,7 @@ napi_value EnumExport(napi_env env, napi_value exports) DECLARE_NAPI_PROPERTY("FaceTipsCode", FaceTipsCodeConstructor(env)), DECLARE_NAPI_PROPERTY("FingerprintTips", FingerprintTipsConstructor(env)), }; - napi_define_properties(env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors); + napi_define_properties(env, exports, sizeof(descriptors) / sizeof(napi_property_descriptor), descriptors); return exports; } diff --git a/interfaces/innerkits/napi/src/user_auth_impl.cpp b/interfaces/innerkits/napi/src/user_auth_impl.cpp index 102745e..6f10ba4 100644 --- a/interfaces/innerkits/napi/src/user_auth_impl.cpp +++ b/interfaces/innerkits/napi/src/user_auth_impl.cpp @@ -93,10 +93,10 @@ napi_value UserAuthImpl::GetProperty(napi_env env, napi_callback_info info) if (ret == nullptr) { USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s GetPropertyWrap fail", __func__); delete getPropertyInfo; - delete asyncHolder; if (asyncHolder->asyncWork != nullptr) { napi_delete_async_work(env, asyncHolder->asyncWork); } + delete asyncHolder; } return ret; } @@ -231,10 +231,10 @@ napi_value UserAuthImpl::SetProperty(napi_env env, napi_callback_info info) if (ret == nullptr) { USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s SetPropertyWrap fail", __func__); delete setPropertyInfo; - delete asyncHolder; if (asyncHolder->asyncWork != nullptr) { napi_delete_async_work(env, asyncHolder->asyncWork); } + delete asyncHolder; } return ret; } @@ -389,6 +389,14 @@ napi_value UserAuthImpl::BuildAuthInfo(napi_env env, AuthInfo *authInfo) napi_value UserAuthImpl::DoExecute(ExecuteInfo* executeInfo) { + bool isPromise = executeInfo->isPromise; + napi_value result = nullptr; + if (isPromise) { + result = executeInfo->promise; + } else { + napi_get_null(executeInfo->env, &result); + } + AuthApiCallback *object = new (std::nothrow) AuthApiCallback(executeInfo); if (object == nullptr) { delete executeInfo; @@ -407,14 +415,9 @@ napi_value UserAuthImpl::DoExecute(ExecuteInfo* executeInfo) delete executeInfo; return nullptr; } + UserAuth::GetInstance().Auth(0, FACE, convertAuthTurstLevel[executeInfo->level], callback); - if (executeInfo->isPromise) { - return executeInfo->promise; - } else { - napi_value result = nullptr; - NAPI_CALL(executeInfo->env, napi_get_null(executeInfo->env, &result)); - return result; - } + return result; } napi_value UserAuthImpl::Execute(napi_env env, napi_callback_info info) diff --git a/services/src/userauth_service.cpp b/services/src/userauth_service.cpp index 0c3e8a2..e10a9e2 100644 --- a/services/src/userauth_service.cpp +++ b/services/src/userauth_service.cpp @@ -159,7 +159,7 @@ void UserAuthService::SetProperty(const SetPropertyRequest request, sptronResult(E_CHECK_PERMISSION_FAILED, extraInfo); return; } @@ -271,7 +271,7 @@ uint64_t UserAuthService::AuthUser(const int32_t userId, const uint64_t challeng uint64_t contextID = 0; AuthSolution authSolutionParam; CoAuthInfo coAuthInfo; - AuthResult extraInfo; + AuthResult extraInfo = {}; sptr dr = new UserAuthServiceCallbackDeathRecipient(callback); if ((!callback->AsObject()->AddDeathRecipient(dr))) { @@ -318,38 +318,30 @@ int32_t UserAuthService::GetControllerData(sptr& callback, Au const AuthTurstLevel authTurstLevel, uint64_t &callerID, std::string &callerName, uint64_t &contextID) { - int ret = SUCCESS; - int result = SUCCESS; if (callback == nullptr) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService AuthUser IUserAuthCallback is NULL!"); - ret = FAIL; - return ret; + return FAIL; } if (ATL4 < authTurstLevel || authTurstLevel < ATL1) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService AuthUser AuthTurstLevel is NOT SUPPORT!"); callback->onResult(TRUST_LEVEL_NOT_SUPPORT, extraInfo); - ret = FAIL; - return ret; + return FAIL; } callerID = this->GetCallingUid(); callerName = std::to_string(callerID); - result = userauthController_.GenerateContextID(contextID); - if (result != SUCCESS) { + if (userauthController_.GenerateContextID(contextID) != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService AuthUser GenerateContextID is ERROR!"); callback->onResult(GENERAL_ERROR, extraInfo); - ret = FAIL; - return ret; + return FAIL; } - result = userauthController_.AddContextID(contextID); - if (result != SUCCESS) { + if (userauthController_.AddContextID(contextID) != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService AuthUser AddContextID is ERROR!"); callback->onResult(GENERAL_ERROR, extraInfo); - ret = FAIL; - return ret; + return FAIL; } - return ret; + return SUCCESS; } int32_t UserAuthService::CancelAuth(const uint64_t contextId)