diff options
author | Andrii Kalinich (GitHub) <AKalinich@luxoft.com> | 2021-08-25 09:47:45 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-25 09:47:45 -0400 |
commit | e75bd4f3131b63e7e7f805bc36eb70c758c37640 (patch) | |
tree | 3c2c54b09110d69858cd8a784105109c71019888 /src/components | |
parent | 90879643717a1f8fef5061345e5e9b279329619f (diff) | |
download | sdl_core-e75bd4f3131b63e7e7f805bc36eb70c758c37640.tar.gz |
Fix SDL crash on validation of numeric schemas with type overflow (#3750)
* Added handling of cases with type overflow
* Update generator logic and schema validation
* Add validation info
Co-authored-by: Jacob Keeler <jacob.keeler@livioradio.com>
Diffstat (limited to 'src/components')
3 files changed, 26 insertions, 16 deletions
diff --git a/src/components/smart_objects/include/smart_objects/number_schema_item.h b/src/components/smart_objects/include/smart_objects/number_schema_item.h index 25b7684829..5fa9597db7 100644 --- a/src/components/smart_objects/include/smart_objects/number_schema_item.h +++ b/src/components/smart_objects/include/smart_objects/number_schema_item.h @@ -135,6 +135,7 @@ bool TNumberSchemaItem<NumberType>::isValidNumberType(SmartType type) { typeid(int32_t), typeid(uint32_t), typeid(int64_t), + typeid(uint64_t), typeid(double))) { return true; } @@ -157,11 +158,26 @@ errors::eType TNumberSchemaItem<NumberType>::validate( report->set_validation_info(validation_info); return errors::INVALID_VALUE; } + NumberType value(0); if (typeid(int32_t) == typeid(value)) { - value = utils::SafeStaticCast<int64_t, int32_t>(Object.asInt()); + if (Object.asInt() < std::numeric_limits<int32_t>::min() || + Object.asInt() > std::numeric_limits<int32_t>::max()) { + const std::string validation_info = + "Value " + Object.asString() + " out of int32 range"; + report->set_validation_info(validation_info); + return errors::OUT_OF_RANGE; + } + value = Object.asInt(); } else if (typeid(uint32_t) == typeid(value)) { - value = utils::SafeStaticCast<uint64_t, uint32_t>(Object.asUInt()); + if (Object.asInt() < std::numeric_limits<uint32_t>::min() || + Object.asInt() > std::numeric_limits<uint32_t>::max()) { + const std::string validation_info = + "Value " + Object.asString() + " out of uint32 range"; + report->set_validation_info(validation_info); + return errors::OUT_OF_RANGE; + } + value = Object.asUInt(); } else if (typeid(double) == typeid(value)) { value = Object.asDouble(); } else if (typeid(int64_t) == typeid(value)) { @@ -231,6 +247,9 @@ template <> SmartType TNumberSchemaItem<int64_t>::getSmartType() const; template <> +SmartType TNumberSchemaItem<uint64_t>::getSmartType() const; + +template <> SmartType TNumberSchemaItem<double>::getSmartType() const; } // namespace ns_smart_objects diff --git a/src/components/smart_objects/src/number_schema_item.cc b/src/components/smart_objects/src/number_schema_item.cc index c383f6ecca..1874ff30a0 100644 --- a/src/components/smart_objects/src/number_schema_item.cc +++ b/src/components/smart_objects/src/number_schema_item.cc @@ -50,6 +50,11 @@ SmartType TNumberSchemaItem<int64_t>::getSmartType() const { } template <> +SmartType TNumberSchemaItem<uint64_t>::getSmartType() const { + return SmartType_UInteger; +} + +template <> SmartType TNumberSchemaItem<double>::getSmartType() const { return SmartType_Double; } diff --git a/src/components/utils/include/utils/convert_utils.h b/src/components/utils/include/utils/convert_utils.h index ba23b620dc..e51cf8cd57 100644 --- a/src/components/utils/include/utils/convert_utils.h +++ b/src/components/utils/include/utils/convert_utils.h @@ -70,20 +70,6 @@ unsigned long long int ConvertUInt64ToLongLongUInt(const uint64_t value); uint64_t ConvertLongLongUIntToUInt64(const unsigned long long int value); /** - * @brief Convert one number value to another type value - * @param value to be converted - * @return conversion result - */ -template <typename InputType, typename OutputType> -OutputType SafeStaticCast(const InputType value) { - DCHECK_OR_RETURN(value >= std::numeric_limits<OutputType>::min(), - std::numeric_limits<OutputType>::min()); - DCHECK_OR_RETURN(value <= std::numeric_limits<OutputType>::max(), - std::numeric_limits<OutputType>::max()); - return static_cast<OutputType>(value); -} - -/** * @brief Convert binary data to a string value * @param data raw binary data * @param data_size string length. Required to check whether the data is a |