diff --git a/common/utils/include/constants.h b/common/utils/include/constants.h index 5e2a6bea57c34fe9807c9cdf7899ded960e30110..52bd4b77dd73ec49e3e54992a3eb7acf999f5ce7 100644 --- a/common/utils/include/constants.h +++ b/common/utils/include/constants.h @@ -36,6 +36,11 @@ namespace DistributedHardware { constexpr int32_t MODE_ENABLE = 0; constexpr int32_t MODE_DISABLE = 1; constexpr uint32_t MAX_SWITCH_SIZE = 256; + constexpr uint32_t MAX_ROUND_SIZE = 1000; + constexpr uint32_t MAX_JSON_SIZE = 40 * 1024 * 1024; + constexpr uint32_t MAX_HASH_SIZE = 64; + constexpr uint32_t MAX_KEY_SIZE = 256; + const std::string LOW_LATENCY_KEY = "identity"; const std::u16string DHMS_STUB_INTERFACE_TOKEN = u"ohos.distributedhardware.accessToken"; const std::string APP_ID = "dtbhardware_manager_service"; diff --git a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp index 782e2dd5c0308a2a8eee2ac8e5c7d79748f54cc9..01affc084f2ef6eb665b9f472765e75d62d6cbca 100644 --- a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp +++ b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp @@ -24,6 +24,7 @@ #include "distributed_hardware_log.h" #include "dh_utils_hisysevent.h" #include "idistributed_hardware.h" +#include "dh_utils_tool.h" namespace OHOS { namespace DistributedHardware { @@ -89,8 +90,7 @@ int32_t DistributedHardwareFwkKit::PublishMessage(const DHTopic topic, const std DHLOGE("Topic invalid, topic: %{public}" PRIu32, (uint32_t)topic); return ERR_DH_FWK_PARA_INVALID; } - if (message.empty() || message.size() > MAX_MESSAGE_LEN) { - DHLOGE("Message size is invalid!"); + if (MessageLengthInvalid(message)) { return ERR_DH_FWK_PARA_INVALID; } @@ -203,6 +203,9 @@ int32_t DistributedHardwareFwkKit::RegisterCtlCenterCallback(int32_t engineId, int32_t DistributedHardwareFwkKit::PauseDistributedHardware(DHType dhType, const std::string &networkId) { + if (IdLengthInvalid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("Pause distributed hardware dhType %{public}u, networkId %{public}s", (uint32_t)dhType, GetAnonyString(networkId).c_str()); @@ -217,6 +220,9 @@ int32_t DistributedHardwareFwkKit::PauseDistributedHardware(DHType dhType, const int32_t DistributedHardwareFwkKit::ResumeDistributedHardware(DHType dhType, const std::string &networkId) { + if (IdLengthInvalid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("Resume distributed hardware dhType %{public}u, networkId %{public}s", (uint32_t)dhType, GetAnonyString(networkId).c_str()); @@ -231,6 +237,9 @@ int32_t DistributedHardwareFwkKit::ResumeDistributedHardware(DHType dhType, cons int32_t DistributedHardwareFwkKit::StopDistributedHardware(DHType dhType, const std::string &networkId) { + if (IdLengthInvalid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("Stop distributed hardware dhType %{public}u, networkId %{public}s", (uint32_t)dhType, GetAnonyString(networkId).c_str()); diff --git a/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp b/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp index ae595b0af0476b21cc7374b759c8f82ad5c9be85..547ed76f7c73d3557779a79e98d0746b87355613 100644 --- a/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp +++ b/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp @@ -24,6 +24,7 @@ #include "distributed_hardware_log.h" #include "distributed_hardware_proxy.h" #include "distributed_hardware_errno.h" +#include "dh_utils_tool.h" namespace OHOS { namespace DistributedHardware { @@ -102,6 +103,9 @@ void DHFWKSAManager::RegisterSAStateCallback(DHFWKSAStateCb callback) void DHFWKSAManager::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) { + if (IdLengthInvalid(deviceId)) { + return; + } (void)deviceId; if (systemAbilityId != DISTRIBUTED_HARDWARE_SA_ID) { DHLOGW("Receive SA Start, but sa id is not DHFWK, id: %{public}" PRId32, systemAbilityId); @@ -123,6 +127,9 @@ void DHFWKSAManager::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbi void DHFWKSAManager::SystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) { + if (IdLengthInvalid(deviceId)) { + return; + } (void)deviceId; if (systemAbilityId != DISTRIBUTED_HARDWARE_SA_ID) { DHLOGW("Receive SA Stop, but sa id is not DHFWK, id: %{public}" PRId32, systemAbilityId); diff --git a/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp index a1abe8eb752b6a7d7c0c3c7ef79644d8af8dde6b..8054a0222794bd9d2ba52c90b00db3badbe6a6e0 100644 --- a/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp +++ b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp @@ -24,6 +24,7 @@ #include "dhardware_ipc_interface_code.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" +#include "dh_utils_tool.h" #include "parcel.h" namespace OHOS { @@ -144,8 +145,7 @@ int32_t DistributedHardwareProxy::PublishMessage(const DHTopic topic, const std: DHLOGE("Topic is invalid!"); return ERR_DH_FWK_PARA_INVALID; } - if (msg.empty() || msg.size() > MAX_MESSAGE_LEN) { - DHLOGE("Msg is invalid"); + if (MessageLengthInvalid(msg)) { return ERR_DH_FWK_SERVICE_MSG_INVALID; } @@ -416,6 +416,9 @@ int32_t DistributedHardwareProxy::NotifySourceRemoteSinkStarted(std::string &dev int32_t DistributedHardwareProxy::PauseDistributedHardware(DHType dhType, const std::string &networkId) { + if (IdLengthInvalid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } sptr remote = Remote(); if (remote == nullptr) { DHLOGE("remote service is null"); @@ -451,6 +454,9 @@ int32_t DistributedHardwareProxy::PauseDistributedHardware(DHType dhType, const int32_t DistributedHardwareProxy::ResumeDistributedHardware(DHType dhType, const std::string &networkId) { + if (IdLengthInvalid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } sptr remote = Remote(); if (remote == nullptr) { DHLOGE("remote service is null"); @@ -486,6 +492,9 @@ int32_t DistributedHardwareProxy::ResumeDistributedHardware(DHType dhType, const int32_t DistributedHardwareProxy::StopDistributedHardware(DHType dhType, const std::string &networkId) { + if (IdLengthInvalid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } sptr remote = Remote(); if (remote == nullptr) { DHLOGE("remote service is null"); diff --git a/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp b/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp index a2f8df6f9d96c7c7ce0f2ff9ee405c8f44660175..35c65ecd706ea5635d58156365fbafe76d1a6681 100644 --- a/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp +++ b/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp @@ -130,14 +130,9 @@ void AccessManager::OnDeviceOffline(const DmDeviceInfo &deviceInfo) deviceInfo.deviceTypeId); auto networkId = std::string(deviceInfo.networkId); - if (networkId.size() == 0 || networkId.size() > MAX_ID_LEN) { - DHLOGE("NetworkId is invalid!"); - return; - } std::string uuid = DHContext::GetInstance().GetUUIDByNetworkId(networkId); std::string udid = DHContext::GetInstance().GetUDIDByNetworkId(networkId); - if (uuid.empty() || uuid.size() > MAX_ID_LEN || udid.empty() || udid.size() > MAX_ID_LEN) { - DHLOGE("uuid or udid is invalid!"); + if (IdLengthInvalid(networkId) || IdLengthInvalid(uuid) || IdLengthInvalid(udid)) { return; } @@ -155,18 +150,9 @@ void AccessManager::OnDeviceReady(const DmDeviceInfo &deviceInfo) deviceInfo.deviceTypeId); auto networkId = std::string(deviceInfo.networkId); - if (networkId.size() == 0 || networkId.size() > MAX_ID_LEN) { - DHLOGE("NetworkId is invalid!"); - return; - } auto uuid = GetUUIDByDm(networkId); - if (uuid.size() == 0 || uuid.size() > MAX_ID_LEN) { - DHLOGE("Uuid is invalid!"); - return; - } auto udid = GetUDIDByDm(networkId); - if (udid.size() == 0 || udid.size() > MAX_ID_LEN) { - DHLOGE("Udid is invalid!"); + if (IdLengthInvalid(networkId) || IdLengthInvalid(uuid) || IdLengthInvalid(udid)) { return; } auto ret = diff --git a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp index f9a6165ef3183a193feaff2da60bd3fbce878fd8..d9fa48ad3501cfb9cf1a58c13408761db0af9bec 100644 --- a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp +++ b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp @@ -116,22 +116,22 @@ std::vector ComponentLoader::GetAllCompTypes() int32_t ParseComponent(const cJSON *json, CompConfig &cfg) { if (!IsString(json, COMP_NAME)) { - DHLOGE("COMP_NAME is invalid"); + DHLOGE("COMP_NAME is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } cfg.name = cJSON_GetObjectItem(json, COMP_NAME.c_str())->valuestring; if (!IsString(json, COMP_TYPE)) { - DHLOGE("COMP_TYPE is invalid"); + DHLOGE("COMP_TYPE is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } cfg.type = g_mapDhTypeName[cJSON_GetObjectItem(json, COMP_TYPE.c_str())->valuestring]; if (!IsString(json, COMP_HANDLER_LOC)) { - DHLOGE("COMP_HANDLER_LOC is invalid"); + DHLOGE("COMP_HANDLER_LOC is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } cfg.compHandlerLoc = cJSON_GetObjectItem(json, COMP_HANDLER_LOC.c_str())->valuestring; if (!IsString(json, COMP_HANDLER_VERSION)) { - DHLOGE("COMP_HANDLER_VERSION is invalid"); + DHLOGE("COMP_HANDLER_VERSION is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } cfg.compHandlerVersion = cJSON_GetObjectItem(json, COMP_HANDLER_VERSION.c_str())->valuestring; @@ -141,17 +141,17 @@ int32_t ParseComponent(const cJSON *json, CompConfig &cfg) int32_t ParseSource(const cJSON *json, CompConfig &cfg) { if (!IsString(json, COMP_SOURCE_LOC)) { - DHLOGE("COMP_SOURCE_LOC is invalid"); + DHLOGE("COMP_SOURCE_LOC is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } cfg.compSourceLoc = cJSON_GetObjectItem(json, COMP_SOURCE_LOC.c_str())->valuestring; if (!IsString(json, COMP_SOURCE_VERSION)) { - DHLOGE("COMP_SOURCE_VERSION is invalid"); + DHLOGE("COMP_SOURCE_VERSION is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } cfg.compSourceVersion = cJSON_GetObjectItem(json, COMP_SOURCE_VERSION.c_str())->valuestring; if (!IsInt32(json, COMP_SOURCE_SA_ID)) { - DHLOGE("COMP_SOURCE_SA_ID is invalid"); + DHLOGE("COMP_SOURCE_SA_ID is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } cfg.compSourceSaId = static_cast(cJSON_GetObjectItem(json, COMP_SOURCE_SA_ID.c_str())->valuedouble); @@ -161,17 +161,17 @@ int32_t ParseSource(const cJSON *json, CompConfig &cfg) int32_t ParseSink(const cJSON *json, CompConfig &cfg) { if (!IsString(json, COMP_SINK_LOC)) { - DHLOGE("COMP_SINK_LOC is invalid"); + DHLOGE("COMP_SINK_LOC is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } cfg.compSinkLoc = cJSON_GetObjectItem(json, COMP_SINK_LOC.c_str())->valuestring; if (!IsString(json, COMP_SINK_VERSION)) { - DHLOGE("COMP_SINK_VERSION is invalid"); + DHLOGE("COMP_SINK_VERSION is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } cfg.compSinkVersion = cJSON_GetObjectItem(json, COMP_SINK_VERSION.c_str())->valuestring; if (!IsInt32(json, COMP_SINK_SA_ID)) { - DHLOGE("COMP_SINK_SA_ID is invalid"); + DHLOGE("COMP_SINK_SA_ID is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } cfg.compSinkSaId = static_cast(cJSON_GetObjectItem(json, COMP_SINK_SA_ID.c_str())->valuedouble); @@ -181,7 +181,7 @@ int32_t ParseSink(const cJSON *json, CompConfig &cfg) int32_t ParseResourceDesc(const cJSON *json, CompConfig &cfg) { if (!IsArray(json, COMP_RESOURCE_DESC)) { - DHLOGE("COMP_RESOURCE_DESC is invalid"); + DHLOGE("COMP_RESOURCE_DESC is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } cJSON *resourceDescArray = cJSON_GetObjectItem(json, COMP_RESOURCE_DESC.c_str()); @@ -189,7 +189,7 @@ int32_t ParseResourceDesc(const cJSON *json, CompConfig &cfg) cJSON_ArrayForEach(element, resourceDescArray) { ResourceDesc desc; if (!IsString(element, COMP_SUBTYPE)) { - DHLOGE("COMP_SUBTYPE is invalid"); + DHLOGE("COMP_SUBTYPE is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } desc.subtype = cJSON_GetObjectItem(element, COMP_SUBTYPE.c_str())->valuestring; @@ -253,6 +253,9 @@ bool ComponentLoader::CheckComponentEnable(const CompConfig &config) int32_t ComponentLoader::GetCompPathAndVersion(const std::string &jsonStr, std::map &dhtypeMap) { + if (JsonLengthInvalid(jsonStr)) { + return ERR_DH_FWK_PARA_INVALID; + } cJSON *root = cJSON_Parse(jsonStr.c_str()); if (root == NULL) { DHLOGE("jsonStr parse failed"); @@ -337,7 +340,7 @@ void ComponentLoader::ParseResourceDescFromJson(cJSON *resourceDescs, CompConfig } ResourceDesc resource; if (!IsString(resourceDesc, COMP_SUBTYPE)) { - DHLOGE("COMP_SUBTYPE is invalid"); + DHLOGE("COMP_SUBTYPE is invalid!"); return; } resource.subtype = cJSON_GetObjectItem(resourceDesc, COMP_SUBTYPE.c_str())->valuestring; @@ -519,8 +522,7 @@ int32_t ComponentLoader::ParseConfig() } std::string componentProfilePath(path); std::string jsonStr = Readfile(componentProfilePath); - if (jsonStr.length() == 0 || jsonStr.size() > MAX_MESSAGE_LEN) { - DHLOGE("ConfigJson size is invalid!"); + if (MessageLengthInvalid(jsonStr)) { return ERR_DH_FWK_LOADER_CONFIG_JSON_INVALID; } ret = GetCompPathAndVersion(jsonStr, dhtypeMap); diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_disable.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_disable.cpp index c70b14cd0dced2a33eb380fce3350d8afdb627fb..cc949dee5558fa24fb2e339b5a1104b0e63105b5 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_disable.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_disable.cpp @@ -18,6 +18,7 @@ #include "anonymous_string.h" #include "constants.h" #include "dh_utils_hisysevent.h" +#include "dh_utils_tool.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" @@ -33,6 +34,9 @@ ComponentDisable::~ComponentDisable() {} int32_t ComponentDisable::Disable(const std::string &networkId, const std::string &dhId, IDistributedHardwareSource *handler) { + if (IdLengthInvalid(networkId) || IdLengthInvalid(dhId)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGD("networkId = %{public}s dhId = %{public}s.", GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str()); if (handler == nullptr) { @@ -68,6 +72,9 @@ int32_t ComponentDisable::Disable(const std::string &networkId, const std::strin int32_t ComponentDisable::OnUnregisterResult(const std::string &networkId, const std::string &dhId, int32_t status, const std::string &data) { + if (IdLengthInvalid(networkId) || IdLengthInvalid(dhId)) { + return ERR_DH_FWK_PARA_INVALID; + } if (status == DH_FWK_SUCCESS) { DHLOGI("disable success, networkId = %{public}s, dhId = %{public}s.", GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str()); diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_enable.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_enable.cpp index d0f757b0338e78b278ab59f2e54cfedfac71eb06..87715b6cc7e835f92d165cd2b1504493006c2839 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_enable.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_enable.cpp @@ -18,6 +18,7 @@ #include "anonymous_string.h" #include "constants.h" #include "dh_utils_hisysevent.h" +#include "dh_utils_tool.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" @@ -33,6 +34,9 @@ ComponentEnable::~ComponentEnable() {} int32_t ComponentEnable::Enable(const std::string &networkId, const std::string &dhId, const EnableParam ¶m, IDistributedHardwareSource *handler) { + if (IdLengthInvalid(networkId) || IdLengthInvalid(dhId)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGD("networkId = %{public}s dhId = %{public}s.", GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str()); if (handler == nullptr) { @@ -68,6 +72,9 @@ int32_t ComponentEnable::Enable(const std::string &networkId, const std::string int32_t ComponentEnable::OnRegisterResult(const std::string &networkId, const std::string &dhId, int32_t status, const std::string &data) { + if (IdLengthInvalid(networkId) || IdLengthInvalid(dhId)) { + return ERR_DH_FWK_PARA_INVALID; + } if (status == DH_FWK_SUCCESS) { DHLOGI("enable success, networkId = %{public}s, dhId = %{public}s.", GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str()); diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp index 525268eb97bb786d0e11a57cfb2ed28a74ca1782..f326c058eb607d2b1c75cf4e741f0c337975cc29 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp @@ -516,9 +516,11 @@ bool ComponentManager::InitCompSink() int32_t ComponentManager::Enable(const std::string &networkId, const std::string &uuid, const std::string &dhId, const DHType dhType) { + if (IdLengthInvalid(networkId) || IdLengthInvalid(uuid) || IdLengthInvalid(dhId)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("start."); - auto find = compSource_.find(dhType); - if (find == compSource_.end()) { + if (compSource_.find(dhType) == compSource_.end()) { DHLOGE("can not find handler for dhId = %{public}s.", GetAnonyString(dhId).c_str()); return ERR_DH_FWK_PARA_INVALID; } @@ -537,22 +539,20 @@ int32_t ComponentManager::Enable(const std::string &networkId, const std::string DHLOGE("GetCompResourceDesc failed, subtype: %{public}s", subtype.c_str()); return ERR_DH_FWK_RESOURCE_KEY_IS_EMPTY; } - bool sensitiveVal = resourceDesc[subtype]; - bool isSameAuthForm = IsIdenticalAccount(networkId); - if (sensitiveVal && !isSameAuthForm) { + if (resourceDesc[subtype] && !IsIdenticalAccount(networkId)) { DHLOGE("Privacy resources must be logged in with the same account."); return ERR_DH_FWK_COMPONENT_ENABLE_FAILED; } auto compEnable = std::make_shared(); - auto result = compEnable->Enable(networkId, dhId, param, find->second); + auto result = compEnable->Enable(networkId, dhId, param, (compSource_.find(dhType))->second); if (result != DH_FWK_SUCCESS) { for (int32_t retryCount = 0; retryCount < ENABLE_RETRY_MAX_TIMES; retryCount++) { if (!DHContext::GetInstance().IsDeviceOnline(uuid)) { DHLOGE("device is already offline, no need try enable, uuid= %{public}s", GetAnonyString(uuid).c_str()); return result; } - if (compEnable->Enable(networkId, dhId, param, find->second) == DH_FWK_SUCCESS) { + if (compEnable->Enable(networkId, dhId, param, (compSource_.find(dhType))->second) == DH_FWK_SUCCESS) { DHLOGE("enable success, retryCount = %{public}d", retryCount); EnabledCompsDump::GetInstance().DumpEnabledComp(networkId, dhType, dhId); return DH_FWK_SUCCESS; @@ -571,6 +571,9 @@ int32_t ComponentManager::Enable(const std::string &networkId, const std::string int32_t ComponentManager::RetryGetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId, const DHType dhType, EnableParam ¶m) { + if (IdLengthInvalid(networkId) || IdLengthInvalid(uuid) || IdLengthInvalid(dhId)) { + return ERR_DH_FWK_PARA_INVALID; + } for (int32_t retryCount = 0; retryCount < ENABLE_RETRY_MAX_TIMES; retryCount++) { if (!DHContext::GetInstance().IsDeviceOnline(uuid)) { DHLOGE("device is already offline, no need try GetEnableParam, uuid = %{public}s", @@ -590,12 +593,15 @@ int32_t ComponentManager::RetryGetEnableParam(const std::string &networkId, cons int32_t ComponentManager::Disable(const std::string &networkId, const std::string &uuid, const std::string &dhId, const DHType dhType) { + if (IdLengthInvalid(networkId) || IdLengthInvalid(uuid) || IdLengthInvalid(dhId)) { + return ERR_DH_FWK_PARA_INVALID; + } auto find = compSource_.find(dhType); if (find == compSource_.end()) { DHLOGE("can not find handler for dhId = %{public}s.", GetAnonyString(dhId).c_str()); return ERR_DH_FWK_PARA_INVALID; } - + auto compDisable = std::make_shared(); auto result = compDisable->Disable(networkId, dhId, find->second); if (result != DH_FWK_SUCCESS) { @@ -636,6 +642,9 @@ DHType ComponentManager::GetDHType(const std::string &uuid, const std::string &d int32_t ComponentManager::GetEnableCapParam(const std::string &networkId, const std::string &uuid, DHType dhType, EnableParam ¶m, std::shared_ptr &capability) { + if (IdLengthInvalid(networkId) || IdLengthInvalid(uuid)) { + return ERR_DH_FWK_PARA_INVALID; + } DeviceInfo sourceDeviceInfo = GetLocalDeviceInfo(); std::vector> sourceCapInfos; std::string sourceDHId; @@ -682,6 +691,9 @@ int32_t ComponentManager::GetEnableCapParam(const std::string &networkId, const int32_t ComponentManager::GetEnableMetaParam(const std::string &networkId, const std::string &uuid, DHType dhType, EnableParam ¶m, std::shared_ptr &metaCapPtr) { + if (IdLengthInvalid(networkId) || IdLengthInvalid(uuid)) { + return ERR_DH_FWK_PARA_INVALID; + } DeviceInfo sourceDeviceInfo = GetLocalDeviceInfo(); std::vector> sourceMetaInfos; std::string sourceDHId; @@ -714,6 +726,9 @@ int32_t ComponentManager::GetEnableMetaParam(const std::string &networkId, const int32_t ComponentManager::GetCapParam(const std::string &uuid, const std::string &dhId, std::shared_ptr &capability) { + if (IdLengthInvalid(uuid) || IdLengthInvalid(dhId)) { + return ERR_DH_FWK_PARA_INVALID; + } std::string deviceId = GetDeviceIdByUUID(uuid); auto ret = CapabilityInfoManager::GetInstance()->GetCapability(deviceId, dhId, capability); if ((ret == DH_FWK_SUCCESS) && (capability != nullptr)) { @@ -735,6 +750,9 @@ int32_t ComponentManager::GetCapParam(const std::string &uuid, const std::string int32_t ComponentManager::GetMetaParam(const std::string &uuid, const std::string &dhId, std::shared_ptr &metaCapPtr) { + if (IdLengthInvalid(uuid) || IdLengthInvalid(dhId)) { + return ERR_DH_FWK_PARA_INVALID; + } auto ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(DHContext::GetInstance().GetUdidHashIdByUUID(uuid), dhId, metaCapPtr); if ((ret == DH_FWK_SUCCESS) && (metaCapPtr != nullptr)) { @@ -748,6 +766,9 @@ int32_t ComponentManager::GetMetaParam(const std::string &uuid, const std::strin int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId, DHType dhType, EnableParam ¶m) { + if (IdLengthInvalid(networkId) || IdLengthInvalid(uuid) || IdLengthInvalid(dhId)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("GetEnableParam start, networkId= %{public}s, uuid = %{public}s, dhId = %{public}s, dhType = %{public}#X,", GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str(), dhType); std::shared_ptr capability = nullptr; @@ -774,6 +795,9 @@ int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std int32_t ComponentManager::GetVersionFromVerMgr(const std::string &uuid, const DHType dhType, std::string &version, bool isSink) { + if (IdLengthInvalid(uuid)) { + return ERR_DH_FWK_PARA_INVALID; + } CompVersion compversion; int32_t ret = VersionManager::GetInstance().GetCompVersion(uuid, dhType, compversion); if (ret != DH_FWK_SUCCESS) { @@ -791,6 +815,9 @@ int32_t ComponentManager::GetVersionFromVerMgr(const std::string &uuid, const DH int32_t ComponentManager::GetVersionFromVerInfoMgr(const std::string &uuid, const DHType dhType, std::string &version, bool isSink) { + if (IdLengthInvalid(uuid)) { + return ERR_DH_FWK_PARA_INVALID; + } VersionInfo versionInfo; int32_t ret = VersionInfoManager::GetInstance()->GetVersionInfoByDeviceId(GetDeviceIdByUUID(uuid), versionInfo); if (ret != DH_FWK_SUCCESS) { @@ -813,6 +840,9 @@ int32_t ComponentManager::GetVersionFromVerInfoMgr(const std::string &uuid, cons int32_t ComponentManager::GetVersion(const std::string &uuid, DHType dhType, std::string &version, bool isSink) { + if (IdLengthInvalid(uuid)) { + return ERR_DH_FWK_PARA_INVALID; + } int32_t ret = GetVersionFromVerMgr(uuid, dhType, version, isSink); if ((ret == DH_FWK_SUCCESS) && (!version.empty())) { return DH_FWK_SUCCESS; @@ -828,6 +858,9 @@ int32_t ComponentManager::GetVersion(const std::string &uuid, DHType dhType, std void ComponentManager::UpdateVersionCache(const std::string &uuid, const VersionInfo &versionInfo) { + if (IdLengthInvalid(uuid)) { + return; + } DHVersion dhVersion; dhVersion.uuid = uuid; dhVersion.dhVersion = versionInfo.dhVersion; @@ -914,6 +947,9 @@ std::map ComponentManager::GetDHSinkInstance( bool ComponentManager::IsIdenticalAccount(const std::string &networkId) { + if (IdLengthInvalid(networkId)) { + return false; + } DmAuthForm authForm = DmAuthForm::INVALID_TYPE; std::vector deviceList; DeviceManager::GetInstance().GetTrustedDeviceList(DH_FWK_PKG_NAME, "", deviceList); @@ -935,6 +971,9 @@ bool ComponentManager::IsIdenticalAccount(const std::string &networkId) void ComponentManager::UpdateBusinessState(const std::string &networkId, const std::string &dhId, BusinessState state) { + if (IdLengthInvalid(networkId) || IdLengthInvalid(dhId)) { + return; + } DHLOGI("UpdateBusinessState, networkId: %{public}s, dhId: %{public}s, state: %{public}" PRIu32, GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str(), (uint32_t)state); { @@ -956,6 +995,9 @@ void ComponentManager::UpdateBusinessState(const std::string &networkId, const s BusinessState ComponentManager::QueryBusinessState(const std::string &uuid, const std::string &dhId) { + if (IdLengthInvalid(uuid) || IdLengthInvalid(dhId)) { + return BusinessState::UNKNOWN; + } std::lock_guard lock(bizStateMtx_); std::pair key = {uuid, dhId}; if (dhBizStates_.find(key) == dhBizStates_.end()) { @@ -967,8 +1009,7 @@ BusinessState ComponentManager::QueryBusinessState(const std::string &uuid, cons void ComponentManager::TriggerFullCapsSync(const std::string &networkId) { - if (networkId.empty()) { - DHLOGE("Remote networkid is null"); + if (IdLengthInvalid(networkId)) { return; } dhCommToolPtr_->TriggerReqFullDHCaps(networkId); @@ -1007,7 +1048,7 @@ void ComponentManager::ComponentManagerEventHandler::ProcessEvent( // do muanul sync with remote auto sharedObjPtr = event->GetSharedObject(); if (sharedObjPtr == nullptr) { - DHLOGE("The data sync param invalid"); + DHLOGE("The data sync param invalid!"); break; } std::string networkId = *sharedObjPtr; diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_monitor.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_monitor.cpp index fdcd7026f56b2dd620509c96f69e59e277e4d962..27df7a5eb6f3b8bb4f01dc4ca1c4960212e72f0a 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_monitor.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_monitor.cpp @@ -25,8 +25,10 @@ #include "component_loader.h" #include "component_manager.h" #include "device_type.h" +#include "constants.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" +#include "dh_utils_tool.h" namespace OHOS { namespace DistributedHardware { @@ -47,11 +49,17 @@ ComponentMonitor::~ComponentMonitor() void ComponentMonitor::CompSystemAbilityListener::OnAddSystemAbility(int32_t saId, const std::string &deviceId) { + if (IdLengthInvalid(deviceId)) { + return; + } DHLOGI("OnAddSystemAbility, saId: %{public}d, deviceId: %{public}s", saId, GetAnonyString(deviceId).c_str()); } void ComponentMonitor::CompSystemAbilityListener::OnRemoveSystemAbility(int32_t saId, const std::string &deviceId) { + if (IdLengthInvalid(deviceId)) { + return; + } DHLOGI("OnRemoveSystemAbility, saId: %{public}d, deviceId: %{public}s", saId, GetAnonyString(deviceId).c_str()); DHType dhType = ComponentLoader::GetInstance().GetDHTypeBySrcSaId(saId); if (dhType == DHType::UNKNOWN) { diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_privacy.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_privacy.cpp index 6e836376f618fd83bc1d4f3f6802cabcabe7d8a0..391ff4a36fe65bc16c09dd412101c6dda85ce233 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_privacy.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_privacy.cpp @@ -47,6 +47,9 @@ ComponentPrivacy::~ComponentPrivacy() int32_t ComponentPrivacy::OnPrivaceResourceMessage(const ResourceEventType &type, const std::string &subtype, const std::string &networkId, bool &isSensitive, bool &isSameAccout) { + if (IdLengthInvalid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("OnPrivaceResourceMessage start."); int32_t ret = DH_FWK_SUCCESS; if (type == ResourceEventType::EVENT_TYPE_QUERY_RESOURCE) { @@ -61,6 +64,9 @@ int32_t ComponentPrivacy::OnPrivaceResourceMessage(const ResourceEventType &type void ComponentPrivacy::HandlePullUpPage(const std::string &subtype, const std::string &networkId) { + if (IdLengthInvalid(networkId)) { + return; + } cJSON *jsonArrayMsg = cJSON_CreateArray(); if (jsonArrayMsg == NULL) { DHLOGE("Failed to create cJSON arrary."); @@ -119,6 +125,9 @@ void ComponentPrivacy::HandleClosePage(const std::string &subtype) int32_t ComponentPrivacy::OnResourceInfoCallback(const std::string &subtype, const std::string &networkId, bool &isSensitive, bool &isSameAccout) { + if (IdLengthInvalid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("OnResourceInfoCallback start."); std::map resourceDesc = ComponentLoader::GetInstance().GetCompResourceDesc(); if (resourceDesc.find(subtype) == resourceDesc.end()) { @@ -149,6 +158,9 @@ int32_t ComponentPrivacy::OnResourceInfoCallback(const std::string &subtype, con int32_t ComponentPrivacy::StartPrivacePage(const std::string &subtype, const std::string &networkId) { + if (IdLengthInvalid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("StartPrivacePage start."); DmDeviceInfo deviceInfo; DeviceManager::GetInstance().GetDeviceInfo(DH_FWK_PKG_NAME, networkId, deviceInfo); @@ -285,12 +297,12 @@ void ComponentPrivacy::ComponentEventHandler::ProcessStartPage(const AppExecFwk: std::shared_ptr dataMsg = event->GetSharedObject(); cJSON *innerMsg = cJSON_GetArrayItem(dataMsg.get(), 0); if (!IsString(innerMsg, PRIVACY_SUBTYPE)) { - DHLOGE("PRIVACY_SUBTYPE is invalid"); + DHLOGE("PRIVACY_SUBTYPE is invalid!"); return; } std::string subtype = cJSON_GetObjectItem(innerMsg, PRIVACY_SUBTYPE.c_str())->valuestring; if (!IsString(innerMsg, PRIVACY_NETWORKID)) { - DHLOGE("PRIVACY_NETWORKID is invalid"); + DHLOGE("PRIVACY_NETWORKID is invalid!"); return; } std::string networkId = cJSON_GetObjectItem(innerMsg, PRIVACY_NETWORKID.c_str())->valuestring; @@ -303,7 +315,7 @@ void ComponentPrivacy::ComponentEventHandler::ProcessStopPage(const AppExecFwk:: std::shared_ptr dataMsg = event->GetSharedObject(); cJSON *innerMsg = cJSON_GetArrayItem(dataMsg.get(), 0); if (!IsString(innerMsg, PRIVACY_SUBTYPE)) { - DHLOGE("PRIVACY_SUBTYPE is invalid"); + DHLOGE("PRIVACY_SUBTYPE is invalid!"); return; } std::string subtype = cJSON_GetObjectItem(innerMsg, PRIVACY_SUBTYPE.c_str())->valuestring; diff --git a/services/distributedhardwarefwkservice/src/componentmanager/dh_data_sync_trigger_listener.cpp b/services/distributedhardwarefwkservice/src/componentmanager/dh_data_sync_trigger_listener.cpp index d74cec39a687aa733d6f112d836e3b321d26064c..2f970be39b22f7b6ce6d5732539dad9175a7b2d1 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/dh_data_sync_trigger_listener.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/dh_data_sync_trigger_listener.cpp @@ -21,6 +21,7 @@ #include "component_manager.h" #include "distributed_hardware_log.h" #include "event_handler.h" +#include "dh_utils_tool.h" class ComponentManager; namespace OHOS { @@ -39,6 +40,9 @@ DHDataSyncTriggerListener::~DHDataSyncTriggerListener() void DHDataSyncTriggerListener::OnDataSyncTrigger(const std::string &networkId) { + if (IdLengthInvalid(networkId)) { + return; + } DHLOGI("Receive data sync trigger, networkId: %{public}s", GetAnonyString(networkId).c_str()); if (networkId.empty()) { DHLOGE("OnDataSyncTrigger networkId is empty"); diff --git a/services/distributedhardwarefwkservice/src/componentmanager/dh_state_listener.cpp b/services/distributedhardwarefwkservice/src/componentmanager/dh_state_listener.cpp index 9ce4d817f365d4f553017ce25af6caa33bd9c60d..e08dbea9443b7440b5efc4e96b9a65a9d95b6efe 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/dh_state_listener.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/dh_state_listener.cpp @@ -20,6 +20,7 @@ #include "anonymous_string.h" #include "component_manager.h" #include "distributed_hardware_log.h" +#include "dh_utils_tool.h" class ComponentManager; namespace OHOS { @@ -38,6 +39,9 @@ DHStateListener::~DHStateListener() void DHStateListener::OnStateChanged(const std::string &networkId, const std::string &dhId, const BusinessState state) { + if (IdLengthInvalid(networkId)) { + return; + } DHLOGI("Receive business state change, networkId: %{public}s, dhId: %{public}s, state: %{public}" PRIu32, GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str(), (uint32_t)state); ComponentManager::GetInstance().UpdateBusinessState(networkId, dhId, state); diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_manager.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_manager.cpp index 2df70d6898a65bbe2fd6039d1390208ab2dee789..04cc67d1645b3634dd4ea4688b60979ea596de82 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_manager.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_manager.cpp @@ -97,6 +97,9 @@ int32_t DistributedHardwareManager::Release() int32_t DistributedHardwareManager::SendOnLineEvent(const std::string &networkId, const std::string &uuid, const std::string &udid, uint16_t deviceType) { + if (IdLengthInvalid(networkId) || IdLengthInvalid(uuid) || IdLengthInvalid(udid)) { + return ERR_DH_FWK_PARA_INVALID; + } (void)deviceType; DHLOGI("SendOnLineEvent networkId = %{public}s, uuid = %{public}s, udid = %{public}s", GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str(), GetAnonyString(udid).c_str()); @@ -116,6 +119,9 @@ int32_t DistributedHardwareManager::SendOnLineEvent(const std::string &networkId int32_t DistributedHardwareManager::SendOffLineEvent(const std::string &networkId, const std::string &uuid, const std::string &udid, uint16_t deviceType) { + if (IdLengthInvalid(networkId) || IdLengthInvalid(uuid) || IdLengthInvalid(udid)) { + return ERR_DH_FWK_PARA_INVALID; + } (void)deviceType; DHLOGI("SendOffLineEvent networkId = %{public}s, uuid = %{public}s, udid = %{public}s", GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str(), GetAnonyString(udid).c_str()); diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp index 4d826ce737a1fb34aea9a9bc3d361175e62a7570..c1a95caae8882e330a86adb40b62411d6752f786 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp @@ -148,6 +148,9 @@ bool DistributedHardwareManagerFactory::IsInit() int32_t DistributedHardwareManagerFactory::SendOnLineEvent(const std::string &networkId, const std::string &uuid, const std::string &udid, uint16_t deviceType) { + if (IdLengthInvalid(networkId) || IdLengthInvalid(uuid) || IdLengthInvalid(udid)) { + return ERR_DH_FWK_PARA_INVALID; + } int32_t ret = pthread_setname_np(pthread_self(), SEND_ONLINE); if (ret != DH_FWK_SUCCESS) { DHLOGE("SendOnLineEvent setname failed."); @@ -181,6 +184,9 @@ int32_t DistributedHardwareManagerFactory::SendOnLineEvent(const std::string &ne int32_t DistributedHardwareManagerFactory::SendOffLineEvent(const std::string &networkId, const std::string &uuid, const std::string &udid, uint16_t deviceType) { + if (IdLengthInvalid(networkId) || IdLengthInvalid(uuid) || IdLengthInvalid(udid)) { + return ERR_DH_FWK_PARA_INVALID; + } if (!isInit && !Init()) { DHLOGE("distributedHardwareMgr is null"); return ERR_DH_FWK_HARDWARE_MANAGER_INIT_FAILED; @@ -202,6 +208,13 @@ int32_t DistributedHardwareManagerFactory::SendOffLineEvent(const std::string &n if (offlineResult != DH_FWK_SUCCESS) { DHLOGE("offline failed, errCode = %{public}d", offlineResult); } + + DHContext::GetInstance().RemoveOnlineDeviceByUUID(uuid); + if (DistributedHardwareManager::GetInstance().GetOnLineCount() == 0 && + DHContext::GetInstance().GetIsomerismConnectCount() == 0) { + DHLOGI("all devices are offline, start to free the resource"); + UnInit(); + } return DH_FWK_SUCCESS; } diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp index 3f2bf503b2dc8057132b67a82b5964d5ec1c5747..e70b7b6a82557bd45cf42e9dcc1ad36ebea893c8 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp @@ -306,6 +306,9 @@ int DistributedHardwareService::Dump(int32_t fd, const std::vector sinkMap = ComponentManager::GetInstance().GetDHSinkInstance(); if (sinkMap.find(dhType) == sinkMap.end()) { DHLOGE("PauseDistributedHardware for DHType: %{public}u not init sink handler", (uint32_t)dhType); @@ -321,6 +324,9 @@ int32_t DistributedHardwareService::PauseDistributedHardware(DHType dhType, cons int32_t DistributedHardwareService::ResumeDistributedHardware(DHType dhType, const std::string &networkId) { + if (IdLengthInvalid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } std::map sinkMap = ComponentManager::GetInstance().GetDHSinkInstance(); if (sinkMap.find(dhType) == sinkMap.end()) { DHLOGE("ResumeDistributedHardware for DHType: %{public}u not init sink handler", (uint32_t)dhType); @@ -336,6 +342,9 @@ int32_t DistributedHardwareService::ResumeDistributedHardware(DHType dhType, con int32_t DistributedHardwareService::StopDistributedHardware(DHType dhType, const std::string &networkId) { + if (IdLengthInvalid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } std::map sinkMap = ComponentManager::GetInstance().GetDHSinkInstance(); if (sinkMap.find(dhType) == sinkMap.end()) { DHLOGE("StopDistributedHardware for DHType: %{public}u not init sink handler", (uint32_t)dhType); diff --git a/services/distributedhardwarefwkservice/src/hidumphelper/enabled_comps_dump.cpp b/services/distributedhardwarefwkservice/src/hidumphelper/enabled_comps_dump.cpp index abfd99cb8f04efdeb1c6f224ebe12ce113438bcc..cecdcb6f0913724aaff89dba759220ae46c68885 100644 --- a/services/distributedhardwarefwkservice/src/hidumphelper/enabled_comps_dump.cpp +++ b/services/distributedhardwarefwkservice/src/hidumphelper/enabled_comps_dump.cpp @@ -14,6 +14,7 @@ */ #include "enabled_comps_dump.h" +#include "dh_utils_tool.h" namespace OHOS { namespace DistributedHardware { @@ -21,6 +22,9 @@ IMPLEMENT_SINGLE_INSTANCE(EnabledCompsDump); void EnabledCompsDump::DumpEnabledComp(const std::string &networkId, const DHType dhType, const std::string &dhId) { + if (IdLengthInvalid(networkId) || IdLengthInvalid(dhId)) { + return; + } HidumpCompInfo info(networkId, dhType, dhId); std::lock_guard lock(compInfosMutex_); @@ -29,6 +33,9 @@ void EnabledCompsDump::DumpEnabledComp(const std::string &networkId, const DHTyp void EnabledCompsDump::DumpDisabledComp(const std::string &networkId, const DHType dhType, const std::string &dhId) { + if (IdLengthInvalid(networkId) || IdLengthInvalid(dhId)) { + return; + } HidumpCompInfo info(networkId, dhType, dhId); std::lock_guard lock(compInfosMutex_); diff --git a/services/distributedhardwarefwkservice/src/ipc/publisher_listener_proxy.cpp b/services/distributedhardwarefwkservice/src/ipc/publisher_listener_proxy.cpp index 11fdf70a7863b72dcfd2bfc571c5a9c3487e4c9b..ca44ea78a2da6136671cdf05f9cba273879493c4 100644 --- a/services/distributedhardwarefwkservice/src/ipc/publisher_listener_proxy.cpp +++ b/services/distributedhardwarefwkservice/src/ipc/publisher_listener_proxy.cpp @@ -16,6 +16,7 @@ #include "constants.h" #include "distributed_hardware_log.h" #include "publisher_listener_proxy.h" +#include "dh_utils_tool.h" namespace OHOS { namespace DistributedHardware { @@ -39,8 +40,7 @@ void PublisherListenerProxy::OnMessage(const DHTopic topic, const std::string& m DHLOGE("Topic is invalid!"); return; } - if (message.size() == 0 || message.size() > MAX_MESSAGE_LEN) { - DHLOGE("Message is invalid"); + if (MessageLengthInvalid(message)) { return; } diff --git a/services/distributedhardwarefwkservice/src/localhardwaremanager/local_hardware_manager.cpp b/services/distributedhardwarefwkservice/src/localhardwaremanager/local_hardware_manager.cpp index 1146c1d978dc87e3ff07210fe15de2b1c9c35b46..2652175fa39a9be615cd70771ed4a95862b35a5a 100644 --- a/services/distributedhardwarefwkservice/src/localhardwaremanager/local_hardware_manager.cpp +++ b/services/distributedhardwarefwkservice/src/localhardwaremanager/local_hardware_manager.cpp @@ -196,8 +196,7 @@ void LocalHardwareManager::CheckNonExistCapabilityInfo(const std::vector void LocalHardwareManager::GetLocalCapabilityMapByPrefix(const DHType dhType, CapabilityInfoMap &capabilityInfoMap) { std::string localDeviceId = DHContext::GetInstance().GetDeviceInfo().deviceId; - if (localDeviceId.size() == 0 || localDeviceId.size() > MAX_ID_LEN) { - DHLOGE("LocalDeviceId is invalid"); + if (IdLengthInvalid(localDeviceId)) { return; } if (DHTypePrefixMap.find(dhType) == DHTypePrefixMap.end()) { diff --git a/services/distributedhardwarefwkservice/src/localhardwaremanager/plugin_listener_impl.cpp b/services/distributedhardwarefwkservice/src/localhardwaremanager/plugin_listener_impl.cpp index 103b836c50b73f4970f74e9cba1827624a371c03..b5e030d38ff315db760321d9df283199ab10497a 100644 --- a/services/distributedhardwarefwkservice/src/localhardwaremanager/plugin_listener_impl.cpp +++ b/services/distributedhardwarefwkservice/src/localhardwaremanager/plugin_listener_impl.cpp @@ -22,6 +22,7 @@ #include "dh_context.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" +#include "dh_utils_tool.h" #include "publisher.h" namespace OHOS { @@ -31,8 +32,7 @@ namespace DistributedHardware { void PluginListenerImpl::PluginHardware(const std::string &dhId, const std::string &attrs, const std::string &subtype) { - if (dhId.size() == 0 || dhId.size() > MAX_ID_LEN || attrs.size() == 0 || attrs.size() > MAX_MESSAGE_LEN) { - DHLOGE("Param is invalid!"); + if (IdLengthInvalid(dhId) || MessageLengthInvalid(attrs)) { return; } DHLOGI("plugin start, dhId: %{public}s", GetAnonyString(dhId).c_str()); @@ -55,8 +55,7 @@ void PluginListenerImpl::PluginHardware(const std::string &dhId, const std::stri void PluginListenerImpl::UnPluginHardware(const std::string &dhId) { - if (dhId.size() == 0 || dhId.size() > MAX_ID_LEN) { - DHLOGE("DhId is invalid!"); + if (IdLengthInvalid(dhId)) { return; } DHLOGI("unplugin start, dhId: %{public}s", GetAnonyString(dhId).c_str()); diff --git a/services/distributedhardwarefwkservice/src/lowlatency/low_latency_listener.cpp b/services/distributedhardwarefwkservice/src/lowlatency/low_latency_listener.cpp index 3cd6c701e4523c9a49ef5095ab782cba222a6a4c..70216b6dc8163e500b61207777574d95e27dd3c2 100644 --- a/services/distributedhardwarefwkservice/src/lowlatency/low_latency_listener.cpp +++ b/services/distributedhardwarefwkservice/src/lowlatency/low_latency_listener.cpp @@ -45,8 +45,7 @@ void LowLatencyListener::OnMessage(const DHTopic topic, const std::string& messa DHLOGE("Topic is invalid, topic: %{public}" PRIu32, (uint32_t)topic); return; } - if (message.size() == 0 || message.size() > MAX_MESSAGE_LEN) { - DHLOGE("Message is invalid"); + if (MessageLengthInvalid(message)) { return; } cJSON *jsonObj = cJSON_Parse(message.c_str()); @@ -55,12 +54,12 @@ void LowLatencyListener::OnMessage(const DHTopic topic, const std::string& messa return; } if (!IsUInt32(jsonObj, DH_TYPE)) { - DHLOGE("The DH_TYPE key is invalid"); + DHLOGE("The DH_TYPE key is invalid!"); cJSON_Delete(jsonObj); return; } if (!IsBool(jsonObj, LOW_LATENCY_ENABLE)) { - DHLOGE("The LOW_LATENCY_ENABLE key is invalid"); + DHLOGE("The LOW_LATENCY_ENABLE key is invalid!"); cJSON_Delete(jsonObj); return; } diff --git a/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp b/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp index fd757809ff46b6221dc521ac409b53f4e78cda3c..77abd42589a6479771c722639643221368e7b8c6 100644 --- a/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp +++ b/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp @@ -16,6 +16,7 @@ #include "constants.h" #include "publisher_item.h" #include "distributed_hardware_log.h" +#include "dh_utils_tool.h" namespace OHOS { namespace DistributedHardware { @@ -64,8 +65,7 @@ void PublisherItem::RemoveListener(const sptr &listener) void PublisherItem::PublishMessage(const std::string &message) { - if (message.size() == 0 || message.size() > MAX_MESSAGE_LEN) { - DHLOGE("Message is invalid"); + if (MessageLengthInvalid(message)) { return; } std::lock_guard lock(mutex_); diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp index 7f65f5f84aa1bfcbead09a33e1d80aad7ded10ad..7401a6644c081779bbcd05a818150b0e4a8e2686 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp @@ -120,6 +120,9 @@ std::string CapabilityInfo::GetAnonymousKey() const int32_t CapabilityInfo::FromJsonString(const std::string &jsonStr) { + if (JsonLengthInvalid(jsonStr)) { + return ERR_DH_FWK_PARA_INVALID; + } cJSON *jsonObj = cJSON_Parse(jsonStr.c_str()); if (jsonObj == NULL) { DHLOGE("jsonStr parse failed"); @@ -185,6 +188,10 @@ bool CapabilityInfo::Compare(const CapabilityInfo& capInfo) void ToJson(cJSON *jsonObject, const CapabilityInfo &capability) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } cJSON_AddStringToObject(jsonObject, DH_ID.c_str(), capability.GetDHId().c_str()); cJSON_AddStringToObject(jsonObject, DEV_ID.c_str(), capability.GetDeviceId().c_str()); cJSON_AddStringToObject(jsonObject, DEV_NAME.c_str(), capability.GetDeviceName().c_str()); @@ -196,6 +203,10 @@ void ToJson(cJSON *jsonObject, const CapabilityInfo &capability) void FromJson(const cJSON *jsonObject, CapabilityInfo &capability) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } if (!IsString(jsonObject, DH_ID)) { DHLOGE("DH_ID is invalid!"); return; diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp index aef278d926382edea7ccf9ef36ba604925959a37..5566bf3abd7e461f74708b01878c3ceb3e65adf8 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp @@ -120,6 +120,9 @@ int32_t CapabilityInfoManager::UnInit() int32_t CapabilityInfoManager::SyncDeviceInfoFromDB(const std::string &deviceId) { + if (IdLengthInvalid(deviceId)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("Sync DeviceInfo from DB, deviceId: %{public}s", GetAnonyString(deviceId).c_str()); std::lock_guard lock(capInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { @@ -131,8 +134,8 @@ int32_t CapabilityInfoManager::SyncDeviceInfoFromDB(const std::string &deviceId) DHLOGE("Query data from DB by deviceId failed, id: %{public}s", GetAnonyString(deviceId).c_str()); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } - if (dataVector.size() == 0 || dataVector.size() > MAX_DB_RECORD_SIZE) { - DHLOGE("DataVector size is invalid!"); + if (dataVector.empty() || dataVector.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("On dataVector error, maybe empty or too large."); return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; } for (const auto &data : dataVector) { @@ -159,8 +162,8 @@ int32_t CapabilityInfoManager::SyncRemoteCapabilityInfos() DHLOGE("Query all data from DB failed"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } - if (dataVector.size() == 0 || dataVector.size() > MAX_DB_RECORD_SIZE) { - DHLOGE("DataVector size is invalid!"); + if (dataVector.empty() || dataVector.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("On dataVector error, maybe empty or too large."); return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; } for (const auto &data : dataVector) { @@ -186,8 +189,8 @@ int32_t CapabilityInfoManager::SyncRemoteCapabilityInfos() int32_t CapabilityInfoManager::AddCapability(const std::vector> &resInfos) { - if (resInfos.size() == 0 || resInfos.size() > MAX_DB_RECORD_SIZE) { - DHLOGE("ResInfos size is invalid!"); + if (resInfos.empty() || resInfos.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("ResInfo is empty or too large!"); return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; } std::lock_guard lock(capInfoMgrMutex_); @@ -227,6 +230,10 @@ int32_t CapabilityInfoManager::AddCapability(const std::vector> &resInfos) { + if (resInfos.empty() || resInfos.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("ResInfo is empty or too large!"); + return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; + } std::lock_guard lock(capInfoMgrMutex_); for (auto &resInfo : resInfos) { if (resInfo == nullptr) { @@ -241,8 +248,7 @@ int32_t CapabilityInfoManager::AddCapabilityInMem(const std::vector MAX_ID_LEN) { - DHLOGE("DeviceId is invalid!"); + if (IdLengthInvalid(deviceId)) { return ERR_DH_FWK_PARA_INVALID; } DHLOGI("Remove capability device info, deviceId: %{public}s", GetAnonyString(deviceId).c_str()); @@ -270,6 +276,9 @@ int32_t CapabilityInfoManager::RemoveCapabilityInfoInDB(const std::string &devic int32_t CapabilityInfoManager::RemoveCapabilityInfoByKey(const std::string &key) { + if (IdLengthInvalid(key)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("Remove capability device info, key: %{public}s", GetAnonyString(key).c_str()); std::lock_guard lock(capInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { @@ -289,6 +298,9 @@ int32_t CapabilityInfoManager::RemoveCapabilityInfoByKey(const std::string &key) int32_t CapabilityInfoManager::RemoveCapabilityInfoInMem(const std::string &deviceId) { + if (IdLengthInvalid(deviceId)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("remove capability device info in memory, deviceId: %{public}s", GetAnonyString(deviceId).c_str()); std::lock_guard lock(capInfoMgrMutex_); for (auto iter = globalCapInfoMap_.begin(); iter != globalCapInfoMap_.end();) { @@ -370,6 +382,10 @@ void CapabilityInfoManager::OnChange(const DistributedKv::DataOrigin &origin, Ke void CapabilityInfoManager::HandleCapabilityAddChange(const std::vector &insertRecords) { + if (insertRecords.empty() || insertRecords.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("Records is empty or too large!"); + return; + } std::lock_guard lock(capInfoMgrMutex_); for (const auto &item : insertRecords) { const std::string value = item.value.ToString(); @@ -404,6 +420,10 @@ void CapabilityInfoManager::HandleCapabilityAddChange(const std::vector &updateRecords) { + if (updateRecords.empty() || updateRecords.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("Records is empty or too large!"); + return; + } if (DistributedHardwareManagerFactory::GetInstance().GetUnInitFlag()) { DHLOGE("no need Update, is in uniniting."); return; @@ -424,6 +444,10 @@ void CapabilityInfoManager::HandleCapabilityUpdateChange(const std::vector &deleteRecords) { + if (deleteRecords.empty() || deleteRecords.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("Records is empty or too large!"); + return; + } if (DistributedHardwareManagerFactory::GetInstance().GetUnInitFlag()) { DHLOGE("no need Update, is in uniniting."); return; @@ -467,6 +491,9 @@ bool CapabilityInfoManager::IsCapabilityMatchFilter(const std::shared_ptr> &resInfos) { + if (resInfos.empty() || resInfos.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("resInfo is empty or too large!"); + return; + } std::lock_guard lock(capInfoMgrMutex_); for (auto &capabilityInfo : globalCapInfoMap_) { if (IsCapKeyMatchDeviceId(capabilityInfo.first, deviceId)) { @@ -517,6 +548,9 @@ void CapabilityInfoManager::GetCapabilitiesByDeviceId(const std::string &deviceI bool CapabilityInfoManager::HasCapability(const std::string &deviceId, const std::string &dhId) { + if (IdLengthInvalid(deviceId) || IdLengthInvalid(dhId)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(capInfoMgrMutex_); std::string kvKey = GetCapabilityKey(deviceId, dhId); if (globalCapInfoMap_.find(kvKey) == globalCapInfoMap_.end()) { @@ -528,6 +562,9 @@ bool CapabilityInfoManager::HasCapability(const std::string &deviceId, const std int32_t CapabilityInfoManager::GetCapability(const std::string &deviceId, const std::string &dhId, std::shared_ptr &capPtr) { + if (IdLengthInvalid(deviceId) || IdLengthInvalid(dhId)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(capInfoMgrMutex_); std::string key = GetCapabilityKey(deviceId, dhId); if (globalCapInfoMap_.find(key) == globalCapInfoMap_.end()) { @@ -540,6 +577,9 @@ int32_t CapabilityInfoManager::GetCapability(const std::string &deviceId, const int32_t CapabilityInfoManager::GetDataByKey(const std::string &key, std::shared_ptr &capInfoPtr) { + if (IdLengthInvalid(key)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(capInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGI("dbAdapterPtr_ is null"); @@ -567,6 +607,9 @@ int32_t CapabilityInfoManager::GetDataByDHType(const DHType dhType, CapabilityIn int32_t CapabilityInfoManager::GetDataByKeyPrefix(const std::string &keyPrefix, CapabilityInfoMap &capabilityMap) { + if (IdLengthInvalid(keyPrefix)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(capInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr is null"); @@ -577,8 +620,8 @@ int32_t CapabilityInfoManager::GetDataByKeyPrefix(const std::string &keyPrefix, DHLOGE("Query capability info from db failed, key: %{public}s", GetAnonyString(keyPrefix).c_str()); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } - if (dataVector.size() == 0 || dataVector.size() > MAX_DB_RECORD_SIZE) { - DHLOGE("DataVector size is invalid!"); + if (dataVector.empty() || dataVector.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("On dataVector error, maybe empty or too large."); return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; } for (const auto &data : dataVector) { @@ -602,6 +645,9 @@ void CapabilityInfoManager::DumpCapabilityInfos(std::vector &cap std::vector CapabilityInfoManager::GetEntriesByKeys(const std::vector &keys) { + if (ArrayLengthInvalid(keys)) { + return {}; + } DHLOGI("call"); if (keys.empty()) { DHLOGE("keys empty."); diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/capability_utils.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/capability_utils.cpp index 52d1c8113dc5b61e65eb4fb7debe77067084e224..922400d3e60cecf8b41fb52803586b99861ed83f 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/capability_utils.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/capability_utils.cpp @@ -18,6 +18,7 @@ #include "capability_info.h" #include "constants.h" #include "distributed_hardware_errno.h" +#include "dh_utils_tool.h" namespace OHOS { namespace DistributedHardware { @@ -31,6 +32,9 @@ std::string GetCapabilityKey(const std::string &deviceId, const std::string &dhI bool IsCapKeyMatchDeviceId(const std::string &key, const std::string &deviceId) { + if (IdLengthInvalid(key) || IdLengthInvalid(deviceId)) { + return false; + } std::size_t separatorPos = key.find(RESOURCE_SEPARATOR); if (separatorPos == std::string::npos) { return false; diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/db_adapter.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/db_adapter.cpp index cadc081bbec9c51770b5c5bbe225eecb057c96c3..5420037dc4dcc586e0004c6190217ea05d658e76 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/db_adapter.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/db_adapter.cpp @@ -23,6 +23,7 @@ #include "capability_utils.h" #include "constants.h" #include "dh_context.h" +#include "dh_utils_tool.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" #include "event_handler.h" @@ -190,6 +191,9 @@ int32_t DBAdapter::ReInit(bool isAutoSync) std::string DBAdapter::GetNetworkIdByKey(const std::string &key) { + if (IdLengthInvalid(key)) { + return ""; + } DHLOGI("Get networkId by key: %{public}s", GetAnonyString(key).c_str()); std::string deviceId = DHContext::GetInstance().GetDeviceIdByDBGetPrefix(key); if (deviceId.empty()) { @@ -217,6 +221,9 @@ std::string DBAdapter::GetNetworkIdByKey(const std::string &key) void DBAdapter::SyncByNotFound(const std::string &key) { + if (IdLengthInvalid(key)) { + return; + } std::string networkId = GetNetworkIdByKey(key); if (networkId.empty()) { DHLOGW("The networkId emtpy."); @@ -232,6 +239,9 @@ void DBAdapter::SyncByNotFound(const std::string &key) int32_t DBAdapter::GetDataByKey(const std::string &key, std::string &data) { + if (IdLengthInvalid(key) || MessageLengthInvalid(data)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("Get data by key: %{public}s, storeId: %{public}s, dataType: %{public}d", GetAnonyString(key).c_str(), storeId_.storeId.c_str(), static_cast(this->dataType)); std::lock_guard lock(dbAdapterMutex_); @@ -262,6 +272,9 @@ int32_t DBAdapter::GetDataByKey(const std::string &key, std::string &data) int32_t DBAdapter::GetDataByKeyPrefix(const std::string &keyPrefix, std::vector &values) { + if (IdLengthInvalid(keyPrefix)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("Get data by key prefix: %{public}s, storeId: %{public}s, dataType: %{public}d", GetAnonyString(keyPrefix).c_str(), storeId_.storeId.c_str(), static_cast(this->dataType)); std::lock_guard lock(dbAdapterMutex_); @@ -288,9 +301,9 @@ int32_t DBAdapter::GetDataByKeyPrefix(const std::string &keyPrefix, std::vector< GetAnonyString(keyPrefix).c_str()); return ERR_DH_FWK_RESOURCE_KV_STORAGE_OPERATION_FAIL; } - if (allEntries.size() == 0 || allEntries.size() > MAX_DB_RECORD_SIZE) { - DHLOGE("AllEntries size is invalid!"); - return ERR_DH_FWK_PARA_INVALID; + if (allEntries.empty() || allEntries.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("AllEntries is empty or too large!"); + return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; } for (const auto& item : allEntries) { values.push_back(item.value.ToString()); @@ -300,8 +313,7 @@ int32_t DBAdapter::GetDataByKeyPrefix(const std::string &keyPrefix, std::vector< int32_t DBAdapter::PutData(const std::string &key, const std::string &value) { - if (key.empty() || key.size() > MAX_MESSAGE_LEN || value.empty() || value.size() > MAX_MESSAGE_LEN) { - DHLOGI("Param is invalid!"); + if (IdLengthInvalid(key) || MessageLengthInvalid(value)) { return ERR_DH_FWK_PARA_INVALID; } std::lock_guard lock(dbAdapterMutex_); @@ -321,6 +333,9 @@ int32_t DBAdapter::PutData(const std::string &key, const std::string &value) int32_t DBAdapter::PutDataBatch(const std::vector &keys, const std::vector &values) { + if (ArrayLengthInvalid(keys) || ArrayLengthInvalid(values)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(dbAdapterMutex_); if (kvStoragePtr_ == nullptr) { DHLOGE("kvStoragePtr_ is null"); @@ -471,6 +486,9 @@ void DBAdapter::DeleteKvStore() int32_t DBAdapter::RemoveDeviceData(const std::string &deviceId) { + if (IdLengthInvalid(deviceId)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(dbAdapterMutex_); if (kvStoragePtr_ == nullptr) { DHLOGE("kvStoragePtr_ is null"); @@ -487,6 +505,9 @@ int32_t DBAdapter::RemoveDeviceData(const std::string &deviceId) int32_t DBAdapter::RemoveDataByKey(const std::string &key) { + if (IdLengthInvalid(key)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(dbAdapterMutex_); if (kvStoragePtr_ == nullptr) { DHLOGE("kvStoragePtr_ is null"); @@ -504,6 +525,9 @@ int32_t DBAdapter::RemoveDataByKey(const std::string &key) std::vector DBAdapter::GetEntriesByKeys(const std::vector &keys) { + if (ArrayLengthInvalid(keys)) { + return {}; + } DHLOGI("call"); std::vector entries; if (keys.empty()) { diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/local_capability_info_manager.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/local_capability_info_manager.cpp index 574e98429fc468404cf0868235df5347109906d9..a813eee5a64a01e28e1ffcb8183f0b40142976c4 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/local_capability_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/local_capability_info_manager.cpp @@ -83,6 +83,9 @@ int32_t LocalCapabilityInfoManager::UnInit() int32_t LocalCapabilityInfoManager::SyncDeviceInfoFromDB(const std::string &deviceId) { + if (IdLengthInvalid(deviceId)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("Sync DeviceInfo from DB, deviceId: %{public}s", GetAnonyString(deviceId).c_str()); std::lock_guard lock(capInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { @@ -94,8 +97,8 @@ int32_t LocalCapabilityInfoManager::SyncDeviceInfoFromDB(const std::string &devi DHLOGE("Query data from DB by deviceId failed, id: %{public}s", GetAnonyString(deviceId).c_str()); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } - if (dataVector.size() == 0 || dataVector.size() > MAX_DB_RECORD_SIZE) { - DHLOGE("DataVector size is invalid!"); + if (dataVector.empty() || dataVector.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("On dataVector error, maybe empty or too large."); return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; } for (const auto &data : dataVector) { @@ -111,8 +114,8 @@ int32_t LocalCapabilityInfoManager::SyncDeviceInfoFromDB(const std::string &devi int32_t LocalCapabilityInfoManager::AddCapability(const std::vector> &resInfos) { - if (resInfos.size() == 0 || resInfos.size() > MAX_DB_RECORD_SIZE) { - DHLOGE("ResInfos size is invalid!"); + if (resInfos.empty() || resInfos.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("resInfo is empty or too large!"); return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; } std::lock_guard lock(capInfoMgrMutex_); @@ -146,6 +149,9 @@ int32_t LocalCapabilityInfoManager::AddCapability(const std::vector lock(capInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { @@ -166,6 +172,10 @@ int32_t LocalCapabilityInfoManager::RemoveCapabilityInfoByKey(const std::string void LocalCapabilityInfoManager::GetCapabilitiesByDeviceId(const std::string &deviceId, std::vector> &resInfos) { + if (IdLengthInvalid(deviceId) || resInfos.empty() || resInfos.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("On parameter error, maybe empty or too large!"); + return; + } std::lock_guard lock(capInfoMgrMutex_); for (auto &capabilityInfo : globalCapInfoMap_) { if (IsCapKeyMatchDeviceId(capabilityInfo.first, deviceId)) { @@ -177,6 +187,9 @@ void LocalCapabilityInfoManager::GetCapabilitiesByDeviceId(const std::string &de int32_t LocalCapabilityInfoManager::GetCapability(const std::string &deviceId, const std::string &dhId, std::shared_ptr &capPtr) { + if (IdLengthInvalid(deviceId) || IdLengthInvalid(dhId)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(capInfoMgrMutex_); std::string key = GetCapabilityKey(deviceId, dhId); if (globalCapInfoMap_.find(key) == globalCapInfoMap_.end()) { @@ -189,6 +202,9 @@ int32_t LocalCapabilityInfoManager::GetCapability(const std::string &deviceId, c int32_t LocalCapabilityInfoManager::GetDataByKey(const std::string &key, std::shared_ptr &capInfoPtr) { + if (IdLengthInvalid(key)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(capInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGI("dbAdapterPtr_ is null"); @@ -216,6 +232,9 @@ int32_t LocalCapabilityInfoManager::GetDataByDHType(const DHType dhType, Capabil int32_t LocalCapabilityInfoManager::GetDataByKeyPrefix(const std::string &keyPrefix, CapabilityInfoMap &capabilityMap) { + if (KeySizeInvalid(keyPrefix)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(capInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr is null"); @@ -226,8 +245,8 @@ int32_t LocalCapabilityInfoManager::GetDataByKeyPrefix(const std::string &keyPre DHLOGE("Query capability info from db failed, key: %{public}s", GetAnonyString(keyPrefix).c_str()); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } - if (dataVector.size() == 0 || dataVector.size() > MAX_DB_RECORD_SIZE) { - DHLOGE("DataVector size is invalid!"); + if (dataVector.empty() || dataVector.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("On dataVector error, maybe empty or too large!"); return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; } for (const auto &data : dataVector) { diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/meta_capability_info.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/meta_capability_info.cpp index 6630e29c80189b44d8e7bd88b2e41801030e36c7..d0648c18733eebd94564e6e61ef7527c208b6e54 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/meta_capability_info.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/meta_capability_info.cpp @@ -52,6 +52,9 @@ void MetaCapabilityInfo::SetSinkVersion(const std::string &sinkVersion) int32_t MetaCapabilityInfo::FromJsonString(const std::string &jsonStr) { + if (JsonLengthInvalid(jsonStr)) { + return ERR_DH_FWK_PARA_INVALID; + } cJSON *jsonObj = cJSON_Parse(jsonStr.c_str()); if (jsonObj == NULL) { DHLOGE("jsonStr parse failed"); @@ -143,6 +146,10 @@ std::string MetaCapabilityInfo::GetAnonymousKey() const void ToJson(cJSON *jsonObject, const MetaCapabilityInfo &metaCapInfo) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } cJSON_AddStringToObject(jsonObject, DH_ID.c_str(), metaCapInfo.GetDHId().c_str()); cJSON_AddStringToObject(jsonObject, DEV_ID.c_str(), metaCapInfo.GetDeviceId().c_str()); cJSON_AddStringToObject(jsonObject, DEV_NAME.c_str(), metaCapInfo.GetDeviceName().c_str()); diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/meta_info_manager.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/meta_info_manager.cpp index 7eddc2ad84f809afeee6677155a0923ad82ef229..bf5d35f7d7f0015728ae9961e10eb900dd58b1f8 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/meta_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/meta_info_manager.cpp @@ -116,8 +116,8 @@ int32_t MetaInfoManager::UnInit() int32_t MetaInfoManager::AddMetaCapInfos(const std::vector> &metaCapInfos) { - if (metaCapInfos.size() == 0 || metaCapInfos.size() > MAX_DB_RECORD_SIZE) { - DHLOGE("metaCapInfos size is invalid!"); + if (metaCapInfos.empty() || metaCapInfos.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("MetaCapInfos is empty or too large!"); return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; } std::lock_guard lock(metaInfoMgrMutex_); @@ -156,6 +156,9 @@ int32_t MetaInfoManager::AddMetaCapInfos(const std::vector lock(metaInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { @@ -167,8 +170,8 @@ int32_t MetaInfoManager::SyncMetaInfoFromDB(const std::string &udidHash) DHLOGE("Query Metadata from DB by udidHash failed, udidHash: %{public}s", GetAnonyString(udidHash).c_str()); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } - if (dataVector.size() == 0 || dataVector.size() > MAX_DB_RECORD_SIZE) { - DHLOGE("DataVector size is invalid!"); + if (dataVector.empty() || dataVector.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("On dataVector error, maybe empty or too large."); return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; } for (const auto &data : dataVector) { @@ -195,8 +198,8 @@ int32_t MetaInfoManager::SyncRemoteMetaInfos() DHLOGE("Query all Metadata from DB failed"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } - if (dataVector.size() == 0 || dataVector.size() > MAX_DB_RECORD_SIZE) { - DHLOGE("DataVector size is invalid!"); + if (dataVector.empty() || dataVector.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("On dataVector error, maybe empty or too large."); return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; } for (const auto &data : dataVector) { @@ -224,6 +227,9 @@ int32_t MetaInfoManager::SyncRemoteMetaInfos() int32_t MetaInfoManager::GetDataByKeyPrefix(const std::string &keyPrefix, MetaCapInfoMap &metaCapMap) { + if (KeySizeInvalid(keyPrefix)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(metaInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr is null"); @@ -234,8 +240,8 @@ int32_t MetaInfoManager::GetDataByKeyPrefix(const std::string &keyPrefix, MetaCa DHLOGE("Query metaInfo from db failed, keyPrefix: %{public}s", GetAnonyString(keyPrefix).c_str()); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } - if (dataVector.size() == 0 || dataVector.size() > MAX_DB_RECORD_SIZE) { - DHLOGE("DataVector size is invalid!"); + if (dataVector.empty() || dataVector.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("On dataVector error, maybe empty or too large."); return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; } for (const auto &data : dataVector) { @@ -251,6 +257,9 @@ int32_t MetaInfoManager::GetDataByKeyPrefix(const std::string &keyPrefix, MetaCa int32_t MetaInfoManager::RemoveMetaInfoByKey(const std::string &key) { + if (KeySizeInvalid(key)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("Remove device metaInfo, key: %{public}s", GetAnonyString(key).c_str()); std::lock_guard lock(metaInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { @@ -269,6 +278,9 @@ int32_t MetaInfoManager::RemoveMetaInfoByKey(const std::string &key) int32_t MetaInfoManager::GetMetaCapInfo(const std::string &udidHash, const std::string &dhId, std::shared_ptr &metaCapPtr) { + if (HashSizeInvalid(udidHash) || IdLengthInvalid(dhId)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(metaInfoMgrMutex_); std::string key = GetCapabilityKey(udidHash, dhId); if (globalMetaInfoMap_.find(key) == globalMetaInfoMap_.end()) { @@ -282,6 +294,9 @@ int32_t MetaInfoManager::GetMetaCapInfo(const std::string &udidHash, void MetaInfoManager::GetMetaCapInfosByUdidHash(const std::string &udidHash, std::vector> &metaCapInfos) { + if (HashSizeInvalid(udidHash)) { + return; + } std::lock_guard lock(metaInfoMgrMutex_); for (auto &metaCapInfo : globalMetaInfoMap_) { if (IsCapKeyMatchDeviceId(metaCapInfo.first, udidHash)) { @@ -292,6 +307,9 @@ void MetaInfoManager::GetMetaCapInfosByUdidHash(const std::string &udidHash, int32_t MetaInfoManager::GetMetaCapByValue(const std::string &value, std::shared_ptr &metaCapPtr) { + if (MessageLengthInvalid(value)) { + return ERR_DH_FWK_PARA_INVALID; + } if (metaCapPtr == nullptr) { metaCapPtr = std::make_shared(); } @@ -347,6 +365,10 @@ void MetaInfoManager::OnChange(const DistributedKv::DataOrigin &origin, Keys &&k void MetaInfoManager::HandleMetaCapabilityAddChange(const std::vector &insertRecords) { + if (insertRecords.empty() || insertRecords.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("Records is empty or too large!"); + return; + } std::lock_guard lock(metaInfoMgrMutex_); for (const auto &item : insertRecords) { const std::string value = item.value.ToString(); @@ -381,6 +403,10 @@ void MetaInfoManager::HandleMetaCapabilityAddChange(const std::vector &updateRecords) { + if (updateRecords.empty() || updateRecords.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("Records is empty or too large!"); + return; + } std::lock_guard lock(metaInfoMgrMutex_); for (const auto &item : updateRecords) { const std::string value = item.value.ToString(); @@ -397,6 +423,10 @@ void MetaInfoManager::HandleMetaCapabilityUpdateChange(const std::vector &deleteRecords) { + if (deleteRecords.empty() || deleteRecords.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("Records is empty or too large!"); + return; + } std::lock_guard lock(metaInfoMgrMutex_); for (const auto &item : deleteRecords) { const std::string value = item.value.ToString(); @@ -413,6 +443,9 @@ void MetaInfoManager::HandleMetaCapabilityDeleteChange(const std::vector MetaInfoManager::GetEntriesByKeys(const std::vector &keys) { + if (ArrayLengthInvalid(keys)) { + return {}; + } DHLOGI("call"); if (keys.empty()) { DHLOGE("keys empty."); diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp index 6871330b24d961261e6eff707748fec34abc040c..10bd5654af0ee4eab6c4721042297668e122a90e 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp @@ -32,6 +32,9 @@ namespace DistributedHardware { int32_t VersionInfo::FromJsonString(const std::string &jsonStr) { + if (JsonLengthInvalid(jsonStr)) { + return ERR_DH_FWK_PARA_INVALID; + } cJSON *jsonObj = cJSON_Parse(jsonStr.c_str()); if (jsonObj == NULL) { DHLOGE("json string parse failed"); @@ -63,6 +66,10 @@ std::string VersionInfo::ToJsonString() const void ToJson(cJSON *jsonObject, const VersionInfo &versionInfo) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } cJSON_AddStringToObject(jsonObject, DEV_ID.c_str(), versionInfo.deviceId.c_str()); cJSON_AddStringToObject(jsonObject, DH_VER.c_str(), versionInfo.dhVersion.c_str()); @@ -90,6 +97,10 @@ void ToJson(cJSON *jsonObject, const VersionInfo &versionInfo) void FromJson(const cJSON *jsonObject, CompVersion &compVer) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } if (IsString(jsonObject, NAME)) { compVer.name = cJSON_GetObjectItem(jsonObject, NAME.c_str())->valuestring; } @@ -110,6 +121,10 @@ void FromJson(const cJSON *jsonObject, CompVersion &compVer) void FromJson(const cJSON *jsonObject, VersionInfo &versionInfo) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } if (IsString(jsonObject, DEV_ID)) { versionInfo.deviceId = cJSON_GetObjectItem(jsonObject, DEV_ID.c_str())->valuestring; } diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp index b26557e68803a4f1b2fb3c3e7a95adbdb2b2ad4d..3fee3d79fc6307cdf6e089098bd5f8ffa828c702 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp @@ -138,6 +138,9 @@ int32_t VersionInfoManager::AddVersion(const VersionInfo &versionInfo) int32_t VersionInfoManager::GetVersionInfoByDeviceId(const std::string &deviceId, VersionInfo &versionInfo) { + if (IdLengthInvalid(deviceId)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(verInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr_ is null"); @@ -167,6 +170,9 @@ void VersionInfoManager::UpdateVersionCache(const VersionInfo &versionInfo) int32_t VersionInfoManager::RemoveVersionInfoByDeviceId(const std::string &deviceId) { + if (IdLengthInvalid(deviceId)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("Remove version device info, key: %{public}s", GetAnonyString(deviceId).c_str()); std::lock_guard lock(verInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { @@ -192,6 +198,9 @@ int32_t VersionInfoManager::RemoveVersionInfoByDeviceId(const std::string &devic int32_t VersionInfoManager::SyncVersionInfoFromDB(const std::string &deviceId) { + if (IdLengthInvalid(deviceId)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("Sync versionInfo from DB, deviceId: %{public}s", GetAnonyString(deviceId).c_str()); std::lock_guard lock(verInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { @@ -227,8 +236,8 @@ int32_t VersionInfoManager::SyncRemoteVersionInfos() DHLOGE("Query all data from DB failed"); return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL; } - if (dataVector.size() == 0 || dataVector.size() > MAX_DB_RECORD_SIZE) { - DHLOGE("DataVector Size is invalid!"); + if (dataVector.empty() || dataVector.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("On dataVector error, maybe empty or too large."); return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; } for (const auto &data : dataVector) { @@ -274,6 +283,10 @@ void VersionInfoManager::OnChange(const DistributedKv::ChangeNotification &chang void VersionInfoManager::HandleVersionAddChange(const std::vector &insertRecords) { + if (insertRecords.empty() || insertRecords.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("Records is empty or too large!"); + return; + } DHLOGI("Version add change"); for (const auto &item : insertRecords) { const std::string value = item.value.ToString(); @@ -287,6 +300,10 @@ void VersionInfoManager::HandleVersionAddChange(const std::vector &updateRecords) { + if (updateRecords.empty() || updateRecords.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("Records is empty or too large!"); + return; + } DHLOGI("Version update change"); for (const auto &item : updateRecords) { const std::string value = item.value.ToString(); @@ -300,6 +317,10 @@ void VersionInfoManager::HandleVersionUpdateChange(const std::vector &deleteRecords) { + if (deleteRecords.empty() || deleteRecords.size() > MAX_DB_RECORD_SIZE) { + DHLOGE("Records is empty or too large!"); + return; + } DHLOGI("Version delete change"); for (const auto &item : deleteRecords) { const std::string value = item.value.ToString(); diff --git a/services/distributedhardwarefwkservice/src/transport/dh_comm_tool.cpp b/services/distributedhardwarefwkservice/src/transport/dh_comm_tool.cpp index 3998af7e699aa331e46eb742d73ded3c0d3266dd..05df2ed02109d6a96310a230a4eeba11967e960e 100644 --- a/services/distributedhardwarefwkservice/src/transport/dh_comm_tool.cpp +++ b/services/distributedhardwarefwkservice/src/transport/dh_comm_tool.cpp @@ -197,7 +197,7 @@ void DHCommTool::DHCommToolEventHandler::ProcessFullCapsRsp(const FullCapsRsp &c const std::shared_ptr dhCommToolPtr) { if (capsRsp.networkId.empty() || capsRsp.caps.empty()) { - DHLOGE("Receive remote caps info invalid"); + DHLOGE("Receive remote caps info invalid!"); return; } // after receive rsp, close dsoftbus channel diff --git a/services/distributedhardwarefwkservice/src/transport/dh_transport.cpp b/services/distributedhardwarefwkservice/src/transport/dh_transport.cpp index 90a54bd775da3bf1b118ac1947c9f3af9b4c4828..078be884016ecdab87ce972cd1c26b1afda05258 100644 --- a/services/distributedhardwarefwkservice/src/transport/dh_transport.cpp +++ b/services/distributedhardwarefwkservice/src/transport/dh_transport.cpp @@ -108,6 +108,9 @@ void DHTransport::OnBytesReceived(int32_t socketId, const void *data, uint32_t d void DHTransport::HandleReceiveMessage(const std::string &payload) { + if (MessageLengthInvalid(payload)) { + return; + } std::string rawPayload = Decompress(payload); cJSON *root = cJSON_Parse(rawPayload.c_str()); @@ -205,6 +208,10 @@ void OnFile(int32_t socket, FileEvent *event) void OnQos(int32_t socket, QoSEvent eventId, const QosTV *qos, uint32_t qosCount) { + if (qosCount <= 0 || qosCount > MAX_ROUND_SIZE) { + DHLOGE("qosCount is invalid!"); + return; + } DHLOGI("OnQos, socket: %{public}d, QoSEvent: %{public}d, qosCount: %{public}" PRIu32, socket, (int32_t)eventId, qosCount); for (uint32_t idx = 0; idx < qosCount; idx++) { @@ -241,6 +248,9 @@ int32_t DHTransport::CreateServerSocket() int32_t DHTransport::CreateClientSocket(const std::string &remoteNetworkId) { + if (IdLengthInvalid(remoteNetworkId)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("CreateClientSocket start, peerNetworkId: %{public}s", GetAnonyString(remoteNetworkId).c_str()); std::string peerSocketName = DH_FWK_SESSION_NAME + remoteNetworkId.substr(0, INTERCEPT_STRING_LENGTH); SocketInfo info = { @@ -305,6 +315,9 @@ int32_t DHTransport::UnInit() bool DHTransport::IsDeviceSessionOpened(const std::string &remoteNetworkId, int32_t &socketId) { + if (IdLengthInvalid(remoteNetworkId)) { + return false; + } std::lock_guard lock(rmtSocketIdMtx_); if (remoteDevSocketIds_.find(remoteNetworkId) == remoteDevSocketIds_.end()) { return false; @@ -330,12 +343,18 @@ std::string DHTransport::GetRemoteNetworkIdBySocketId(int32_t socketId) void DHTransport::ClearDeviceSocketOpened(const std::string &remoteDevId) { + if (IdLengthInvalid(remoteDevId)) { + return; + } std::lock_guard lock(rmtSocketIdMtx_); remoteDevSocketIds_.erase(remoteDevId); } int32_t DHTransport::StartSocket(const std::string &remoteNetworkId) { + if (IdLengthInvalid(remoteNetworkId)) { + return ERR_DH_FWK_PARA_INVALID; + } int32_t socketId = -1; if (IsDeviceSessionOpened(remoteNetworkId, socketId)) { DHLOGE("Softbus session has already opened, deviceId: %{public}s", GetAnonyString(remoteNetworkId).c_str()); @@ -371,6 +390,9 @@ int32_t DHTransport::StartSocket(const std::string &remoteNetworkId) int32_t DHTransport::StopSocket(const std::string &remoteNetworkId) { + if (IdLengthInvalid(remoteNetworkId)) { + return ERR_DH_FWK_PARA_INVALID; + } int32_t socketId = -1; if (!IsDeviceSessionOpened(remoteNetworkId, socketId)) { DHLOGI("remote dev may be not opened, remoteNetworkId: %{public}s", GetAnonyString(remoteNetworkId).c_str()); @@ -386,6 +408,9 @@ int32_t DHTransport::StopSocket(const std::string &remoteNetworkId) int32_t DHTransport::Send(const std::string &remoteNetworkId, const std::string &payload) { + if (IdLengthInvalid(remoteNetworkId) || MessageLengthInvalid(payload)) { + return ERR_DH_FWK_PARA_INVALID; + } int32_t socketId = -1; if (!IsDeviceSessionOpened(remoteNetworkId, socketId)) { DHLOGI("The session is not open, target networkId: %{public}s", GetAnonyString(remoteNetworkId).c_str()); diff --git a/services/distributedhardwarefwkservice/src/transport/dh_transport_obj.cpp b/services/distributedhardwarefwkservice/src/transport/dh_transport_obj.cpp index feebd097b9c02795e51f888b84936385edfdcfb6..467075f5962ff97b1e95f2604be4711da8b798d0 100644 --- a/services/distributedhardwarefwkservice/src/transport/dh_transport_obj.cpp +++ b/services/distributedhardwarefwkservice/src/transport/dh_transport_obj.cpp @@ -16,12 +16,17 @@ #include "dh_transport_obj.h" #include "dh_utils_tool.h" +#include "distributed_hardware_log.h" namespace OHOS { namespace DistributedHardware { void ToJson(cJSON *jsonObject, const FullCapsRsp &capsRsp) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } const char *networkId = capsRsp.networkId.c_str(); cJSON_AddStringToObject(jsonObject, CAPS_RSP_NETWORKID_KEY, networkId); cJSON *capArr = cJSON_CreateArray(); @@ -42,6 +47,10 @@ void ToJson(cJSON *jsonObject, const FullCapsRsp &capsRsp) void FromJson(const cJSON *jsonObject, FullCapsRsp &capsRsp) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } std::string keyNetworkId(CAPS_RSP_NETWORKID_KEY); if (IsString(jsonObject, keyNetworkId)) { capsRsp.networkId = cJSON_GetObjectItem(jsonObject, CAPS_RSP_NETWORKID_KEY)->valuestring; @@ -61,6 +70,10 @@ void FromJson(const cJSON *jsonObject, FullCapsRsp &capsRsp) void ToJson(cJSON *jsonObject, const CommMsg &commMsg) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } cJSON_AddNumberToObject(jsonObject, COMM_MSG_CODE_KEY, commMsg.code); const char *msg = commMsg.msg.c_str(); cJSON_AddStringToObject(jsonObject, COMM_MSG_MSG_KEY, msg); @@ -68,6 +81,10 @@ void ToJson(cJSON *jsonObject, const CommMsg &commMsg) void FromJson(const cJSON *jsonObject, CommMsg &commMsg) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } std::string keyCode(COMM_MSG_CODE_KEY); if (IsInt32(jsonObject, keyCode)) { commMsg.code = cJSON_GetObjectItem(jsonObject, COMM_MSG_CODE_KEY)->valueint; diff --git a/services/distributedhardwarefwkservice/src/utils/dh_context.cpp b/services/distributedhardwarefwkservice/src/utils/dh_context.cpp index 0959d9534fa3859706568f07e9e43ea47fc7e671..115f44bedd9758dd5d2c3e46d58cf710e6f37177 100644 --- a/services/distributedhardwarefwkservice/src/utils/dh_context.cpp +++ b/services/distributedhardwarefwkservice/src/utils/dh_context.cpp @@ -114,8 +114,7 @@ const DeviceInfo& DHContext::GetDeviceInfo() void DHContext::AddOnlineDevice(const std::string &udid, const std::string &uuid, const std::string &networkId) { - if (udid.empty() || uuid.empty() || networkId.empty()) { - DHLOGE("Add online device id invalid"); + if (IdLengthInvalid(udid) || IdLengthInvalid(uuid) || IdLengthInvalid(networkId)) { return; } std::unique_lock lock(onlineDevMutex_); @@ -137,6 +136,9 @@ void DHContext::AddOnlineDevice(const std::string &udid, const std::string &uuid void DHContext::RemoveOnlineDeviceByUUID(const std::string &uuid) { + if (IdLengthInvalid(uuid)) { + return; + } std::unique_lock lock(onlineDevMutex_); for (auto iter = devIdEntrySet_.begin(); iter != devIdEntrySet_.end(); iter++) { if (iter->uuid == uuid) { @@ -148,7 +150,7 @@ void DHContext::RemoveOnlineDeviceByUUID(const std::string &uuid) bool DHContext::IsDeviceOnline(const std::string &uuid) { - if (uuid.empty()) { + if (IdLengthInvalid(uuid)) { return false; } std::shared_lock lock(onlineDevMutex_); @@ -170,6 +172,9 @@ size_t DHContext::GetOnlineCount() std::string DHContext::GetNetworkIdByUUID(const std::string &uuid) { + if (IdLengthInvalid(uuid)) { + return ""; + } std::unique_lock lock(onlineDevMutex_); std::string networkId = ""; for (auto iter = devIdEntrySet_.begin(); iter != devIdEntrySet_.end(); iter++) { @@ -183,6 +188,9 @@ std::string DHContext::GetNetworkIdByUUID(const std::string &uuid) std::string DHContext::GetUdidHashIdByUUID(const std::string &uuid) { + if (IdLengthInvalid(uuid)) { + return ""; + } std::unique_lock lock(onlineDevMutex_); std::string udidHash = ""; for (auto iter = devIdEntrySet_.begin(); iter != devIdEntrySet_.end(); iter++) { @@ -196,6 +204,9 @@ std::string DHContext::GetUdidHashIdByUUID(const std::string &uuid) std::string DHContext::GetUUIDByNetworkId(const std::string &networkId) { + if (IdLengthInvalid(networkId)) { + return ""; + } std::unique_lock lock(onlineDevMutex_); std::string uuid = ""; for (auto iter = devIdEntrySet_.begin(); iter != devIdEntrySet_.end(); iter++) { @@ -209,6 +220,9 @@ std::string DHContext::GetUUIDByNetworkId(const std::string &networkId) std::string DHContext::GetUDIDByNetworkId(const std::string &networkId) { + if (IdLengthInvalid(networkId)) { + return ""; + } std::unique_lock lock(onlineDevMutex_); std::string udid = ""; for (auto iter = devIdEntrySet_.begin(); iter != devIdEntrySet_.end(); iter++) { @@ -222,6 +236,9 @@ std::string DHContext::GetUDIDByNetworkId(const std::string &networkId) std::string DHContext::GetUUIDByDeviceId(const std::string &deviceId) { + if (IdLengthInvalid(deviceId)) { + return ""; + } std::unique_lock lock(onlineDevMutex_); std::string uuid = ""; for (auto iter = devIdEntrySet_.begin(); iter != devIdEntrySet_.end(); iter++) { @@ -235,6 +252,9 @@ std::string DHContext::GetUUIDByDeviceId(const std::string &deviceId) std::string DHContext::GetNetworkIdByDeviceId(const std::string &deviceId) { + if (IdLengthInvalid(deviceId)) { + return ""; + } std::unique_lock lock(onlineDevMutex_); std::string networkId = ""; for (auto iter = devIdEntrySet_.begin(); iter != devIdEntrySet_.end(); iter++) { @@ -248,6 +268,9 @@ std::string DHContext::GetNetworkIdByDeviceId(const std::string &deviceId) std::string DHContext::GetDeviceIdByDBGetPrefix(const std::string &prefix) { + if (IdLengthInvalid(prefix)) { + return ""; + } std::string id = ""; if (prefix.empty()) { return id; @@ -274,15 +297,14 @@ void DHContext::RegisDHFWKIsomerismListener() void DHContext::DHFWKIsomerismListener::OnMessage(const DHTopic topic, const std::string &message) { + if (MessageLengthInvalid(message)) { + return; + } DHLOGI("OnMessage topic: %{public}u", static_cast(topic)); if (topic != DHTopic::TOPIC_ISOMERISM) { DHLOGE("OnMessage topic is wrong"); return; } - if (message.length() > MAX_MESSAGE_LEN) { - DHLOGE("OnMessage error, message too large"); - return; - } cJSON *messageJson = cJSON_Parse(message.c_str()); if (messageJson == nullptr) { DHLOGE("OnMessage error, parse failed"); @@ -313,6 +335,9 @@ void DHContext::DHFWKIsomerismListener::OnMessage(const DHTopic topic, const std void DHContext::AddIsomerismConnectDev(const std::string &IsomerismDeviceId) { + if (IdLengthInvalid(IsomerismDeviceId)) { + return; + } DHLOGI("AddIsomerismConnectDev id = %{public}s", GetAnonyString(IsomerismDeviceId).c_str()); std::shared_lock lock(connectDevMutex_); connectedDevIds_.insert(IsomerismDeviceId); @@ -320,6 +345,9 @@ void DHContext::AddIsomerismConnectDev(const std::string &IsomerismDeviceId) void DHContext::DelIsomerismConnectDev(const std::string &IsomerismDeviceId) { + if (IdLengthInvalid(IsomerismDeviceId)) { + return; + } DHLOGI("DelIsomerismConnectDev id = %{public}s", GetAnonyString(IsomerismDeviceId).c_str()); std::shared_lock lock(connectDevMutex_); if (connectedDevIds_.find(IsomerismDeviceId) == connectedDevIds_.end()) { diff --git a/services/distributedhardwarefwkservice/src/versionmanager/version_manager.cpp b/services/distributedhardwarefwkservice/src/versionmanager/version_manager.cpp index 8cd7b0340a2f7537fc07a72938fd5b09523017b2..aeb74beb45d980af0228d6c12d0295b030392f4a 100644 --- a/services/distributedhardwarefwkservice/src/versionmanager/version_manager.cpp +++ b/services/distributedhardwarefwkservice/src/versionmanager/version_manager.cpp @@ -19,6 +19,7 @@ #include "component_loader.h" #include "dh_context.h" #include "distributed_hardware_log.h" +#include "dh_utils_tool.h" namespace OHOS { namespace DistributedHardware { @@ -60,6 +61,9 @@ void VersionManager::ShowLocalVersion(const DHVersion &dhVersion) const int32_t VersionManager::AddDHVersion(const std::string &uuid, const DHVersion &dhVersion) { + if (IdLengthInvalid(uuid)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("addDHVersion uuid: %{public}s", GetAnonyString(uuid).c_str()); std::lock_guard lock(versionMutex_); dhVersions_[uuid] = dhVersion; @@ -68,6 +72,9 @@ int32_t VersionManager::AddDHVersion(const std::string &uuid, const DHVersion &d int32_t VersionManager::RemoveDHVersion(const std::string &uuid) { + if (IdLengthInvalid(uuid)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("uuid: %{public}s", GetAnonyString(uuid).c_str()); std::lock_guard lock(versionMutex_); std::unordered_map::iterator iter = dhVersions_.find(uuid); @@ -81,6 +88,9 @@ int32_t VersionManager::RemoveDHVersion(const std::string &uuid) int32_t VersionManager::GetDHVersion(const std::string &uuid, DHVersion &dhVersion) { + if (IdLengthInvalid(uuid)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("uuid: %{public}s", GetAnonyString(uuid).c_str()); std::lock_guard lock(versionMutex_); std::unordered_map::iterator iter = dhVersions_.find(uuid); @@ -95,6 +105,9 @@ int32_t VersionManager::GetDHVersion(const std::string &uuid, DHVersion &dhVersi int32_t VersionManager::GetCompVersion(const std::string &uuid, const DHType dhType, CompVersion &compVersion) { + if (IdLengthInvalid(uuid)) { + return ERR_DH_FWK_PARA_INVALID; + } DHVersion dhVersion; int32_t ret = GetDHVersion(uuid, dhVersion); if (ret != DH_FWK_SUCCESS) { diff --git a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/pluginlistenerimpl/src/mock_hardware_handler.cpp b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/pluginlistenerimpl/src/mock_hardware_handler.cpp index ef388b0df6e383feefb98906917c225a9b1b2265..3927913614f2c3e41eb20d3b19fddf86ae4ed9f6 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/pluginlistenerimpl/src/mock_hardware_handler.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/pluginlistenerimpl/src/mock_hardware_handler.cpp @@ -15,6 +15,7 @@ #include "mock_hardware_handler.h" +#include "distributed_hardware_log.h" #include "distributed_hardware_errno.h" namespace OHOS { diff --git a/utils/include/dh_utils_tool.h b/utils/include/dh_utils_tool.h index c52e1c4558c5652a6fc819b12f62983871f48b85..8cffdc14f0a03ceeca8e319b2f4dd13289e84920 100644 --- a/utils/include/dh_utils_tool.h +++ b/utils/include/dh_utils_tool.h @@ -65,6 +65,19 @@ std::string Compress(const std::string& data); std::string Decompress(const std::string& data); bool GetSysPara(const char *key, bool &value); + +bool IdLengthInvalid(const std::string &input); + +bool MessageLengthInvalid(const std::string &input); + + +bool JsonLengthInvalid(const std::string &jsonStr); + +bool ArrayLengthInvalid(const std::vector &array); + +bool KeySizeInvalid(const std::string &key); + +bool HashSizeInvalid(const std::string &hashValue); } // namespace DistributedHardware } // namespace OHOS #endif diff --git a/utils/src/anonymous_string.cpp b/utils/src/anonymous_string.cpp index c5491d1258f1e3c3907f3a8636ae7b8f5741c13b..f91191afe48999b283e3b029e425e4ab23f81a80 100644 --- a/utils/src/anonymous_string.cpp +++ b/utils/src/anonymous_string.cpp @@ -19,6 +19,10 @@ #include #include "securec.h" +#include "constants.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#include "dh_utils_tool.h" namespace OHOS { namespace DistributedHardware { @@ -30,7 +34,7 @@ namespace { } std::string GetAnonyString(const std::string &value) { - if (value.empty()) { + if (MessageLengthInvalid(value)) { return ""; } std::string res; diff --git a/utils/src/dh_utils_tool.cpp b/utils/src/dh_utils_tool.cpp index be684494c6980145bfb7ff808afa28adc4f8c88a..a46f30c6ffd749912f117027b21e6e774f7d1482 100644 --- a/utils/src/dh_utils_tool.cpp +++ b/utils/src/dh_utils_tool.cpp @@ -29,7 +29,6 @@ #include "parameter.h" #include "device_manager.h" #include "dm_device_info.h" - #include "constants.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" @@ -82,9 +81,6 @@ std::string GetRandomID() std::string GetUUIDByDm(const std::string &networkId) { - if (networkId.empty()) { - return ""; - } std::string uuid = ""; auto ret = DeviceManager::GetInstance().GetUuidByNetworkId(DH_FWK_PKG_NAME, networkId, uuid); return (ret == DH_FWK_SUCCESS) ? uuid : ""; @@ -92,9 +88,6 @@ std::string GetUUIDByDm(const std::string &networkId) std::string GetUDIDByDm(const std::string &networkId) { - if (networkId.empty()) { - return ""; - } std::string udid = ""; auto ret = DeviceManager::GetInstance().GetUdidByNetworkId(DH_FWK_PKG_NAME, networkId, udid); return (ret == DH_FWK_SUCCESS) ? udid : ""; @@ -102,10 +95,6 @@ std::string GetUDIDByDm(const std::string &networkId) std::string GetDeviceIdByUUID(const std::string &uuid) { - if (uuid.size() == 0 || uuid.size() > MAX_ID_LEN) { - DHLOGE("uuid is invalid!"); - return ""; - } return Sha256(uuid); } @@ -286,5 +275,59 @@ bool GetSysPara(const char *key, bool &value) valueStr >> std::boolalpha >> value; return true; } + +bool IdLengthInvalid(const std::string &input) +{ + if (input.empty() || input.length() > MAX_ID_LEN) { + DHLOGE("On parameter length error, maybe empty or beyond MAX_ID_LEN!"); + return true; + } + return false; +} + +bool MessageLengthInvalid(const std::string &input) +{ + if (input.empty() || input.length() > MAX_MESSAGE_LEN) { + DHLOGE("On parameter error, maybe empty or beyond MAX_MESSAGE_LEN!"); + return true; + } + return false; +} + +bool JsonLengthInvalid(const std::string &jsonStr) +{ + if (jsonStr.empty() || jsonStr.length() > MAX_JSON_SIZE) { + DHLOGE("On parameter error, maybe empty or beyond MAX_JSON_SIZE"); + return true; + } + return false; +} + +bool ArrayLengthInvalid(const std::vector &array) +{ + if (array.empty() || array.size() > MAX_ARR_SIZE) { + DHLOGE("On parameter error, maybe empty or beyond MAX_ARR_SIZE"); + return true; + } + return false; +} + +bool KeySizeInvalid(const std::string &key) +{ + if (key.empty() || key.length() > MAX_KEY_SIZE) { + DHLOGE("On parameter error, maybe empty or beyond MAX_KEY_SIZE"); + return true; + } + return false; +} + +bool HashSizeInvalid(const std::string &hashValue) +{ + if (hashValue.empty() || hashValue.length() > MAX_HASH_SIZE) { + DHLOGE("On parameter error, maybe empty or beyond MAX_HASH_SIZE"); + return true; + } + return false; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/utils/src/histreamer_ability_parser.cpp b/utils/src/histreamer_ability_parser.cpp index a9631116a76b850bbb2400755838fc30416093e6..ee59c2ca7e4e1e9c814ced621f6d130be53667a9 100644 --- a/utils/src/histreamer_ability_parser.cpp +++ b/utils/src/histreamer_ability_parser.cpp @@ -37,6 +37,10 @@ static const std::string VIDEO_BIT_STREAM_FMT = "vd_bit_stream_fmt"; void FromJson(const cJSON *jsonObject, AudioEncoderIn &audioEncoderIn) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } if (!IsString(jsonObject, MIME)) { DHLOGE("AudioEncoderIn MIME is invalid!\n"); return; @@ -59,26 +63,30 @@ void FromJson(const cJSON *jsonObject, AudioEncoderIn &audioEncoderIn) void FromJson(const cJSON *jsonObject, AudioEncoderOut &audioEncoderOut) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } if (!IsString(jsonObject, MIME)) { DHLOGE("AudioEncoderOut MIME is invalid!"); return; } audioEncoderOut.mime = cJSON_GetObjectItem(jsonObject, MIME.c_str())->valuestring; if (!IsUInt32(jsonObject, AD_MPEG_VER)) { - DHLOGE("AudioEncoderOut AD_MPEG_VER is invalid"); + DHLOGE("AudioEncoderOut AD_MPEG_VER is invalid!"); return; } audioEncoderOut.ad_mpeg_ver = (uint32_t)cJSON_GetObjectItem(jsonObject, AD_MPEG_VER.c_str())->valuedouble; if (!IsUInt8(jsonObject, AUDIO_AAC_PROFILE)) { - DHLOGE("AudioEncoderOut AUDIO_AAC_PROFILE is invalid"); + DHLOGE("AudioEncoderOut AUDIO_AAC_PROFILE is invalid!"); return; } audioEncoderOut.aac_profile = (AudioAacProfile)cJSON_GetObjectItem(jsonObject, AUDIO_AAC_PROFILE.c_str())->valuedouble; if (!IsUInt8(jsonObject, AUDIO_AAC_STREAM_FORMAT)) { - DHLOGE("AudioEncoderOut AUDIO_AAC_STREAM_FORMAT is invalid"); + DHLOGE("AudioEncoderOut AUDIO_AAC_STREAM_FORMAT is invalid!"); return; } audioEncoderOut.aac_stm_fmt = @@ -87,14 +95,18 @@ void FromJson(const cJSON *jsonObject, AudioEncoderOut &audioEncoderOut) void FromJson(const cJSON *jsonObject, AudioEncoder &audioEncoder) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } if (!IsString(jsonObject, NAME)) { - DHLOGE("AudioEncoder NAME is invalid"); + DHLOGE("AudioEncoder NAME is invalid!"); return; } audioEncoder.name = cJSON_GetObjectItem(jsonObject, NAME.c_str())->valuestring; if (!IsArray(jsonObject, INS)) { - DHLOGE("AudioEncoder INS is invalid"); + DHLOGE("AudioEncoder INS is invalid!"); return; } cJSON *insJson = cJSON_GetObjectItem(jsonObject, INS.c_str()); @@ -106,7 +118,7 @@ void FromJson(const cJSON *jsonObject, AudioEncoder &audioEncoder) } if (!IsArray(jsonObject, OUTS)) { - DHLOGE("AudioEncoder OUTS is invalid"); + DHLOGE("AudioEncoder OUTS is invalid!"); return; } cJSON *outsJson = cJSON_GetObjectItem(jsonObject, OUTS.c_str()); @@ -120,14 +132,18 @@ void FromJson(const cJSON *jsonObject, AudioEncoder &audioEncoder) void FromJson(const cJSON *jsonObject, AudioDecoderIn &audioDecoderIn) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } if (!IsString(jsonObject, MIME)) { - DHLOGE("AudioDecoderIn MIME is invalid"); + DHLOGE("AudioDecoderIn MIME is invalid!"); return; } audioDecoderIn.mime = cJSON_GetObjectItem(jsonObject, MIME.c_str())->valuestring; if (!IsArray(jsonObject, AUDIO_CHANNEL_LAYOUT)) { - DHLOGE("AudioDecoder AUDIO_CHANNEL_LAYOUT is invalid"); + DHLOGE("AudioDecoder AUDIO_CHANNEL_LAYOUT is invalid!"); return; } const cJSON *channelLayoutJson = cJSON_GetObjectItem(jsonObject, AUDIO_CHANNEL_LAYOUT.c_str()); @@ -139,13 +155,17 @@ void FromJson(const cJSON *jsonObject, AudioDecoderIn &audioDecoderIn) void FromJson(const cJSON *jsonObject, AudioDecoderOut &audioDecoderOut) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } if (!IsString(jsonObject, MIME)) { - DHLOGE("AudioDecoderOut MIME is invalid"); + DHLOGE("AudioDecoderOut MIME is invalid!"); return; } audioDecoderOut.mime = cJSON_GetObjectItem(jsonObject, MIME.c_str())->valuestring; if (!IsArray(jsonObject, AUDIO_SAMPLE_FORMAT)) { - DHLOGE("AudioDecoderOut AUDIO_SAMPLE_FORMAT is invalid"); + DHLOGE("AudioDecoderOut AUDIO_SAMPLE_FORMAT is invalid!"); return; } cJSON *sampleFormatJson = cJSON_GetObjectItem(jsonObject, AUDIO_SAMPLE_FORMAT.c_str()); @@ -157,14 +177,18 @@ void FromJson(const cJSON *jsonObject, AudioDecoderOut &audioDecoderOut) void FromJson(const cJSON *jsonObject, AudioDecoder &audioDecoder) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } if (!IsString(jsonObject, NAME)) { - DHLOGE("AudioDecoderOut MIME is invalid"); + DHLOGE("AudioDecoderOut MIME is invalid!"); return; } audioDecoder.name = cJSON_GetObjectItem(jsonObject, NAME.c_str())->valuestring; if (!IsArray(jsonObject, INS)) { - DHLOGE("AudioDecoder OUTS is invalid"); + DHLOGE("AudioDecoder OUTS is invalid!"); return; } const cJSON *insJson = cJSON_GetObjectItem(jsonObject, INS.c_str()); @@ -175,7 +199,7 @@ void FromJson(const cJSON *jsonObject, AudioDecoder &audioDecoder) audioDecoder.ins.push_back(in); } if (!IsArray(jsonObject, OUTS)) { - DHLOGE("AudioDecoder OUTS is invalid"); + DHLOGE("AudioDecoder OUTS is invalid!"); return; } cJSON *outsJson = cJSON_GetObjectItem(jsonObject, OUTS.c_str()); @@ -189,14 +213,18 @@ void FromJson(const cJSON *jsonObject, AudioDecoder &audioDecoder) void FromJson(const cJSON *jsonObject, VideoEncoderIn &videoEncoderIn) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } if (!IsString(jsonObject, MIME)) { - DHLOGE("VideoEncoderIn MIME is invalid"); + DHLOGE("VideoEncoderIn MIME is invalid!"); return; } videoEncoderIn.mime = cJSON_GetObjectItem(jsonObject, MIME.c_str())->valuestring; if (!IsArray(jsonObject, VIDEO_PIXEL_FMT)) { - DHLOGE("VideoEncoderIn VIDEO_PIXEL_FMT is invalid"); + DHLOGE("VideoEncoderIn VIDEO_PIXEL_FMT is invalid!"); return; } cJSON *videoPixelFmt = cJSON_GetObjectItem(jsonObject, VIDEO_PIXEL_FMT.c_str()); @@ -208,8 +236,12 @@ void FromJson(const cJSON *jsonObject, VideoEncoderIn &videoEncoderIn) void FromJson(const cJSON *jsonObject, VideoEncoderOut &videoEncoderOut) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } if (!IsString(jsonObject, MIME)) { - DHLOGE("VideoEncoderIn MIME is invalid"); + DHLOGE("VideoEncoderIn MIME is invalid!"); return; } videoEncoderOut.mime = cJSON_GetObjectItem(jsonObject, MIME.c_str())->valuestring; @@ -217,14 +249,18 @@ void FromJson(const cJSON *jsonObject, VideoEncoderOut &videoEncoderOut) void FromJson(const cJSON *jsonObject, VideoEncoder &videoEncoder) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } if (!IsString(jsonObject, NAME)) { - DHLOGE("VideoEncoder NAME is invalid"); + DHLOGE("VideoEncoder NAME is invalid!"); return; } videoEncoder.name = cJSON_GetObjectItem(jsonObject, NAME.c_str())->valuestring; if (!IsArray(jsonObject, INS)) { - DHLOGE("VideoEncoder INS is invalid"); + DHLOGE("VideoEncoder INS is invalid!"); return; } cJSON *videoEncoderInsJson = cJSON_GetObjectItem(jsonObject, INS.c_str()); @@ -236,7 +272,7 @@ void FromJson(const cJSON *jsonObject, VideoEncoder &videoEncoder) } if (!IsArray(jsonObject, OUTS)) { - DHLOGE("VideoEncoder OUTS is invalid"); + DHLOGE("VideoEncoder OUTS is invalid!"); return; } cJSON *videoEncoderOutsJson = cJSON_GetObjectItem(jsonObject, OUTS.c_str()); @@ -250,14 +286,18 @@ void FromJson(const cJSON *jsonObject, VideoEncoder &videoEncoder) void FromJson(const cJSON *jsonObject, VideoDecoderIn &videoDecoderIn) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } if (!IsString(jsonObject, MIME)) { - DHLOGE("VideoDecoderIn MIME is invalid"); + DHLOGE("VideoDecoderIn MIME is invalid!"); return; } videoDecoderIn.mime = cJSON_GetObjectItem(jsonObject, MIME.c_str())->valuestring; if (!IsArray(jsonObject, VIDEO_BIT_STREAM_FMT)) { - DHLOGE("VideoDecoderIn VIDEO_BIT_STREAM_FMT is invalid"); + DHLOGE("VideoDecoderIn VIDEO_BIT_STREAM_FMT is invalid!"); return; } cJSON *videoBitStreamFmtJson = cJSON_GetObjectItem(jsonObject, VIDEO_BIT_STREAM_FMT.c_str()); @@ -269,14 +309,18 @@ void FromJson(const cJSON *jsonObject, VideoDecoderIn &videoDecoderIn) void FromJson(const cJSON *jsonObject, VideoDecoderOut &videoDecoderOut) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } if (!IsString(jsonObject, MIME)) { - DHLOGE("VideoDecoderOut MIME is invalid"); + DHLOGE("VideoDecoderOut MIME is invalid!"); return; } videoDecoderOut.mime = cJSON_GetObjectItem(jsonObject, MIME.c_str())->valuestring; if (!IsArray(jsonObject, VIDEO_BIT_STREAM_FMT)) { - DHLOGE("videoDecoderOut VIDEO_PIXEL_FMT is invalid"); + DHLOGE("videoDecoderOut VIDEO_PIXEL_FMT is invalid!"); return; } cJSON *videoPixelFmtJson = cJSON_GetObjectItem(jsonObject, VIDEO_PIXEL_FMT.c_str()); @@ -288,14 +332,18 @@ void FromJson(const cJSON *jsonObject, VideoDecoderOut &videoDecoderOut) void FromJson(const cJSON *jsonObject, VideoDecoder &videoDecoder) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } if (!IsString(jsonObject, NAME)) { - DHLOGE("VideoDecoder NAME is invalid"); + DHLOGE("VideoDecoder NAME is invalid!"); return; } videoDecoder.name = cJSON_GetObjectItem(jsonObject, NAME.c_str())->valuestring; if (!IsArray(jsonObject, INS)) { - DHLOGE("VideoDecoder INS is invalid"); + DHLOGE("VideoDecoder INS is invalid!"); return; } cJSON *videoDecoderInsJson = cJSON_GetObjectItem(jsonObject, INS.c_str()); @@ -307,7 +355,7 @@ void FromJson(const cJSON *jsonObject, VideoDecoder &videoDecoder) } if (!IsArray(jsonObject, OUTS)) { - DHLOGE("VideoDecoder OUTS is invalid"); + DHLOGE("VideoDecoder OUTS is invalid!"); return; } cJSON *videoDecoderOutsJson = cJSON_GetObjectItem(jsonObject, OUTS.c_str()); @@ -322,6 +370,10 @@ void FromJson(const cJSON *jsonObject, VideoDecoder &videoDecoder) template void FromJson(const std::string &key, const cJSON *jsonObject, std::vector &objs) { + if (jsonObject == nullptr) { + DHLOGE("Json pointer is nullptr!"); + return; + } cJSON *json = cJSON_GetObjectItem(jsonObject, key.c_str()); if (json == NULL) { DHLOGE("JSONObject key invalid, key: %{public}s", key.c_str());