diff --git a/common/utils/include/constants.h b/common/utils/include/constants.h index 5e2a6bea57c34fe9807c9cdf7899ded960e30110..52bd4b77dd73ec49e3e54992a3eb7acf999f5ce7 100644 --- a/common/utils/include/constants.h +++ b/common/utils/include/constants.h @@ -36,6 +36,11 @@ namespace DistributedHardware { constexpr int32_t MODE_ENABLE = 0; constexpr int32_t MODE_DISABLE = 1; constexpr uint32_t MAX_SWITCH_SIZE = 256; + constexpr uint32_t MAX_ROUND_SIZE = 1000; + constexpr uint32_t MAX_JSON_SIZE = 40 * 1024 * 1024; + constexpr uint32_t MAX_HASH_SIZE = 64; + constexpr uint32_t MAX_KEY_SIZE = 256; + const std::string LOW_LATENCY_KEY = "identity"; const std::u16string DHMS_STUB_INTERFACE_TOKEN = u"ohos.distributedhardware.accessToken"; const std::string APP_ID = "dtbhardware_manager_service"; diff --git a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp index 27f24f351e169931478d2ce24ee3305df440d242..a37929a9053dcd29e792edbcd36aaaaeec453f24 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 c51c20d71614de7753b193b8bb2e8cae6865492d..68135245616bb038084456ace1c3c153433e4f12 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 9a0c5194cea7f6a1359a7b2b53474c94c79072ed..d51862f5676e33b0d3ca9e08bb7dd4163ab8f596 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 2ec30f8c840419326556bd3d68aedaf99b3e309d..145f2e27a4bd80ff4e5d8058d9f6c9373cbf5c33 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 c70b14cd0dced2a33eb380fce3350d8afdb627fb..ad09679fc4e90a59457e13c608e5b2fcf9dd89fa 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 d0f757b0338e78b278ab59f2e54cfedfac71eb06..ef10d2db4ce1414485a2034ea0585bd558c3db00 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 5ed86fc28eff1130bcc111601a6f40a1b1d246b8..e09ed8b69843d600d881db19acea8ebbb2d42b0a 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 9b040ea65bec3628035538e90331f3db3a90e006..8a370b3b4857bf704198c282a6a906b14467b1c1 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 d74cec39a687aa733d6f112d836e3b321d26064c..9267f7cfaad23b8fdcfe80a463d0a7af2542562a 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 9ce4d817f365d4f553017ce25af6caa33bd9c60d..2b532fa52837ff0bc3d8cfe79214c1f4489cc6e0 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 8d4729a8fe9179cb95c3386d34a6419c6e638a84..5348135a7ab18dd9f4d4858bd171330b62d10e5f 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 147f3ec7ce6c8660d8028239db69a6c2f1668c51..1e7814f4a0f32ac013997c36f186a557049f993b 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_manager_factory.cpp @@ -152,6 +152,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."); @@ -190,6 +193,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_.load() && !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 7a34192ec60f86657bebfeed6fa6df569ea1d874..7c6c246697813cca09bd675a5c561c721f388a6f 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 abfd99cb8f04efdeb1c6f224ebe12ce113438bcc..d7f62755ad8fc6c30f98bcb944b952002ced1b1c 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 96f51a175d36652d6a2d41dbbcaac95cf7a54ecc..fc281609e99cd28a8e013f95ea7e7d274db84891 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 099de15400b569dd80b85bd2c9df71c574014956..f34843a2944939ca734488791dc47aa298b435d9 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 103b836c50b73f4970f74e9cba1827624a371c03..06a15d93ef16c775636ab2a1bb742e377401058c 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 3cd6c701e4523c9a49ef5095ab782cba222a6a4c..d49b50e83ace610edabdc25c68219d1f9198299c 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 b65d5f1118f16cc3ee02b44543d11444550c77ae..be5cdff66b9c215a534a04e65f2fbf34f4965229 100644 --- a/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp +++ b/services/distributedhardwarefwkservice/src/publisher/publisher_item.cpp @@ -16,6 +16,7 @@ #include "publisher_item.h" #include "constants.h" +#include "dh_utils_tool.h" #include "distributed_hardware_log.h" namespace OHOS { @@ -65,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 7f65f5f84aa1bfcbead09a33e1d80aad7ded10ad..3f1994cacf0d939f950ad570eb8155a22be0e213 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 5cede6f2c711f9276b2da2187714db189599cdec..6f83fc1e50565e504ed0779692ac30fee9906e01 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 52d1c8113dc5b61e65eb4fb7debe77067084e224..4c0b08436bfbd8cef1ad36f50a9b2ea3567f3111 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 b0cdb170060cf2768a5f0977ca086b62c4924127..7715bef1a2638d379a9abfd30aebcfa048ffe0f5 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 361320b707958b67c036c6986e72d2b2f641ec71..0d7d01dde014da1acf8d95d78cfc65afef20dfa7 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 6630e29c80189b44d8e7bd88b2e41801030e36c7..fc08bc6207c82f93211c2ada116694afe6668037 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 3066aabed6286247112b12a7ffe6011563813774..83aa71c07539bde5fff25ca26c186a7a0198db67 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 8c5bab270e1baa2bcd586383294a2c25c9341b86..8f8b2a74d87a8b7275d544522ebf22e7c762f7a4 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 bc7e8fcde9e84021df329b25dffbd201c99af939..232a96081fac4c7bf83e0b5c9ca77148f8e0417c 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 3ff4a7697204f3cdd91212a891d9d1f750a11d98..07cabce942ee363aead2bb4abfab0df3cdc361c2 100644 --- a/services/distributedhardwarefwkservice/src/transport/dh_comm_tool.cpp +++ b/services/distributedhardwarefwkservice/src/transport/dh_comm_tool.cpp @@ -197,7 +197,7 @@ void DHCommTool::DHCommToolEventHandler::ProcessFullCapsRsp(const FullCapsRsp &c const std::shared_ptr dhCommToolPtr) { if (capsRsp.networkId.empty() || capsRsp.caps.empty()) { - DHLOGE("Receive remote caps info invalid"); + DHLOGE("Receive remote caps info invalid!"); return; } // after receive rsp, close dsoftbus channel diff --git a/services/distributedhardwarefwkservice/src/transport/dh_transport.cpp b/services/distributedhardwarefwkservice/src/transport/dh_transport.cpp index 90a54bd775da3bf1b118ac1947c9f3af9b4c4828..c4f9036394ca8cafed8c20e002e95bc249d89bdb 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 151070c7301d10beeea90040f92ea8c7253a14d7..3f40ca9add6a05ff881670a4c1efbcbea0745774 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 37f17ca697a4155a489e49b70631c947aa3e5783..1cb0d94febc2228c38370b243f4adcdc720fa2c6 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 8cd7b0340a2f7537fc07a72938fd5b09523017b2..464cd1e4449f776196bdb9dd209b81a55ac45ce9 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 96c18a4e63aef98e393084d0e626b4dba13a23c8..57917eb693369bab314b12db21db4f49d3a488e6 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 42629dabd3be287f91e5e322c6e8a4a1b2de8cde..1f7e73f615cfcb2ddc3fdb0cd8c0c6e6fe88dca0 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 79750cae08adbd41cd3f3f36a83c40af66f375cf..50e3d1c2d8236393cc23e6d3e66e5e8d0d56e8ab 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 ef388b0df6e383feefb98906917c225a9b1b2265..0f35b1044f5b7687462d796c1016252598c10bbd 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 605a69c547ac3e2735c042d49f55b873ccb8d839..b6f17af93a1932f68d18fe1ec9630da928511c3e 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 145fea41d44b5baaa73ecbd271a8bbcb5c7c2cad..16d4987f81261b501b306dd4f33c0c41d3e4d0eb 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 c52e1c4558c5652a6fc819b12f62983871f48b85..ddb7a49704d77a471b0f2911511981f9f8270774 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 c5491d1258f1e3c3907f3a8636ae7b8f5741c13b..8984f4a44fce3cb0dbdf12cb51a281a4d6cefbf1 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 e46dc20b217cb2cf6fc935ea610aec1dc72ddb1f..455e031f2120bc1f7ad58738ca8967edf0e3f0d5 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" @@ -85,7 +86,7 @@ std::string GetRandomID() std::string GetUUIDByDm(const std::string &networkId) { - if (networkId.empty()) { + if (!IsIdLengthValid(networkId)) { return ""; } std::string uuid = ""; @@ -95,7 +96,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 = ""; @@ -105,7 +106,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 ""; } @@ -289,5 +290,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 cb29ebe5a30475ce4920ccf5c864cda2d95cec0a..8243a2378969d156f28859d12a576b0ade5edf7b 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());