diff --git a/common/include/dm_anonymous.h b/common/include/dm_anonymous.h index e34e3ee718991f0beed2c939b14f3b88bca7bd8d..4e47d6d11ffb37fd270466ed1e51ed0154cb5ced 100644 --- a/common/include/dm_anonymous.h +++ b/common/include/dm_anonymous.h @@ -50,7 +50,6 @@ bool CompareVersion(const std::string &remoteVersion, const std::string &oldVers std::string ComposeStr(const std::string &pkgName, uint16_t subscribeId); std::string GetCallerPkgName(const std::string &pkgName); uint16_t GetSubscribeId(const std::string &pkgName); -void DeleteArrayPtr(const char* arrPtr); template std::string GetAnonyInteger(const T value) { diff --git a/common/src/dm_anonymous.cpp b/common/src/dm_anonymous.cpp index 2ee4165d90a8f7001ea6951e3de04a62f82184e5..00716f295be40206c79d4b2cbce766832059dbdc 100644 --- a/common/src/dm_anonymous.cpp +++ b/common/src/dm_anonymous.cpp @@ -394,13 +394,5 @@ std::string GetSubStr(const std::string &rawStr, const std::string &separator, i LOGE("get failed"); return ""; } - -void DeleteArrayPtr(const char* arrPtr) -{ - if (arrPtr != nullptr) { - delete[] arrPtr; - arrPtr = nullptr; - } -} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/implementation/BUILD.gn b/services/implementation/BUILD.gn index 90e65327e887e0224bed5f4d232610c46a3ca24f..e841ebcdc2a98bbe0079b84cd092e6eebd835f71 100644 --- a/services/implementation/BUILD.gn +++ b/services/implementation/BUILD.gn @@ -216,6 +216,7 @@ if (defined(ohos_lite)) { "access_token:libaccesstoken_sdk", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", + "cJSON:cjson", "c_utils:utils", "common_event_service:cesfwk_core", "common_event_service:cesfwk_innerkits", diff --git a/services/implementation/include/dependency/hichain/hichain_auth_connector.h b/services/implementation/include/dependency/hichain/hichain_auth_connector.h index 9cd87c20d3a4432773b7482afadc04b45943c499..cc92df1fc4caae23c946e6570ff00832f28e15b1 100644 --- a/services/implementation/include/dependency/hichain/hichain_auth_connector.h +++ b/services/implementation/include/dependency/hichain/hichain_auth_connector.h @@ -58,6 +58,8 @@ public: int32_t DeleteCredential(const std::string &deviceId, int32_t userId); int32_t RegisterHiChainAuthCallback(std::shared_ptr callback); int32_t GetCredential(std::string &localUdid, int32_t osAccountId, std::string &publicKey); +private: + void FreeJsonString(char *jsonStr); private: DeviceAuthCallback deviceAuthCallback_; diff --git a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp index 4bce015e48a48ebc839788d4e3f95341e0d3e5fe..88fdf7478dbaa351541a25cbca66ff2a2cd11e6b 100644 --- a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp @@ -18,6 +18,7 @@ #include "dm_anonymous.h" #include "hichain_connector_callback.h" #include "parameter.h" +#include "cJSON.h" namespace OHOS { namespace DistributedHardware { @@ -25,6 +26,14 @@ namespace DistributedHardware { std::shared_ptr HiChainAuthConnector::dmDeviceAuthCallback_ = nullptr; std::mutex HiChainAuthConnector::dmDeviceAuthCallbackMutex_; +void HiChainAuthConnector::FreeJsonString(char *jsonStr) +{ + if (jsonStr != nullptr) { + cJSON_free(jsonStr); + jsonStr = nullptr; + } +} + HiChainAuthConnector::HiChainAuthConnector() { int32_t ret = InitDeviceAuthService(); @@ -82,7 +91,7 @@ int32_t HiChainAuthConnector::ProcessAuthData(int64_t requestId, std::string aut } return DM_OK; } - + bool HiChainAuthConnector::onTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen) { LOGI("AuthDevice onTransmit, requestId %{public}" PRId64, requestId); @@ -165,11 +174,11 @@ int32_t HiChainAuthConnector::GenerateCredential(std::string &localUdid, int32_t char *returnData = nullptr; if (ProcessCredential(CRED_OP_CREATE, requestParam.c_str(), &returnData) != HC_SUCCESS) { LOGE("Hichain generate credential failed."); - DeleteArrayPtr(returnData); + FreeJsonString(returnData); return ERR_DM_FAILED; } std::string returnDataStr = static_cast(returnData); - DeleteArrayPtr(returnData); + FreeJsonString(returnData); nlohmann::json jsonObject = nlohmann::json::parse(returnDataStr, nullptr, false); if (jsonObject.is_discarded()) { LOGE("Decode generate return data jsonStr error."); @@ -200,11 +209,11 @@ bool HiChainAuthConnector::QueryCredential(std::string &localUdid, int32_t osAcc char *returnData = nullptr; if (ProcessCredential(CRED_OP_QUERY, requestParam.c_str(), &returnData) != HC_SUCCESS) { LOGE("Hichain query credential failed."); - DeleteArrayPtr(returnData); + FreeJsonString(returnData); return false; } std::string returnDataStr = static_cast(returnData); - DeleteArrayPtr(returnData); + FreeJsonString(returnData); nlohmann::json jsonObject = nlohmann::json::parse(returnDataStr, nullptr, false); if (jsonObject.is_discarded()) { LOGE("Decode query return data jsonStr error."); @@ -233,11 +242,11 @@ int32_t HiChainAuthConnector::GetCredential(std::string &localUdid, int32_t osAc char *returnData = nullptr; if (ProcessCredential(CRED_OP_QUERY, requestParam.c_str(), &returnData) != HC_SUCCESS) { LOGE("Hichain query credential failed."); - DeleteArrayPtr(returnData); + FreeJsonString(returnData); return ERR_DM_FAILED; } std::string returnDataStr = static_cast(returnData); - DeleteArrayPtr(returnData); + FreeJsonString(returnData); nlohmann::json jsonObject = nlohmann::json::parse(returnDataStr, nullptr, false); if (jsonObject.is_discarded()) { LOGE("Decode query return data jsonStr error."); @@ -267,11 +276,11 @@ int32_t HiChainAuthConnector::ImportCredential(int32_t osAccountId, std::string char *returnData = nullptr; if (ProcessCredential(CRED_OP_IMPORT, requestParam.c_str(), &returnData) != HC_SUCCESS) { LOGE("Hichain query credential failed."); - DeleteArrayPtr(returnData); + FreeJsonString(returnData); return ERR_DM_FAILED; } std::string returnDataStr = static_cast(returnData); - DeleteArrayPtr(returnData); + FreeJsonString(returnData); nlohmann::json jsonObject = nlohmann::json::parse(returnDataStr, nullptr, false); if (jsonObject.is_discarded()) { LOGE("Decode import return data jsonStr error."); @@ -300,11 +309,11 @@ int32_t HiChainAuthConnector::DeleteCredential(const std::string &deviceId, int3 char *returnData = nullptr; if (ProcessCredential(CRED_OP_DELETE, requestParam.c_str(), &returnData) != HC_SUCCESS) { LOGE("Hichain query credential failed."); - DeleteArrayPtr(returnData); + FreeJsonString(returnData); return false; } std::string returnDataStr = static_cast(returnData); - DeleteArrayPtr(returnData); + FreeJsonString(returnData); nlohmann::json jsonObject = nlohmann::json::parse(returnDataStr, nullptr, false); if (jsonObject.is_discarded()) { LOGE("Decode import return data jsonStr error."); diff --git a/services/implementation/src/dependency/hichain/hichain_connector.cpp b/services/implementation/src/dependency/hichain/hichain_connector.cpp index 14adb571ccd167b69e80645100a9c5806a6c7e4e..ab9b1876c214e10cfde8fe2bdee18bb53361d97d 100644 --- a/services/implementation/src/dependency/hichain/hichain_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_connector.cpp @@ -243,7 +243,7 @@ bool HiChainConnector::GetGroupInfoCommon(const int32_t userId, const std::strin int32_t ret = deviceGroupManager_->getGroupInfo(userId, pkgName, queryParams.c_str(), &groupVec, &num); if (ret != 0) { LOGE("[HICHAIN]fail to get group info with ret:%{public}d.", ret); - DeleteArrayPtr(groupVec); + deviceGroupManager_->destroyInfo(&groupVec); return false; } if (groupVec == nullptr) { @@ -252,13 +252,12 @@ bool HiChainConnector::GetGroupInfoCommon(const int32_t userId, const std::strin } if (num == 0) { LOGE("[HICHAIN]return groups info number is zero."); - DeleteArrayPtr(groupVec); + deviceGroupManager_->destroyInfo(&groupVec); return false; } LOGI("HiChainConnector::GetGroupInfo groupNum(%{public}u)", num); std::string relatedGroups = std::string(groupVec); deviceGroupManager_->destroyInfo(&groupVec); - DeleteArrayPtr(groupVec); nlohmann::json jsonObject = nlohmann::json::parse(relatedGroups, nullptr, false); if (jsonObject.is_discarded()) { LOGE("returnGroups parse error"); @@ -852,13 +851,12 @@ int32_t HiChainConnector::getRegisterInfo(const std::string &queryParams, std::s char *credentialInfo = nullptr; if (deviceGroupManager_->getRegisterInfo(queryParams.c_str(), &credentialInfo) != DM_OK) { LOGE("[HICHAIN]fail to request hichain registerinfo."); - DeleteArrayPtr(credentialInfo); + deviceGroupManager_->destroyInfo(&credentialInfo); return ERR_DM_FAILED; } returnJsonStr = credentialInfo; deviceGroupManager_->destroyInfo(&credentialInfo); - DeleteArrayPtr(credentialInfo); LOGI("request hichain device registerinfo successfully."); return DM_OK; } @@ -1083,6 +1081,7 @@ std::vector HiChainConnector::GetTrustedDevices(const std::string & &devicesJson, &devNum); if (ret != 0 || devicesJson == nullptr) { LOGE("[HICHAIN]failed to get trusted devicesJson, ret: %{public}d", ret); + deviceGroupManager_->destroyInfo(&devicesJson); return {}; } GetTrustedDevicesUdid(devicesJson, trustedDevices); @@ -1151,7 +1150,7 @@ int32_t HiChainConnector::GetRelatedGroupsCommon(const std::string &deviceId, co deviceGroupManager_->getRelatedGroups(userId, pkgName, deviceId.c_str(), &returnGroups, &groupNum); if (ret != 0) { LOGE("[HICHAIN] fail to get related groups with ret:%{public}d.", ret); - DeleteArrayPtr(returnGroups); + deviceGroupManager_->destroyInfo(&returnGroups); return ERR_DM_FAILED; } if (returnGroups == nullptr) { @@ -1160,11 +1159,11 @@ int32_t HiChainConnector::GetRelatedGroupsCommon(const std::string &deviceId, co } if (groupNum == 0) { LOGE("[HICHAIN]return related goups number is zero."); - DeleteArrayPtr(returnGroups); + deviceGroupManager_->destroyInfo(&returnGroups); return ERR_DM_FAILED; } std::string relatedGroups = std::string(returnGroups); - DeleteArrayPtr(returnGroups); + deviceGroupManager_->destroyInfo(&returnGroups); nlohmann::json jsonObject = nlohmann::json::parse(relatedGroups, nullptr, false); if (jsonObject.is_discarded()) { LOGE("returnGroups parse error"); @@ -1215,7 +1214,7 @@ int32_t HiChainConnector::GetRelatedGroupsCommon(int32_t userId, const std::stri deviceGroupManager_->getRelatedGroups(userId, pkgName, deviceId.c_str(), &returnGroups, &groupNum); if (ret != 0) { LOGE("[HICHAIN] fail to get related groups with ret:%{public}d.", ret); - DeleteArrayPtr(returnGroups); + deviceGroupManager_->destroyInfo(&returnGroups); return ERR_DM_FAILED; } if (returnGroups == nullptr) { @@ -1224,11 +1223,11 @@ int32_t HiChainConnector::GetRelatedGroupsCommon(int32_t userId, const std::stri } if (groupNum == 0) { LOGE("[HICHAIN]return related goups number is zero."); - DeleteArrayPtr(returnGroups); + deviceGroupManager_->destroyInfo(&returnGroups); return ERR_DM_FAILED; } std::string relatedGroups = std::string(returnGroups); - DeleteArrayPtr(returnGroups); + deviceGroupManager_->destroyInfo(&returnGroups); nlohmann::json jsonObject = nlohmann::json::parse(relatedGroups, nullptr, false); if (jsonObject.is_discarded()) { LOGE("returnGroups parse error");