From 4e93e30de95c13c08255669f5b2a625edd114ac3 Mon Sep 17 00:00:00 2001 From: l60055366 Date: Thu, 16 Jan 2025 10:32:06 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=8E=92=E6=9F=A5dm=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=9A=84hichain=E5=9B=9E=E8=B0=83=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: l60055366 --- common/src/dm_anonymous.cpp | 3 ++- services/implementation/BUILD.gn | 1 + .../dependency/hichain/hichain_connector.cpp | 21 +++++++++---------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/common/src/dm_anonymous.cpp b/common/src/dm_anonymous.cpp index 2ee4165d9..8aa4edffa 100644 --- a/common/src/dm_anonymous.cpp +++ b/common/src/dm_anonymous.cpp @@ -16,6 +16,7 @@ #include "dm_anonymous.h" #include "dm_log.h" #include +#include "cJSON.h" namespace OHOS { namespace DistributedHardware { namespace { @@ -398,7 +399,7 @@ std::string GetSubStr(const std::string &rawStr, const std::string &separator, i void DeleteArrayPtr(const char* arrPtr) { if (arrPtr != nullptr) { - delete[] arrPtr; + cJSON_free(arrPtr); arrPtr = nullptr; } } diff --git a/services/implementation/BUILD.gn b/services/implementation/BUILD.gn index 90e65327e..e841ebcdc 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/src/dependency/hichain/hichain_connector.cpp b/services/implementation/src/dependency/hichain/hichain_connector.cpp index 14adb571c..ab9b1876c 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"); -- Gitee From 44ed78618368c60b564328af96efecef7f377396 Mon Sep 17 00:00:00 2001 From: l60055366 Date: Thu, 16 Jan 2025 11:08:27 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=8E=92=E6=9F=A5dm=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=9A=84hichain=E5=9B=9E=E8=B0=83=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: l60055366 --- common/include/dm_anonymous.h | 1 - common/src/dm_anonymous.cpp | 9 ------ .../hichain/hichain_auth_connector.cpp | 29 ++++++++++++------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/common/include/dm_anonymous.h b/common/include/dm_anonymous.h index e34e3ee71..4e47d6d11 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 8aa4edffa..00716f295 100644 --- a/common/src/dm_anonymous.cpp +++ b/common/src/dm_anonymous.cpp @@ -16,7 +16,6 @@ #include "dm_anonymous.h" #include "dm_log.h" #include -#include "cJSON.h" namespace OHOS { namespace DistributedHardware { namespace { @@ -395,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) { - cJSON_free(arrPtr); - arrPtr = nullptr; - } -} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp index 4bce015e4..373ad71d0 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_; +static void FreeJsonString(const char *jsonStr) +{ + if (jsonStr != nullptr) { + cJSON_free(jsonStr); + jsonStr = nullptr; + } +} + HiChainAuthConnector::HiChainAuthConnector() { int32_t ret = InitDeviceAuthService(); @@ -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."); -- Gitee From 2b27acd3779a718180127bcb287ae10668da2c40 Mon Sep 17 00:00:00 2001 From: l60055366 Date: Thu, 16 Jan 2025 11:57:05 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=8E=92=E6=9F=A5dm=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=9A=84hichain=E5=9B=9E=E8=B0=83=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: l60055366 --- .../src/dependency/hichain/hichain_auth_connector.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp index 373ad71d0..16dd4386b 100644 --- a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp @@ -26,7 +26,7 @@ namespace DistributedHardware { std::shared_ptr HiChainAuthConnector::dmDeviceAuthCallback_ = nullptr; std::mutex HiChainAuthConnector::dmDeviceAuthCallbackMutex_; -static void FreeJsonString(const char *jsonStr) +static void FreeJsonString(char *jsonStr) { if (jsonStr != nullptr) { cJSON_free(jsonStr); @@ -91,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); -- Gitee From 54735166d7eaf489cde0ffcf1867cca71a7b74a0 Mon Sep 17 00:00:00 2001 From: l60055366 Date: Thu, 16 Jan 2025 16:16:23 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=8E=92=E6=9F=A5dm=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=9A=84hichain=E5=9B=9E=E8=B0=83=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: l60055366 --- .../include/dependency/hichain/hichain_auth_connector.h | 2 ++ .../src/dependency/hichain/hichain_auth_connector.cpp | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/services/implementation/include/dependency/hichain/hichain_auth_connector.h b/services/implementation/include/dependency/hichain/hichain_auth_connector.h index 9cd87c20d..cc92df1fc 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 16dd4386b..88fdf7478 100644 --- a/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp +++ b/services/implementation/src/dependency/hichain/hichain_auth_connector.cpp @@ -26,7 +26,7 @@ namespace DistributedHardware { std::shared_ptr HiChainAuthConnector::dmDeviceAuthCallback_ = nullptr; std::mutex HiChainAuthConnector::dmDeviceAuthCallbackMutex_; -static void FreeJsonString(char *jsonStr) +void HiChainAuthConnector::FreeJsonString(char *jsonStr) { if (jsonStr != nullptr) { cJSON_free(jsonStr); -- Gitee