diff --git a/foundations/ability/utils/include/updateservice_json_utils.h b/foundations/ability/utils/include/updateservice_json_utils.h index d9bb0eb07d7666fd565663c0a9b6aa5270ba8a50..87a388fc60476832faa3c30216d439e4bcde0c19 100644 --- a/foundations/ability/utils/include/updateservice_json_utils.h +++ b/foundations/ability/utils/include/updateservice_json_utils.h @@ -47,10 +47,9 @@ public: return CAST_INT(JsonParseError::MISSING_PROP); } - if (!CheckBaseType(item, value)) { + if (!CheckTypeAndAsign(item, value)) { return CAST_INT(JsonParseError::TYPE_ERROR); } - GetValue(item, value); return CAST_INT(JsonParseError::ERR_OK); } @@ -61,8 +60,7 @@ public: return nullptr; } - if (!cJSON_IsObject(object)) - { + if (!cJSON_IsObject(object)) { cJSON_Delete(object); return nullptr; } @@ -72,92 +70,90 @@ public: private: - // 检查基本类型 - template static bool CheckBaseType(cJSON *jsonObject, T &value) + static bool IsInteger(double d) { - if (!jsonObject) { - return false; - } + return std::floor(d) == d; + } - if constexpr (std::is_same_v) { - return cJSON_IsString(jsonObject); - } else if constexpr (std::is_same_v || std::is_same_v || - std::is_same_v) { - return cJSON_IsNumber(jsonObject); - } else if constexpr (std::is_same_v || std::is_same_v) { - return cJSON_IsNumber(jsonObject) && jsonObject->valuedouble >=0; - } else if constexpr (std::is_same_v) { - return (jsonObject->type == cJSON_True || jsonObject->type == cJSON_False); - } else { + // 判断int32_t ,int64_t, uint32_t ,uint64_t 是否合法整数 + template static bool CheckInteger(cJSON *jsonObj, T &value) + { + if (!cJSON_IsNumber(jsonObj)) { return false; } - } - template - static bool CheckArrayType(cJSON *jsonObject, std::vector &value) - { - return cJSON_IsArray(jsonObject); - } + double objValue = jsonObj->valuedouble; + if (!IsInteger(objValue)) { + return false; + } - static void GetValueVecString(const cJSON *jsonArray, std::vector &value) - { - if (!jsonArray || !cJSON_IsArray(jsonArray)) { - return; + if (std::is_same_v) { + if (objValue < std::numeric_limits::min() || objValue > std::numeric_limits::max()) { + return false; + } } - const cJSON *element = nullptr; - cJSON_ArrayForEach(element, jsonArray) { - if (cJSON_IsString(element) && element->valuestring != nullptr) { - value.push_back(element->valuestring); + if (std::is_same_v) { + if (objValue < std::numeric_limits::min() || objValue > std::numeric_limits::max()) { + return false; } } - } - static void GetValueVecInt(const cJSON *jsonArray, std::vector &value) - { - if (!jsonArray || !cJSON_IsArray(jsonArray)) { - return; + if (std::is_same_v) { + if (objValue < 0 || objValue > std::numeric_limits::max()) { + return false; + } } - const cJSON *element = nullptr; - cJSON_ArrayForEach(element, jsonArray) { - if (cJSON_IsNumber(element)) { - value.push_back(element->valueint); + if (std::is_same_v) { + if (objValue < 0 || objValue > std::numeric_limits::max()) { + return false; } } + value = objValue; + return true; } - template - static void GetValue(cJSON *item, T &value) + // 检查基本类型 + template static bool CheckTypeAndAsign(cJSON *jsonObject, T &value) { - if (!item) { - return; + if constexpr (std::is_same_v) { + if (cJSON_IsString(jsonObject)) { + value = jsonObject->valuestring; + return true; + } + return false; } - if constexpr (std::is_integral_v && !std::is_same_v) - { - if (cJSON_IsNumber(item)) { - (sizeof(T) <= sizeof(int)) ? value = static_cast(item->valueint) : - value = static_cast(item->valuedouble); + if constexpr (std::is_same_v) { + if (jsonObject->type != cJSON_True && jsonObject->type != cJSON_False) { + return false; } - } else if constexpr (std::is_floating_point_v) { - if (cJSON_IsNumber(item)) { - value = static_cast(item->valuedouble); + value = cJSON_IsTrue(jsonObject) ? true : false; + return true; + } + + if constexpr (std::is_same_v || std::is_same_v) { + return CheckInteger(jsonObject, value); + } + + if constexpr (std::is_same_v) { + if (!cJSON_IsNumber(jsonObject)) { + return false; } - } else if constexpr (std::is_same_v) { - value = cJSON_IsTrue(item) ? true : false; - } else if constexpr (std::is_same_v) { - if (cJSON_IsString(item)) { - value = item->valuestring; + double dbValue = jsonObject->valuedouble; + if (!std::isfinite(dbValue)) { // 排除 NaN INF -INF + return false; } - } else if constexpr (std::is_same_v>) { - GetValueVecInt(item, value); - } else if constexpr (std::is_same_v>) { - GetValueVecString(item, value); - } else { - return; + value = dbValue; } + + if constexpr (std::is_same_v || std::is_same_v) { + return CheckInteger(jsonObject, value); + } + + return false; } }; } // namespace OHOS::UpdateService -#endif // UPDATESERVICE_JSON_UTILS_H +#endif // UPDATESERVICE_JSON_UTILS_H \ No newline at end of file diff --git a/services/core/ability/model/include/device_info.h b/services/core/ability/model/include/device_info.h index eabf71ef873d1a10a8ad46fe67b67be22b10db3a..5ba0908fbbcbc8b56cd6d2c48b1b378b931b720c 100644 --- a/services/core/ability/model/include/device_info.h +++ b/services/core/ability/model/include/device_info.h @@ -29,8 +29,7 @@ public: friend void ToJson(cJSON *jsonObject, const DeviceInfo &deviceInfo, bool isPrint) { - if (jsonObject == nullptr) - { + if (jsonObject == nullptr) { return; } diff --git a/services/firmware/utils/src/firmware_check_analyze_utils.cpp b/services/firmware/utils/src/firmware_check_analyze_utils.cpp index 7f0fc84534e773ef1c8d741737feb62ad9d2a29d..e6993ffee6663a0a296ce516b32d0025ee2f7b78 100644 --- a/services/firmware/utils/src/firmware_check_analyze_utils.cpp +++ b/services/firmware/utils/src/firmware_check_analyze_utils.cpp @@ -78,10 +78,8 @@ int32_t FirmwareCheckAnalyzeUtils::AnalyzeBlVersionCheckResults(cJSON *root, BlC int32_t ret = CAST_INT(JsonParseError::ERR_OK); int32_t status = CAST_INT(CheckResultStatus::STATUS_SYSTEM_ERROR); UpdateServiceJsonUtils::GetValueAndSetTo(root, "searchStatus", status); - if (status == CAST_INT(CheckResultStatus::STATUS_NEW_VERSION_AVAILABLE)) - { - for (int i = 0; i < cJSON_GetArraySize(itemCheckResults); i++) - { + if (status == CAST_INT(CheckResultStatus::STATUS_NEW_VERSION_AVAILABLE)) { + for (int i = 0; i < cJSON_GetArraySize(itemCheckResults); i++) { auto item = cJSON_GetArrayItem(itemCheckResults, i); BlVersionCheckResult checkResult; ret += UpdateServiceJsonUtils::GetValueAndSetTo(item, "descriptPackageId", checkResult.descriptPackageId);