From 06f8c46e6f50554da833dce0378072cd73709439 Mon Sep 17 00:00:00 2001 From: li-tiangang4 Date: Mon, 14 Oct 2024 14:04:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A5=E5=8F=82=E6=A0=A1=E9=AA=8C=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=9B=9E=E5=90=885.0.1-Release?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: li-tiangang4 --- common/utils/include/constants.h | 5 + .../src/distributed_hardware_fwk_kit.cpp | 13 +- .../src/ipc/distributed_hardware_proxy.cpp | 13 +- .../src/accessmanager/access_manager.cpp | 18 ++- .../src/componentloader/component_loader.cpp | 32 +++-- .../componentmanager/component_disable.cpp | 7 + .../src/componentmanager/component_enable.cpp | 7 + .../componentmanager/component_manager.cpp | 61 +++++++-- .../componentmanager/component_privacy.cpp | 15 +- .../dh_data_sync_trigger_listener.cpp | 4 + .../componentmanager/dh_state_listener.cpp | 4 + .../src/distributed_hardware_manager.cpp | 6 + .../distributed_hardware_manager_factory.cpp | 6 + .../src/distributed_hardware_service.cpp | 9 ++ .../src/hidumphelper/enabled_comps_dump.cpp | 1 + .../src/ipc/publisher_listener_proxy.cpp | 4 +- .../local_hardware_manager.cpp | 3 +- .../plugin_listener_impl.cpp | 7 +- .../src/lowlatency/low_latency_listener.cpp | 7 +- .../src/publisher/publisher_item.cpp | 7 +- .../src/resourcemanager/capability_info.cpp | 11 ++ .../capability_info_manager.cpp | 46 +++++-- .../src/resourcemanager/capability_utils.cpp | 4 + .../src/resourcemanager/db_adapter.cpp | 33 +++-- .../local_capability_info_manager.cpp | 31 ++++- .../resourcemanager/meta_capability_info.cpp | 7 + .../src/resourcemanager/meta_info_manager.cpp | 33 ++++- .../src/resourcemanager/version_info.cpp | 15 ++ .../resourcemanager/version_info_manager.cpp | 9 ++ .../src/transport/dh_comm_tool.cpp | 2 +- .../src/transport/dh_transport.cpp | 25 ++++ .../src/transport/dh_transport_obj.cpp | 16 +++ .../src/utils/dh_context.cpp | 45 +++++- .../src/versionmanager/version_manager.cpp | 13 ++ .../accessmanager/access_manager_test.cpp | 10 +- .../src/component_loader_test.cpp | 2 +- .../src/component_manager_test.cpp | 2 +- .../src/mock_hardware_handler.cpp | 1 + .../src/resource_manager_test.cpp | 31 ++++- .../src/version_info_manager_test.cpp | 21 ++- utils/include/dh_utils_tool.h | 12 ++ utils/src/anonymous_string.cpp | 6 +- utils/src/dh_utils_tool.cpp | 61 ++++++++- utils/src/histreamer_ability_parser.cpp | 128 +++++++++++++----- 44 files changed, 652 insertions(+), 141 deletions(-) diff --git a/common/utils/include/constants.h b/common/utils/include/constants.h index 5e2a6bea..52bd4b77 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 782e2dd5..40e6749c 100644 --- a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp +++ b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp @@ -19,6 +19,7 @@ #include "anonymous_string.h" #include "constants.h" +#include "dh_utils_tool.h" #include "dhfwk_sa_manager.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" @@ -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 (!IsMessageLengthValid(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 (!IsIdLengthValid(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 (!IsIdLengthValid(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 (!IsIdLengthValid(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/distributed_hardware_proxy.cpp b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp index e5ea2dd9..0b1c3910 100644 --- a/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp +++ b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp @@ -21,6 +21,7 @@ #include "anonymous_string.h" #include "av_trans_errno.h" #include "constants.h" +#include "dh_utils_tool.h" #include "dhardware_ipc_interface_code.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" @@ -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 (!IsMessageLengthValid(msg)) { return ERR_DH_FWK_SERVICE_MSG_INVALID; } @@ -419,6 +419,9 @@ int32_t DistributedHardwareProxy::NotifySourceRemoteSinkStarted(std::string &dev int32_t DistributedHardwareProxy::PauseDistributedHardware(DHType dhType, const std::string &networkId) { + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } sptr remote = Remote(); if (remote == nullptr) { DHLOGE("remote service is null"); @@ -454,6 +457,9 @@ int32_t DistributedHardwareProxy::PauseDistributedHardware(DHType dhType, const int32_t DistributedHardwareProxy::ResumeDistributedHardware(DHType dhType, const std::string &networkId) { + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } sptr remote = Remote(); if (remote == nullptr) { DHLOGE("remote service is null"); @@ -489,6 +495,9 @@ int32_t DistributedHardwareProxy::ResumeDistributedHardware(DHType dhType, const int32_t DistributedHardwareProxy::StopDistributedHardware(DHType dhType, const std::string &networkId) { + if (!IsIdLengthValid(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 9a0c5194..d51862f5 100644 --- a/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp +++ b/services/distributedhardwarefwkservice/src/accessmanager/access_manager.cpp @@ -126,14 +126,15 @@ 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!"); + if (!IsIdLengthValid(networkId)) { return; } std::string uuid = DHContext::GetInstance().GetUUIDByNetworkId(networkId); + if (!IsIdLengthValid(uuid)) { + return; + } 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 (!IsIdLengthValid(udid)) { return; } @@ -151,18 +152,15 @@ 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!"); + if (!IsIdLengthValid(networkId)) { return; } auto uuid = GetUUIDByDm(networkId); - if (uuid.size() == 0 || uuid.size() > MAX_ID_LEN) { - DHLOGE("Uuid is invalid!"); + if (!IsIdLengthValid(uuid)) { return; } auto udid = GetUDIDByDm(networkId); - if (udid.size() == 0 || udid.size() > MAX_ID_LEN) { - DHLOGE("Udid is invalid!"); + if (!IsIdLengthValid(udid)) { return; } auto ret = diff --git a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp index 2ec30f8c..145f2e27 100644 --- a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp +++ b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp @@ -110,22 +110,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; @@ -135,17 +135,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); @@ -155,17 +155,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); @@ -175,7 +175,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()); @@ -183,7 +183,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; @@ -247,6 +247,9 @@ bool ComponentLoader::CheckComponentEnable(const CompConfig &config) int32_t ComponentLoader::GetCompPathAndVersion(const std::string &jsonStr, std::map &dhtypeMap) { + if (!IsJsonLengthValid(jsonStr)) { + return ERR_DH_FWK_PARA_INVALID; + } cJSON *root = cJSON_Parse(jsonStr.c_str()); if (root == NULL) { DHLOGE("jsonStr parse failed"); @@ -331,7 +334,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; @@ -509,8 +512,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 (!IsMessageLengthValid(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 c70b14cd..ad09679f 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 (!IsIdLengthValid(networkId) || !IsIdLengthValid(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 (!IsIdLengthValid(networkId) || !IsIdLengthValid(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 d0f757b0..ef10d2db 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 (!IsIdLengthValid(networkId) || !IsIdLengthValid(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 (!IsIdLengthValid(networkId) || !IsIdLengthValid(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 17e0d07b..46bc0cd2 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_manager.cpp @@ -539,9 +539,11 @@ bool ComponentManager::InitCompSink() int32_t ComponentManager::Enable(const std::string &networkId, const std::string &uuid, const std::string &dhId, const DHType dhType) { + if (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid) || !IsIdLengthValid(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; } @@ -560,22 +562,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; @@ -594,6 +594,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 (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid) || !IsIdLengthValid(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", @@ -613,6 +616,9 @@ 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 (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid) || !IsIdLengthValid(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()); @@ -659,6 +665,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 (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid)) { + return ERR_DH_FWK_PARA_INVALID; + } DeviceInfo sourceDeviceInfo = GetLocalDeviceInfo(); std::vector> sourceCapInfos; std::string sourceDHId; @@ -705,6 +714,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 (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid)) { + return ERR_DH_FWK_PARA_INVALID; + } DeviceInfo sourceDeviceInfo = GetLocalDeviceInfo(); std::vector> sourceMetaInfos; std::string sourceDHId; @@ -737,6 +749,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 (!IsIdLengthValid(uuid) || !IsIdLengthValid(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)) { @@ -758,6 +773,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 (!IsIdLengthValid(uuid) || !IsIdLengthValid(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)) { @@ -771,6 +789,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 (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid) || !IsIdLengthValid(dhId)) { + return ERR_DH_FWK_COMPONENT_GET_ENABLE_PARAM_FAILED; + } 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; @@ -797,6 +818,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 (!IsIdLengthValid(uuid)) { + return ERR_DH_FWK_PARA_INVALID; + } CompVersion compversion; int32_t ret = VersionManager::GetInstance().GetCompVersion(uuid, dhType, compversion); if (ret != DH_FWK_SUCCESS) { @@ -814,6 +838,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 (!IsIdLengthValid(uuid)) { + return ERR_DH_FWK_PARA_INVALID; + } VersionInfo versionInfo; int32_t ret = VersionInfoManager::GetInstance()->GetVersionInfoByDeviceId(GetDeviceIdByUUID(uuid), versionInfo); if (ret != DH_FWK_SUCCESS) { @@ -836,6 +863,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 (!IsIdLengthValid(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; @@ -851,6 +881,9 @@ int32_t ComponentManager::GetVersion(const std::string &uuid, DHType dhType, std void ComponentManager::UpdateVersionCache(const std::string &uuid, const VersionInfo &versionInfo) { + if (!IsIdLengthValid(uuid)) { + return; + } DHVersion dhVersion; dhVersion.uuid = uuid; dhVersion.dhVersion = versionInfo.dhVersion; @@ -937,6 +970,9 @@ std::map ComponentManager::GetDHSinkInstance( bool ComponentManager::IsIdenticalAccount(const std::string &networkId) { + if (!IsIdLengthValid(networkId)) { + return false; + } DmAuthForm authForm = DmAuthForm::INVALID_TYPE; std::vector deviceList; DeviceManager::GetInstance().GetTrustedDeviceList(DH_FWK_PKG_NAME, "", deviceList); @@ -958,6 +994,9 @@ bool ComponentManager::IsIdenticalAccount(const std::string &networkId) void ComponentManager::UpdateBusinessState(const std::string &networkId, const std::string &dhId, BusinessState state) { + if (!IsIdLengthValid(networkId) || !IsIdLengthValid(dhId)) { + return; + } DHLOGI("UpdateBusinessState, networkId: %{public}s, dhId: %{public}s, state: %{public}" PRIu32, GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str(), (uint32_t)state); { @@ -979,6 +1018,9 @@ void ComponentManager::UpdateBusinessState(const std::string &networkId, const s BusinessState ComponentManager::QueryBusinessState(const std::string &uuid, const std::string &dhId) { + if (!IsIdLengthValid(uuid) || !IsIdLengthValid(dhId)) { + return BusinessState::UNKNOWN; + } std::lock_guard lock(bizStateMtx_); std::pair key = {uuid, dhId}; if (dhBizStates_.find(key) == dhBizStates_.end()) { @@ -990,8 +1032,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 (!IsIdLengthValid(networkId)) { return; } if (dhCommToolPtr_ == nullptr) { @@ -1038,7 +1079,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_privacy.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_privacy.cpp index a18cd4e9..18693aa3 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_privacy.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_privacy.cpp @@ -61,6 +61,9 @@ int32_t ComponentPrivacy::OnPrivaceResourceMessage(const ResourceEventType &type void ComponentPrivacy::HandlePullUpPage(const std::string &subtype, const std::string &networkId) { + if (!IsIdLengthValid(networkId)) { + return; + } cJSON *jsonArrayMsg = cJSON_CreateArray(); if (jsonArrayMsg == NULL) { DHLOGE("Failed to create cJSON arrary."); @@ -119,6 +122,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 (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("OnResourceInfoCallback start."); std::map resourceDesc = ComponentLoader::GetInstance().GetCompResourceDesc(); if (resourceDesc.find(subtype) == resourceDesc.end()) { @@ -149,6 +155,9 @@ int32_t ComponentPrivacy::OnResourceInfoCallback(const std::string &subtype, con int32_t ComponentPrivacy::StartPrivacePage(const std::string &subtype, const std::string &networkId) { + if (!IsIdLengthValid(networkId)) { + return ERR_DH_FWK_PARA_INVALID; + } DHLOGI("StartPrivacePage start."); DmDeviceInfo deviceInfo; DeviceManager::GetInstance().GetDeviceInfo(DH_FWK_PKG_NAME, networkId, deviceInfo); @@ -293,12 +302,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; } if (comPrivacyObj_ == nullptr) { @@ -319,7 +328,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; } if (comPrivacyObj_ == nullptr) { 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 d74cec39..9267f7cf 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/dh_data_sync_trigger_listener.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/dh_data_sync_trigger_listener.cpp @@ -19,6 +19,7 @@ #include "anonymous_string.h" #include "component_manager.h" +#include "dh_utils_tool.h" #include "distributed_hardware_log.h" #include "event_handler.h" @@ -39,6 +40,9 @@ DHDataSyncTriggerListener::~DHDataSyncTriggerListener() void DHDataSyncTriggerListener::OnDataSyncTrigger(const std::string &networkId) { + if (!IsIdLengthValid(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 9ce4d817..2b532fa5 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/dh_state_listener.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/dh_state_listener.cpp @@ -19,6 +19,7 @@ #include "anonymous_string.h" #include "component_manager.h" +#include "dh_utils_tool.h" #include "distributed_hardware_log.h" class ComponentManager; @@ -38,6 +39,9 @@ DHStateListener::~DHStateListener() void DHStateListener::OnStateChanged(const std::string &networkId, const std::string &dhId, const BusinessState state) { + if (!IsIdLengthValid(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 304b05ca..950ae64f 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_manager.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_manager.cpp @@ -98,6 +98,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 (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid) || !IsIdLengthValid(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()); @@ -117,6 +120,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 (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid) || !IsIdLengthValid(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 3758a320..2eb75952 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp @@ -151,6 +151,9 @@ bool DistributedHardwareManagerFactory::IsInit() int32_t DistributedHardwareManagerFactory::SendOnLineEvent(const std::string &networkId, const std::string &uuid, const std::string &udid, uint16_t deviceType) { + if (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid) || !IsIdLengthValid(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."); @@ -189,6 +192,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 (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid) || !IsIdLengthValid(udid)) { + return ERR_DH_FWK_PARA_INVALID; + } if (!isInit && !Init()) { DHLOGE("distributedHardwareMgr is null"); return ERR_DH_FWK_HARDWARE_MANAGER_INIT_FAILED; diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp index 3f2bf503..7510e1bb 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 (!IsIdLengthValid(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 (!IsIdLengthValid(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 abfd99cb..d7f62755 100644 --- a/services/distributedhardwarefwkservice/src/hidumphelper/enabled_comps_dump.cpp +++ b/services/distributedhardwarefwkservice/src/hidumphelper/enabled_comps_dump.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ +#include "dh_utils_tool.h" #include "enabled_comps_dump.h" namespace OHOS { diff --git a/services/distributedhardwarefwkservice/src/ipc/publisher_listener_proxy.cpp b/services/distributedhardwarefwkservice/src/ipc/publisher_listener_proxy.cpp index 11fdf70a..6901d123 100644 --- a/services/distributedhardwarefwkservice/src/ipc/publisher_listener_proxy.cpp +++ b/services/distributedhardwarefwkservice/src/ipc/publisher_listener_proxy.cpp @@ -14,6 +14,7 @@ */ #include "constants.h" +#include "dh_utils_tool.h" #include "distributed_hardware_log.h" #include "publisher_listener_proxy.h" @@ -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 (!IsMessageLengthValid(message)) { return; } diff --git a/services/distributedhardwarefwkservice/src/localhardwaremanager/local_hardware_manager.cpp b/services/distributedhardwarefwkservice/src/localhardwaremanager/local_hardware_manager.cpp index 099de154..f34843a2 100644 --- a/services/distributedhardwarefwkservice/src/localhardwaremanager/local_hardware_manager.cpp +++ b/services/distributedhardwarefwkservice/src/localhardwaremanager/local_hardware_manager.cpp @@ -200,8 +200,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 (!IsIdLengthValid(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 103b836c..06a15d93 100644 --- a/services/distributedhardwarefwkservice/src/localhardwaremanager/plugin_listener_impl.cpp +++ b/services/distributedhardwarefwkservice/src/localhardwaremanager/plugin_listener_impl.cpp @@ -20,6 +20,7 @@ #include "capability_info_manager.h" #include "constants.h" #include "dh_context.h" +#include "dh_utils_tool.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" #include "publisher.h" @@ -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 (!IsIdLengthValid(dhId) || !IsMessageLengthValid(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 (!IsIdLengthValid(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 3cd6c701..d49b50e8 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 (!IsMessageLengthValid(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 fd757809..55de2a01 100644 --- a/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp +++ b/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp @@ -13,8 +13,10 @@ * limitations under the License. */ -#include "constants.h" #include "publisher_item.h" + +#include "constants.h" +#include "dh_utils_tool.h" #include "distributed_hardware_log.h" namespace OHOS { @@ -64,8 +66,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 (!IsMessageLengthValid(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 7f65f5f8..3f1994ca 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 (!IsJsonLengthValid(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 172b1931..a435738e 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info_manager.cpp @@ -117,6 +117,9 @@ int32_t CapabilityInfoManager::UnInit() int32_t CapabilityInfoManager::SyncDeviceInfoFromDB(const std::string &deviceId) { + if (!IsIdLengthValid(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) { @@ -128,7 +131,7 @@ 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) { + if (dataVector.empty() || dataVector.size() > MAX_DB_RECORD_SIZE) { DHLOGE("dataVector size: %{public}zu is invalid, maybe empty or too large.", dataVector.size()); return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; } @@ -183,8 +186,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_); @@ -224,6 +227,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) { @@ -238,8 +245,7 @@ int32_t CapabilityInfoManager::AddCapabilityInMem(const std::vector MAX_ID_LEN) { - DHLOGE("DeviceId is invalid!"); + if (!IsIdLengthValid(deviceId)) { return ERR_DH_FWK_PARA_INVALID; } DHLOGI("Remove capability device info, deviceId: %{public}s", GetAnonyString(deviceId).c_str()); @@ -267,6 +273,9 @@ int32_t CapabilityInfoManager::RemoveCapabilityInfoInDB(const std::string &devic int32_t CapabilityInfoManager::RemoveCapabilityInfoByKey(const std::string &key) { + if (!IsIdLengthValid(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) { @@ -286,6 +295,9 @@ int32_t CapabilityInfoManager::RemoveCapabilityInfoByKey(const std::string &key) int32_t CapabilityInfoManager::RemoveCapabilityInfoInMem(const std::string &deviceId) { + if (!IsIdLengthValid(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();) { @@ -530,6 +542,9 @@ bool CapabilityInfoManager::IsCapabilityMatchFilter(const std::shared_ptr> &resInfos) { + if (!IsIdLengthValid(deviceId)) { + return; + } std::lock_guard lock(capInfoMgrMutex_); for (auto &capabilityInfo : globalCapInfoMap_) { if (IsCapKeyMatchDeviceId(capabilityInfo.first, deviceId)) { @@ -540,6 +555,9 @@ void CapabilityInfoManager::GetCapabilitiesByDeviceId(const std::string &deviceI bool CapabilityInfoManager::HasCapability(const std::string &deviceId, const std::string &dhId) { + if (!IsIdLengthValid(deviceId) || !IsIdLengthValid(dhId)) { + return false; + } std::lock_guard lock(capInfoMgrMutex_); std::string kvKey = GetCapabilityKey(deviceId, dhId); if (globalCapInfoMap_.find(kvKey) == globalCapInfoMap_.end()) { @@ -551,6 +569,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 (!IsIdLengthValid(deviceId) || !IsIdLengthValid(dhId)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(capInfoMgrMutex_); std::string key = GetCapabilityKey(deviceId, dhId); if (globalCapInfoMap_.find(key) == globalCapInfoMap_.end()) { @@ -563,6 +584,9 @@ int32_t CapabilityInfoManager::GetCapability(const std::string &deviceId, const int32_t CapabilityInfoManager::GetDataByKey(const std::string &key, std::shared_ptr &capInfoPtr) { + if (!IsIdLengthValid(key)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(capInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGI("dbAdapterPtr_ is null"); @@ -590,6 +614,9 @@ int32_t CapabilityInfoManager::GetDataByDHType(const DHType dhType, CapabilityIn int32_t CapabilityInfoManager::GetDataByKeyPrefix(const std::string &keyPrefix, CapabilityInfoMap &capabilityMap) { + if (!IsIdLengthValid(keyPrefix)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(capInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr is null"); @@ -600,8 +627,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) { @@ -625,11 +652,10 @@ void CapabilityInfoManager::DumpCapabilityInfos(std::vector &cap std::vector CapabilityInfoManager::GetEntriesByKeys(const std::vector &keys) { - DHLOGI("call"); - if (keys.empty()) { - DHLOGE("keys empty."); + if (!IsArrayLengthValid(keys)) { return {}; } + DHLOGI("call"); std::lock_guard lock(capInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr_ is null"); diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/capability_utils.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/capability_utils.cpp index 52d1c811..4c0b0843 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/capability_utils.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/capability_utils.cpp @@ -17,6 +17,7 @@ #include "capability_info.h" #include "constants.h" +#include "dh_utils_tool.h" #include "distributed_hardware_errno.h" namespace OHOS { @@ -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 (!IsIdLengthValid(key) || !IsIdLengthValid(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 0f6cd79f..ae38b8c7 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 (!IsIdLengthValid(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 (!IsIdLengthValid(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 (!IsIdLengthValid(key)) { + 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_); @@ -287,9 +297,9 @@ int32_t DBAdapter::GetDataByKeyPrefix(const std::string &keyPrefix, std::vector< DHLOGE("Query data by keyPrefix failed, prefix: %{public}s", GetAnonyString(keyPrefix).c_str()); return ERR_DH_FWK_RESOURCE_KV_STORAGE_OPERATION_FAIL; } - if (allEntries.size() == 0 || allEntries.size() > MAX_DB_RECORD_SIZE) { + if (allEntries.empty() || allEntries.size() > MAX_DB_RECORD_SIZE) { DHLOGE("AllEntries size: %{public}zu is invalid, maybe empty or too large.", allEntries.size()); - return ERR_DH_FWK_PARA_INVALID; + return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; } for (const auto& item : allEntries) { values.push_back(item.value.ToString()); @@ -299,8 +309,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 (!IsIdLengthValid(key) || !IsMessageLengthValid(value)) { return ERR_DH_FWK_PARA_INVALID; } std::lock_guard lock(dbAdapterMutex_); @@ -320,6 +329,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 (!IsArrayLengthValid(keys) || !IsArrayLengthValid(values)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(dbAdapterMutex_); if (kvStoragePtr_ == nullptr) { DHLOGE("kvStoragePtr_ is null"); @@ -470,6 +482,9 @@ void DBAdapter::DeleteKvStore() int32_t DBAdapter::RemoveDeviceData(const std::string &deviceId) { + if (!IsIdLengthValid(deviceId)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(dbAdapterMutex_); if (kvStoragePtr_ == nullptr) { DHLOGE("kvStoragePtr_ is null"); @@ -486,6 +501,9 @@ int32_t DBAdapter::RemoveDeviceData(const std::string &deviceId) int32_t DBAdapter::RemoveDataByKey(const std::string &key) { + if (!IsIdLengthValid(key)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(dbAdapterMutex_); if (kvStoragePtr_ == nullptr) { DHLOGE("kvStoragePtr_ is null"); @@ -503,12 +521,11 @@ int32_t DBAdapter::RemoveDataByKey(const std::string &key) std::vector DBAdapter::GetEntriesByKeys(const std::vector &keys) { + if (!IsArrayLengthValid(keys)) { + return {}; + } DHLOGI("call"); std::vector entries; - if (keys.empty()) { - DHLOGE("keys empty."); - return entries; - } { std::lock_guard lock(dbAdapterMutex_); if (kvStoragePtr_ == nullptr) { diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/local_capability_info_manager.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/local_capability_info_manager.cpp index 361320b7..0d7d01dd 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/local_capability_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/local_capability_info_manager.cpp @@ -79,6 +79,9 @@ int32_t LocalCapabilityInfoManager::UnInit() int32_t LocalCapabilityInfoManager::SyncDeviceInfoFromDB(const std::string &deviceId) { + if (!IsIdLengthValid(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) { @@ -90,8 +93,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) { @@ -107,8 +110,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_); @@ -142,6 +145,9 @@ int32_t LocalCapabilityInfoManager::AddCapability(const std::vector lock(capInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { @@ -162,6 +168,10 @@ int32_t LocalCapabilityInfoManager::RemoveCapabilityInfoByKey(const std::string void LocalCapabilityInfoManager::GetCapabilitiesByDeviceId(const std::string &deviceId, std::vector> &resInfos) { + if (!IsIdLengthValid(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)) { @@ -173,6 +183,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 (!IsIdLengthValid(deviceId) || !IsIdLengthValid(dhId)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(capInfoMgrMutex_); std::string key = GetCapabilityKey(deviceId, dhId); if (globalCapInfoMap_.find(key) == globalCapInfoMap_.end()) { @@ -185,6 +198,9 @@ int32_t LocalCapabilityInfoManager::GetCapability(const std::string &deviceId, c int32_t LocalCapabilityInfoManager::GetDataByKey(const std::string &key, std::shared_ptr &capInfoPtr) { + if (!IsIdLengthValid(key)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(capInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGI("dbAdapterPtr_ is null"); @@ -212,6 +228,9 @@ int32_t LocalCapabilityInfoManager::GetDataByDHType(const DHType dhType, Capabil int32_t LocalCapabilityInfoManager::GetDataByKeyPrefix(const std::string &keyPrefix, CapabilityInfoMap &capabilityMap) { + if (!IsKeySizeValid(keyPrefix)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(capInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr is null"); @@ -222,8 +241,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 6630e29c..fc08bc62 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 (!IsJsonLengthValid(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 2f8fadd8..ea505015 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/meta_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/meta_info_manager.cpp @@ -113,8 +113,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_); @@ -153,6 +153,9 @@ int32_t MetaInfoManager::AddMetaCapInfos(const std::vector lock(metaInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { @@ -164,7 +167,7 @@ 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) { + if (dataVector.empty() || dataVector.size() > MAX_DB_RECORD_SIZE) { DHLOGE("dataVector size: %{public}zu is invalid, maybe empty or too large.", dataVector.size()); return ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID; } @@ -219,6 +222,9 @@ int32_t MetaInfoManager::SyncRemoteMetaInfos() int32_t MetaInfoManager::GetDataByKeyPrefix(const std::string &keyPrefix, MetaCapInfoMap &metaCapMap) { + if (!IsKeySizeValid(keyPrefix)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(metaInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr is null"); @@ -229,8 +235,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) { @@ -246,6 +252,9 @@ int32_t MetaInfoManager::GetDataByKeyPrefix(const std::string &keyPrefix, MetaCa int32_t MetaInfoManager::RemoveMetaInfoByKey(const std::string &key) { + if (!IsKeySizeValid(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) { @@ -264,6 +273,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 (!IsHashSizeValid(udidHash) || !IsIdLengthValid(dhId)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(metaInfoMgrMutex_); std::string key = GetCapabilityKey(udidHash, dhId); if (globalMetaInfoMap_.find(key) == globalMetaInfoMap_.end()) { @@ -277,6 +289,9 @@ int32_t MetaInfoManager::GetMetaCapInfo(const std::string &udidHash, void MetaInfoManager::GetMetaCapInfosByUdidHash(const std::string &udidHash, std::vector> &metaCapInfos) { + if (!IsHashSizeValid(udidHash)) { + return; + } std::lock_guard lock(metaInfoMgrMutex_); for (auto &metaCapInfo : globalMetaInfoMap_) { if (IsCapKeyMatchDeviceId(metaCapInfo.first, udidHash)) { @@ -287,6 +302,9 @@ void MetaInfoManager::GetMetaCapInfosByUdidHash(const std::string &udidHash, int32_t MetaInfoManager::GetMetaCapByValue(const std::string &value, std::shared_ptr &metaCapPtr) { + if (!IsMessageLengthValid(value)) { + return ERR_DH_FWK_PARA_INVALID; + } if (metaCapPtr == nullptr) { metaCapPtr = std::make_shared(); } @@ -307,7 +325,7 @@ int32_t MetaInfoManager::GetMetaDataByDHType(const DHType dhType, MetaCapInfoMap int32_t MetaInfoManager::SyncDataByNetworkId(const std::string &networkId) { - if (networkId.size() == 0 || networkId.size() > MAX_ID_LEN) { + if (!IsIdLengthValid(networkId)) { DHLOGE("networId: %{public}s is invalid", GetAnonyString(networkId).c_str()); return ERR_DH_FWK_PARA_INVALID; } @@ -460,6 +478,9 @@ void MetaInfoManager::HandleMetaCapabilityDeleteChange(const std::vector MetaInfoManager::GetEntriesByKeys(const std::vector &keys) { + if (!IsArrayLengthValid(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 8c5bab27..8f8b2a74 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 (!IsJsonLengthValid(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 960c9d66..e15d509e 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/version_info_manager.cpp @@ -134,6 +134,9 @@ int32_t VersionInfoManager::AddVersion(const VersionInfo &versionInfo) int32_t VersionInfoManager::GetVersionInfoByDeviceId(const std::string &deviceId, VersionInfo &versionInfo) { + if (!IsIdLengthValid(deviceId)) { + return ERR_DH_FWK_PARA_INVALID; + } std::lock_guard lock(verInfoMgrMutex_); if (dbAdapterPtr_ == nullptr) { DHLOGE("dbAdapterPtr_ is null"); @@ -163,6 +166,9 @@ void VersionInfoManager::UpdateVersionCache(const VersionInfo &versionInfo) int32_t VersionInfoManager::RemoveVersionInfoByDeviceId(const std::string &deviceId) { + if (!IsIdLengthValid(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) { @@ -188,6 +194,9 @@ int32_t VersionInfoManager::RemoveVersionInfoByDeviceId(const std::string &devic int32_t VersionInfoManager::SyncVersionInfoFromDB(const std::string &deviceId) { + if (!IsIdLengthValid(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) { diff --git a/services/distributedhardwarefwkservice/src/transport/dh_comm_tool.cpp b/services/distributedhardwarefwkservice/src/transport/dh_comm_tool.cpp index 7e01803d..6a9a980e 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 90a54bd7..c4f90363 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 (!IsMessageLengthValid(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 (!IsIdLengthValid(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 (!IsIdLengthValid(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 (!IsIdLengthValid(remoteDevId)) { + return; + } std::lock_guard lock(rmtSocketIdMtx_); remoteDevSocketIds_.erase(remoteDevId); } int32_t DHTransport::StartSocket(const std::string &remoteNetworkId) { + if (!IsIdLengthValid(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 (!IsIdLengthValid(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 (!IsIdLengthValid(remoteNetworkId) || !IsMessageLengthValid(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 151070c7..3f40ca9a 100644 --- a/services/distributedhardwarefwkservice/src/transport/dh_transport_obj.cpp +++ b/services/distributedhardwarefwkservice/src/transport/dh_transport_obj.cpp @@ -23,6 +23,10 @@ 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(); @@ -43,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; @@ -62,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); @@ -69,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 3f7d6465..86aa3705 100644 --- a/services/distributedhardwarefwkservice/src/utils/dh_context.cpp +++ b/services/distributedhardwarefwkservice/src/utils/dh_context.cpp @@ -116,8 +116,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 (!IsIdLengthValid(udid) || !IsIdLengthValid(uuid) || !IsIdLengthValid(networkId)) { return; } std::unique_lock lock(onlineDevMutex_); @@ -139,6 +138,9 @@ void DHContext::AddOnlineDevice(const std::string &udid, const std::string &uuid void DHContext::RemoveOnlineDeviceIdEntryByNetworkId(const std::string &networkId) { + if (!IsIdLengthValid(networkId)) { + return; + } std::unique_lock lock(onlineDevMutex_); for (auto iter = devIdEntrySet_.begin(); iter != devIdEntrySet_.end(); iter++) { if (iter->networkId == networkId) { @@ -150,7 +152,7 @@ void DHContext::RemoveOnlineDeviceIdEntryByNetworkId(const std::string &networkI bool DHContext::IsDeviceOnline(const std::string &uuid) { - if (uuid.empty()) { + if (!IsIdLengthValid(uuid)) { return false; } std::shared_lock lock(onlineDevMutex_); @@ -172,6 +174,9 @@ size_t DHContext::GetOnlineCount() std::string DHContext::GetNetworkIdByUUID(const std::string &uuid) { + if (!IsIdLengthValid(uuid)) { + return ""; + } std::unique_lock lock(onlineDevMutex_); std::string networkId = ""; for (auto iter = devIdEntrySet_.begin(); iter != devIdEntrySet_.end(); iter++) { @@ -185,6 +190,9 @@ std::string DHContext::GetNetworkIdByUUID(const std::string &uuid) std::string DHContext::GetNetworkIdByUDID(const std::string &udid) { + if (!IsIdLengthValid(udid)) { + return ""; + } std::unique_lock lock(onlineDevMutex_); std::string networkId = ""; for (auto iter = devIdEntrySet_.begin(); iter != devIdEntrySet_.end(); iter++) { @@ -198,6 +206,9 @@ std::string DHContext::GetNetworkIdByUDID(const std::string &udid) std::string DHContext::GetUdidHashIdByUUID(const std::string &uuid) { + if (!IsIdLengthValid(uuid)) { + return ""; + } std::unique_lock lock(onlineDevMutex_); std::string udidHash = ""; for (auto iter = devIdEntrySet_.begin(); iter != devIdEntrySet_.end(); iter++) { @@ -211,6 +222,9 @@ std::string DHContext::GetUdidHashIdByUUID(const std::string &uuid) std::string DHContext::GetUUIDByNetworkId(const std::string &networkId) { + if (!IsIdLengthValid(networkId)) { + return ""; + } std::unique_lock lock(onlineDevMutex_); std::string uuid = ""; for (auto iter = devIdEntrySet_.begin(); iter != devIdEntrySet_.end(); iter++) { @@ -224,6 +238,9 @@ std::string DHContext::GetUUIDByNetworkId(const std::string &networkId) std::string DHContext::GetUDIDByNetworkId(const std::string &networkId) { + if (!IsIdLengthValid(networkId)) { + return ""; + } std::unique_lock lock(onlineDevMutex_); std::string udid = ""; for (auto iter = devIdEntrySet_.begin(); iter != devIdEntrySet_.end(); iter++) { @@ -237,6 +254,9 @@ std::string DHContext::GetUDIDByNetworkId(const std::string &networkId) std::string DHContext::GetUUIDByDeviceId(const std::string &deviceId) { + if (!IsIdLengthValid(deviceId)) { + return ""; + } std::unique_lock lock(onlineDevMutex_); std::string uuid = ""; for (auto iter = devIdEntrySet_.begin(); iter != devIdEntrySet_.end(); iter++) { @@ -250,6 +270,9 @@ std::string DHContext::GetUUIDByDeviceId(const std::string &deviceId) std::string DHContext::GetNetworkIdByDeviceId(const std::string &deviceId) { + if (!IsIdLengthValid(deviceId)) { + return ""; + } std::unique_lock lock(onlineDevMutex_); std::string networkId = ""; for (auto iter = devIdEntrySet_.begin(); iter != devIdEntrySet_.end(); iter++) { @@ -279,6 +302,9 @@ void DHContext::GetOnlineDeviceDeviceId(std::vector &deviceIdVec) std::string DHContext::GetDeviceIdByDBGetPrefix(const std::string &prefix) { + if (!IsIdLengthValid(prefix)) { + return ""; + } std::string id = ""; if (prefix.empty()) { return id; @@ -325,15 +351,14 @@ void DHContext::RegisDHFWKIsomerismListener() void DHContext::DHFWKIsomerismListener::OnMessage(const DHTopic topic, const std::string &message) { + if (!IsMessageLengthValid(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"); @@ -364,6 +389,9 @@ void DHContext::DHFWKIsomerismListener::OnMessage(const DHTopic topic, const std void DHContext::AddIsomerismConnectDev(const std::string &IsomerismDeviceId) { + if (!IsIdLengthValid(IsomerismDeviceId)) { + return; + } DHLOGI("AddIsomerismConnectDev id = %{public}s", GetAnonyString(IsomerismDeviceId).c_str()); std::shared_lock lock(connectDevMutex_); connectedDevIds_.insert(IsomerismDeviceId); @@ -371,6 +399,9 @@ void DHContext::AddIsomerismConnectDev(const std::string &IsomerismDeviceId) void DHContext::DelIsomerismConnectDev(const std::string &IsomerismDeviceId) { + if (!IsIdLengthValid(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 8cd7b034..464cd1e4 100644 --- a/services/distributedhardwarefwkservice/src/versionmanager/version_manager.cpp +++ b/services/distributedhardwarefwkservice/src/versionmanager/version_manager.cpp @@ -18,6 +18,7 @@ #include "anonymous_string.h" #include "component_loader.h" #include "dh_context.h" +#include "dh_utils_tool.h" #include "distributed_hardware_log.h" namespace OHOS { @@ -60,6 +61,9 @@ void VersionManager::ShowLocalVersion(const DHVersion &dhVersion) const int32_t VersionManager::AddDHVersion(const std::string &uuid, const DHVersion &dhVersion) { + if (!IsIdLengthValid(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 (!IsIdLengthValid(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 (!IsIdLengthValid(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 (!IsIdLengthValid(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/accessmanager/access_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/access_manager_test.cpp index 16bf3123..129a3963 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/access_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/access_manager_test.cpp @@ -83,9 +83,15 @@ HWTEST_F(AccessManagerTest, SendOnLineEvent_001, TestSize.Level1) HWTEST_F(AccessManagerTest, SendOffLineEvent_001, TestSize.Level1) { DHContext::GetInstance().devIdEntrySet_.clear(); - auto ret = DistributedHardwareManagerFactory::GetInstance().SendOffLineEvent(TEST_NETWORKID, "", TEST_UDID, + auto ret = DistributedHardwareManagerFactory::GetInstance().SendOffLineEvent("", TEST_UUID, TEST_UDID, TEST_DEV_TYPE_PAD); - EXPECT_EQ(ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_OFFLINE, ret); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + ret = DistributedHardwareManagerFactory::GetInstance().SendOffLineEvent(TEST_NETWORKID, "", TEST_UDID, + TEST_DEV_TYPE_PAD); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + ret = DistributedHardwareManagerFactory::GetInstance().SendOffLineEvent(TEST_NETWORKID, TEST_UUID, "", + TEST_DEV_TYPE_PAD); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); DHContext::GetInstance().AddOnlineDevice(TEST_UDID, TEST_UUID, TEST_NETWORKID); ret = DistributedHardwareManagerFactory::GetInstance().SendOffLineEvent(TEST_NETWORKID, TEST_UUID, TEST_UDID, diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentloader/src/component_loader_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentloader/src/component_loader_test.cpp index 42629dab..1f7e73f6 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentloader/src/component_loader_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentloader/src/component_loader_test.cpp @@ -175,7 +175,7 @@ HWTEST_F(ComponentLoaderTest, component_loader_test_017, TestSize.Level0) std::string jsonStr = ""; std::map dhtypeMap; int32_t ret = ComponentLoader::GetInstance().GetCompPathAndVersion(jsonStr, dhtypeMap); - EXPECT_EQ(ERR_DH_FWK_JSON_PARSE_FAILED, ret); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); } /** diff --git a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp index 79750cae..50e3d1c2 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/component_manager/src/component_manager_test.cpp @@ -436,7 +436,7 @@ HWTEST_F(ComponentManagerTest, Enable_002, TestSize.Level0) IDistributedHardwareSource *sourcePtr = nullptr; ComponentManager::GetInstance().compSource_.insert(std::make_pair(dhType, sourcePtr)); int32_t ret = ComponentManager::GetInstance().Enable(networkId, uuid, dhId, dhType); - EXPECT_EQ(ERR_DH_FWK_COMPONENT_GET_ENABLE_PARAM_FAILED, ret); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); } /** 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 ef388b0d..0f35b104 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 @@ -16,6 +16,7 @@ #include "mock_hardware_handler.h" #include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" namespace OHOS { namespace DistributedHardware { diff --git a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/src/resource_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/src/resource_manager_test.cpp index 605a69c5..b6f17af9 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/src/resource_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/src/resource_manager_test.cpp @@ -312,6 +312,8 @@ HWTEST_F(ResourceManagerTest, resource_manager_test_009, TestSize.Level0) HWTEST_F(ResourceManagerTest, resource_manager_test_010, TestSize.Level0) { vector> capInfos; + CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId("", capInfos); + EXPECT_EQ(capInfos.empty(), true); CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(DEV_ID_0, capInfos); EXPECT_EQ(capInfos.size(), TEST_SIZE_5); CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(DEV_ID_1, capInfos); @@ -416,6 +418,11 @@ HWTEST_F(ResourceManagerTest, resource_manager_test_015, TestSize.Level0) std::string value = ""; std::shared_ptr capPtr = nullptr; int32_t ret = GetCapabilityByValue(value, capPtr); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + + value = "invalid JSON string"; + capPtr = nullptr; + ret = GetCapabilityByValue(value, capPtr); EXPECT_EQ(ERR_DH_FWK_JSON_PARSE_FAILED, ret); } @@ -470,6 +477,10 @@ HWTEST_F(ResourceManagerTest, resource_manager_test_019, TestSize.Level0) CapabilityInfo capaInfo; std::string jsonStr = ""; int32_t ret = capaInfo.FromJsonString(jsonStr); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + + jsonStr = "invalid JSON string"; + ret = capaInfo.FromJsonString(jsonStr); EXPECT_EQ(ERR_DH_FWK_JSON_PARSE_FAILED, ret); } @@ -785,11 +796,16 @@ HWTEST_F(ResourceManagerTest, HandleCapabilityDeleteChange_001, TestSize.Level0) */ HWTEST_F(ResourceManagerTest, GetDataByKey_001, TestSize.Level0) { - std::string key; + std::string key = "000"; std::shared_ptr capInfoPtr; CapabilityInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; int32_t ret = CapabilityInfoManager::GetInstance()->GetDataByKey(key, capInfoPtr); EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); + + key = ""; + CapabilityInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; + ret = CapabilityInfoManager::GetInstance()->GetDataByKey(key, capInfoPtr); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); } /** @@ -800,11 +816,16 @@ HWTEST_F(ResourceManagerTest, GetDataByKey_001, TestSize.Level0) */ HWTEST_F(ResourceManagerTest, GetDataByKeyPrefix_001, TestSize.Level0) { - std::string keyPrefix; + std::string keyPrefix = "000"; CapabilityInfoMap capabilityMap; CapabilityInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; int32_t ret = CapabilityInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, capabilityMap); EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); + + keyPrefix = ""; + CapabilityInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; + ret = CapabilityInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, capabilityMap); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); } /** @@ -1085,7 +1106,7 @@ HWTEST_F(ResourceManagerTest, GetMetaCapByValue_001, TestSize.Level0) std::string value = ""; std::shared_ptr metaCapPtr = nullptr; auto ret = MetaInfoManager::GetInstance()->GetMetaCapByValue(value, metaCapPtr); - EXPECT_EQ(ERR_DH_FWK_JSON_PARSE_FAILED, ret); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); std::string deviceId = "deviceId_test"; std::string udidHash = "udidHash_test"; @@ -1093,6 +1114,10 @@ HWTEST_F(ResourceManagerTest, GetMetaCapByValue_001, TestSize.Level0) metaCapPtr = std::make_shared( dhId, deviceId, "devName_test", 14, DHType::CAMERA, "attrs_test", "subtype", udidHash, "1.0"); ret = MetaInfoManager::GetInstance()->GetMetaCapByValue(value, metaCapPtr); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); + + value = "invalid JSON string"; + ret = MetaInfoManager::GetInstance()->GetMetaCapByValue(value, metaCapPtr); EXPECT_EQ(ERR_DH_FWK_JSON_PARSE_FAILED, ret); } diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/src/version_info_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/src/version_info_manager_test.cpp index 145fea41..16d4987f 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/src/version_info_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/versioninfomanager/src/version_info_manager_test.cpp @@ -220,10 +220,15 @@ HWTEST_F(VersionInfoManagerTest, UpdateVersionCache_002, TestSize.Level0) */ HWTEST_F(VersionInfoManagerTest, RemoveVersionInfoByDeviceId_001, TestSize.Level0) { - std::string deviceId; + std::string deviceId = DEV_ID_1; VersionInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; int32_t ret = VersionInfoManager::GetInstance()->RemoveVersionInfoByDeviceId(deviceId); EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); + + deviceId = ""; + VersionInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; + ret = VersionInfoManager::GetInstance()->RemoveVersionInfoByDeviceId(deviceId); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); } /** @@ -251,10 +256,15 @@ HWTEST_F(VersionInfoManagerTest, RemoveVersionInfoByDeviceId_002, TestSize.Level */ HWTEST_F(VersionInfoManagerTest, SyncVersionInfoFromDB_001, TestSize.Level0) { - std::string deviceId; + std::string deviceId = DEV_ID_1; VersionInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; int32_t ret = VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(deviceId); EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); + + deviceId = ""; + VersionInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; + ret = VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(deviceId); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); } /** @@ -405,11 +415,16 @@ HWTEST_F(VersionInfoManagerTest, HandleVersionDeleteChange_001, TestSize.Level0) */ HWTEST_F(VersionInfoManagerTest, GetVersionInfoByDeviceId_001, TestSize.Level0) { - std::string deviceId; + std::string deviceId = DEV_ID_1; VersionInfo versionInfo; VersionInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; int32_t ret = VersionInfoManager::GetInstance()->GetVersionInfoByDeviceId(deviceId, versionInfo); EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret); + + deviceId = ""; + VersionInfoManager::GetInstance()->dbAdapterPtr_ = nullptr; + ret = VersionInfoManager::GetInstance()->GetVersionInfoByDeviceId(deviceId, versionInfo); + EXPECT_EQ(ERR_DH_FWK_PARA_INVALID, ret); } } // namespace DistributedHardware diff --git a/utils/include/dh_utils_tool.h b/utils/include/dh_utils_tool.h index c52e1c45..ddb7a497 100644 --- a/utils/include/dh_utils_tool.h +++ b/utils/include/dh_utils_tool.h @@ -65,6 +65,18 @@ std::string Compress(const std::string& data); std::string Decompress(const std::string& data); bool GetSysPara(const char *key, bool &value); + +bool IsIdLengthValid(const std::string &input); + +bool IsMessageLengthValid(const std::string &input); + +bool IsJsonLengthValid(const std::string &jsonStr); + +bool IsArrayLengthValid(const std::vector &array); + +bool IsKeySizeValid(const std::string &key); + +bool IsHashSizeValid(const std::string &hashValue); } // namespace DistributedHardware } // namespace OHOS #endif diff --git a/utils/src/anonymous_string.cpp b/utils/src/anonymous_string.cpp index c5491d12..8984f4a4 100644 --- a/utils/src/anonymous_string.cpp +++ b/utils/src/anonymous_string.cpp @@ -18,6 +18,10 @@ #include #include +#include "constants.h" +#include "dh_utils_tool.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" #include "securec.h" namespace OHOS { @@ -30,7 +34,7 @@ namespace { } std::string GetAnonyString(const std::string &value) { - if (value.empty()) { + if (!IsMessageLengthValid(value)) { return ""; } std::string res; diff --git a/utils/src/dh_utils_tool.cpp b/utils/src/dh_utils_tool.cpp index be684494..32603086 100644 --- a/utils/src/dh_utils_tool.cpp +++ b/utils/src/dh_utils_tool.cpp @@ -27,6 +27,7 @@ #include "openssl/sha.h" #include "parameter.h" + #include "device_manager.h" #include "dm_device_info.h" @@ -82,7 +83,7 @@ std::string GetRandomID() std::string GetUUIDByDm(const std::string &networkId) { - if (networkId.empty()) { + if (!IsIdLengthValid(networkId)) { return ""; } std::string uuid = ""; @@ -92,7 +93,7 @@ std::string GetUUIDByDm(const std::string &networkId) std::string GetUDIDByDm(const std::string &networkId) { - if (networkId.empty()) { + if (!IsIdLengthValid(networkId)) { return ""; } std::string udid = ""; @@ -102,7 +103,7 @@ std::string GetUDIDByDm(const std::string &networkId) std::string GetDeviceIdByUUID(const std::string &uuid) { - if (uuid.size() == 0 || uuid.size() > MAX_ID_LEN) { + if (!IsIdLengthValid(uuid)) { DHLOGE("uuid is invalid!"); return ""; } @@ -286,5 +287,59 @@ bool GetSysPara(const char *key, bool &value) valueStr >> std::boolalpha >> value; return true; } + +bool IsIdLengthValid(const std::string &inputID) +{ + if (inputID.empty() || inputID.length() > MAX_ID_LEN) { + DHLOGE("On parameter length error, maybe empty or beyond MAX_ID_LEN!"); + return false; + } + return true; +} + +bool IsMessageLengthValid(const std::string &inputMessage) +{ + if (inputMessage.empty() || inputMessage.length() > MAX_MESSAGE_LEN) { + DHLOGE("On parameter error, maybe empty or beyond MAX_MESSAGE_LEN!"); + return false; + } + return true; +} + +bool IsJsonLengthValid(const std::string &inputJsonStr) +{ + if (inputJsonStr.empty() || inputJsonStr.length() > MAX_JSON_SIZE) { + DHLOGE("On parameter error, maybe empty or beyond MAX_JSON_SIZE"); + return false; + } + return true; +} + +bool IsArrayLengthValid(const std::vector &inputArray) +{ + if (inputArray.empty() || inputArray.size() > MAX_ARR_SIZE) { + DHLOGE("On parameter error, maybe empty or beyond MAX_ARR_SIZE"); + return false; + } + return true; +} + +bool IsKeySizeValid(const std::string &inputKey) +{ + if (inputKey.empty() || inputKey.length() > MAX_KEY_SIZE) { + DHLOGE("On parameter error, maybe empty or beyond MAX_KEY_SIZE"); + return false; + } + return true; +} + +bool IsHashSizeValid(const std::string &inputHashValue) +{ + if (inputHashValue.empty() || inputHashValue.length() > MAX_HASH_SIZE) { + DHLOGE("On parameter error, maybe empty or beyond MAX_HASH_SIZE"); + return false; + } + return true; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/utils/src/histreamer_ability_parser.cpp b/utils/src/histreamer_ability_parser.cpp index cb29ebe5..8243a237 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; @@ -53,32 +57,38 @@ void FromJson(const cJSON *jsonObject, AudioEncoderIn &audioEncoderIn) } cJSON *sampleRateItem = nullptr; cJSON_ArrayForEach(sampleRateItem, sampleRate) { - audioEncoderIn.sample_rate.push_back((uint32_t)sampleRateItem->valuedouble); + if (sampleRateItem && sampleRateItem->type == cJSON_Number) { + audioEncoderIn.sample_rate.push_back((uint32_t)sampleRateItem->valuedouble); + } } } 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 +97,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 +120,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,51 +134,67 @@ 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()); const cJSON *layout = nullptr; cJSON_ArrayForEach(layout, channelLayoutJson) { - audioDecoderIn.channel_layout.push_back((AudioChannelLayout)layout->valuedouble); + if (layout && layout->type == cJSON_Number) { + audioDecoderIn.channel_layout.push_back((AudioChannelLayout)layout->valuedouble); + } } } 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()); cJSON *format = nullptr; cJSON_ArrayForEach(format, sampleFormatJson) { - audioDecoderOut.sample_fmt.push_back((AudioSampleFormat)format->valuedouble); + if (format && format->type == cJSON_Number) { + audioDecoderOut.sample_fmt.push_back((AudioSampleFormat)format->valuedouble); + } } } 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 +205,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,27 +219,37 @@ 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()); cJSON *pixelFmt = nullptr; cJSON_ArrayForEach(pixelFmt, videoPixelFmt) { - videoEncoderIn.pixel_fmt.push_back((VideoPixelFormat)pixelFmt->valuedouble); + if (pixelFmt && pixelFmt->type == cJSON_Number) { + videoEncoderIn.pixel_fmt.push_back((VideoPixelFormat)pixelFmt->valuedouble); + } } } 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 +257,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 +280,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,52 +294,68 @@ 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()); cJSON *fmt = nullptr; cJSON_ArrayForEach(fmt, videoBitStreamFmtJson) { - videoDecoderIn.vd_bit_stream_fmt.push_back((VideoBitStreamFormat)(fmt->valuedouble)); + if (fmt && fmt->type == cJSON_Number) { + videoDecoderIn.vd_bit_stream_fmt.push_back((VideoBitStreamFormat)(fmt->valuedouble)); + } } } 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()); cJSON *fmt = nullptr; cJSON_ArrayForEach(fmt, videoPixelFmtJson) { - videoDecoderOut.pixel_fmt.push_back((VideoPixelFormat)(fmt->valuedouble)); + if (fmt && fmt->type == cJSON_Number) { + videoDecoderOut.pixel_fmt.push_back((VideoPixelFormat)(fmt->valuedouble)); + } } } 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 +367,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 +382,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()); -- Gitee