From 41b645484e979713905f09c054530265d57e7bf5 Mon Sep 17 00:00:00 2001 From: tangfan Date: Tue, 5 Dec 2023 20:58:36 +0800 Subject: [PATCH 1/2] fix some bug Signed-off-by: tangfan --- .../core/src/dbstorage/device_profile_storage.cpp | 4 ++-- .../core/src/dbstorage/device_profile_storage_manager.cpp | 8 ++++++-- .../core/src/persistenceadapter/kvadapter/kv_adapter.cpp | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/old/services/core/src/dbstorage/device_profile_storage.cpp b/old/services/core/src/dbstorage/device_profile_storage.cpp index d4fb6bc6..4a3f93b3 100644 --- a/old/services/core/src/dbstorage/device_profile_storage.cpp +++ b/old/services/core/src/dbstorage/device_profile_storage.cpp @@ -36,7 +36,7 @@ using namespace std::chrono_literals; namespace { const std::string TAG = "DeviceProfileStorage"; -constexpr int32_t RETRY_TIMES_GET_KVSTORE = 500; +constexpr int32_t RETRY_TIMES_GET_KVSTORE = 30; } DeviceProfileStorage::DeviceProfileStorage(const std::string& appId, const std::string& storeId) @@ -133,7 +133,7 @@ bool DeviceProfileStorage::TryGetKvStore() return true; } HILOGD("retry get kvstore..."); - std::this_thread::sleep_for(10ms); + std::this_thread::sleep_for(500ms); retryTimes++; } if (kvStorePtr_ == nullptr) { diff --git a/old/services/core/src/dbstorage/device_profile_storage_manager.cpp b/old/services/core/src/dbstorage/device_profile_storage_manager.cpp index 1789a8fd..b6d0cab7 100644 --- a/old/services/core/src/dbstorage/device_profile_storage_manager.cpp +++ b/old/services/core/src/dbstorage/device_profile_storage_manager.cpp @@ -262,8 +262,12 @@ void DeviceProfileStorageManager::SetServiceType(const std::string& udid, HILOGE("parse error"); return; } + if (!jsonData.contains(serviceId)) { + HILOGE("jsonData don't has serviceId attribute!"); + return; + } auto typeData = jsonData[serviceId]; - if (typeData != nullptr && typeData[SERVICE_TYPE] != nullptr) { + if (typeData != nullptr && typeData.contains(SERVICE_TYPE) && typeData[SERVICE_TYPE] != nullptr) { profile.SetServiceType(typeData[SERVICE_TYPE]); } } @@ -277,7 +281,7 @@ int32_t DeviceProfileStorageManager::DeleteDeviceProfile(const std::string& serv } std::unique_lock autoLock(serviceLock_); - if (servicesJson_[serviceId] == nullptr) { + if (servicesJson_.contains(serviceId) && servicesJson_[serviceId] == nullptr) { HILOGW("can't find service %{public}s", serviceId.c_str()); return ERR_DP_INVALID_PARAMS; } diff --git a/services/core/src/persistenceadapter/kvadapter/kv_adapter.cpp b/services/core/src/persistenceadapter/kvadapter/kv_adapter.cpp index ca0652c8..48919ad4 100644 --- a/services/core/src/persistenceadapter/kvadapter/kv_adapter.cpp +++ b/services/core/src/persistenceadapter/kvadapter/kv_adapter.cpp @@ -29,7 +29,7 @@ namespace DistributedDeviceProfile { using namespace OHOS::DistributedKv; namespace { constexpr int32_t MAX_INIT_RETRY_TIMES = 30; - constexpr int32_t INIT_RETRY_SLEEP_INTERVAL = 50 * 1000; // 50ms + constexpr int32_t INIT_RETRY_SLEEP_INTERVAL = 500 * 1000; // 50ms const std::string DATABASE_DIR = "/data/service/el1/public/database/distributed_device_profile_service"; const std::string TAG = "KVAdapter"; } @@ -241,7 +241,7 @@ DistributedKv::Status KVAdapter::GetKvStorePtr() .createIfMissing = true, .encrypt = false, .autoSync = true, - .securityLevel = DistributedKv::SecurityLevel::S0, + .securityLevel = DistributedKv::SecurityLevel::S1, .area = 1, .kvStoreType = KvStoreType::SINGLE_VERSION, .baseDir = DATABASE_DIR -- Gitee From 4f4ca62854a988e7aa053f108c5c8903f0d0091f Mon Sep 17 00:00:00 2001 From: tangfan Date: Wed, 6 Dec 2023 10:46:53 +0800 Subject: [PATCH 2/2] add json valid Signed-off-by: tangfan --- old/services/core/src/authority/authority_manager.cpp | 4 ++-- old/services/core/src/authority/trust_group_manager.cpp | 4 ++++ .../core/src/dbstorage/device_profile_storage_manager.cpp | 8 ++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/old/services/core/src/authority/authority_manager.cpp b/old/services/core/src/authority/authority_manager.cpp index a698d0dc..d50cb0ca 100644 --- a/old/services/core/src/authority/authority_manager.cpp +++ b/old/services/core/src/authority/authority_manager.cpp @@ -178,7 +178,7 @@ bool AuthorityManager::ValidateService(const nlohmann::json& authValJson, bool r bool AuthorityManager::CheckInterfaceAuthority(const std::string& ifaceName) { std::string procName = GetCallingProcName(); - if (!authJson_.contains(procName)) { + if (!authJson_.contains(procName) && !authJson_[procName].contains(INTERFACES)) { HILOGE("can't find entry for proc:%{public}s", procName.c_str()); return false; } @@ -214,7 +214,7 @@ bool AuthorityManager::CheckServicesAuthority(AuthValue authVal, } std::string procName = GetCallingProcName(); - if (!authJson_.contains(procName)) { + if (!authJson_.contains(procName) && !authJson_[procName].contains(SERVICES)) { HILOGE("can't find entry for proc:%{public}s", procName.c_str()); return false; } diff --git a/old/services/core/src/authority/trust_group_manager.cpp b/old/services/core/src/authority/trust_group_manager.cpp index 47f07599..f341c1de 100644 --- a/old/services/core/src/authority/trust_group_manager.cpp +++ b/old/services/core/src/authority/trust_group_manager.cpp @@ -114,6 +114,10 @@ bool TrustGroupManager::CheckGroupsInfo(const char* returnGroups, uint32_t group return false; } + if (!jsonObject.is_array()) { + HILOGE("jsonObject is not array!"); + return false; + } std::vector groupInfos = jsonObject.get>(); for (const auto& groupInfo : groupInfos) { // check group visibility is whether public or not diff --git a/old/services/core/src/dbstorage/device_profile_storage_manager.cpp b/old/services/core/src/dbstorage/device_profile_storage_manager.cpp index b6d0cab7..1f75eb21 100644 --- a/old/services/core/src/dbstorage/device_profile_storage_manager.cpp +++ b/old/services/core/src/dbstorage/device_profile_storage_manager.cpp @@ -151,7 +151,7 @@ int32_t DeviceProfileStorageManager::PutDeviceProfile(const ServiceCharacteristi keys.emplace_back(GenerateKey(localUdid_, serviceId, KeyType::SERVICE)); values.emplace_back(profile.GetCharacteristicProfileJson()); std::unique_lock autoLock(serviceLock_); - if (servicesJson_[serviceId] == nullptr) { + if (servicesJson_.contains(serviceId) && servicesJson_[serviceId] == nullptr) { nlohmann::json j; j[SERVICE_TYPE] = profile.GetServiceType(); servicesJson_[serviceId] = j; @@ -243,8 +243,12 @@ void DeviceProfileStorageManager::SetServiceType(const std::string& udid, { std::unique_lock autoLock(serviceLock_); if (udid.empty()) { + if (!servicesJson_.contains(serviceId)) { + HILOGE("ServicesJson not contains serviceId!"); + return; + } auto jsonData = servicesJson_[serviceId]; - if (jsonData != nullptr) { + if (jsonData != nullptr && jsonData.contains(SERVICE_TYPE)) { profile.SetServiceType(jsonData[SERVICE_TYPE]); } return; -- Gitee