From 2334a04fbdc3b5eae915dec2f8bd590d50862e90 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 23 Mar 2023 08:36:11 +0200 Subject: [PATCH] Validate dimensions while reading multisize measurements. --- src/libs/vformat/vdimensions.cpp | 21 +++++++++++++++------ src/libs/vformat/vdimensions.h | 24 ++++++++++++------------ src/libs/vformat/vmeasurements.cpp | 16 +++++++++++++++- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/libs/vformat/vdimensions.cpp b/src/libs/vformat/vdimensions.cpp index 2487abd2d..babedba7f 100644 --- a/src/libs/vformat/vdimensions.cpp +++ b/src/libs/vformat/vdimensions.cpp @@ -204,10 +204,12 @@ auto VAbstartMeasurementDimension::ValidBases(qreal min, qreal max, qreal step, } //--------------------------------------------------------------------------------------------------------------------- -auto VAbstartMeasurementDimension::IsRangeValid() -> bool +auto VAbstartMeasurementDimension::IsRangeValid() const -> bool { - bool valid = m_minValue > 0 && m_maxValue > 0 && m_minValue >= RangeMin() && m_minValue <= RangeMax() - && m_minValue <= m_maxValue; + bool valid = m_minValue > 0 && m_maxValue > 0 && + (m_minValue > RangeMin() || VFuzzyComparePossibleNulls(m_minValue, RangeMin())) && + (m_maxValue < RangeMax() || VFuzzyComparePossibleNulls(m_maxValue, RangeMax())) && + (m_minValue < m_maxValue || VFuzzyComparePossibleNulls(m_minValue, m_maxValue)); if (not valid) { @@ -218,7 +220,7 @@ auto VAbstartMeasurementDimension::IsRangeValid() -> bool } //--------------------------------------------------------------------------------------------------------------------- -auto VAbstartMeasurementDimension::IsStepValid() -> bool +auto VAbstartMeasurementDimension::IsStepValid() const -> bool { bool valid = ValidSteps().indexOf(m_step) != -1; if (not valid) @@ -230,7 +232,7 @@ auto VAbstartMeasurementDimension::IsStepValid() -> bool } //--------------------------------------------------------------------------------------------------------------------- -auto VAbstartMeasurementDimension::IsBaseValid() -> bool +auto VAbstartMeasurementDimension::IsBaseValid() const -> bool { bool valid = ValidBases().indexOf(m_baseValue) != -1; if (not valid) @@ -244,7 +246,14 @@ auto VAbstartMeasurementDimension::IsBaseValid() -> bool //--------------------------------------------------------------------------------------------------------------------- auto VAbstartMeasurementDimension::IsUnitsValid() const -> bool { - return m_units == Unit::Cm || m_units == Unit::Mm || m_units == Unit::Inch; + bool valid = (m_units == Unit::Cm || m_units == Unit::Mm || m_units == Unit::Inch); + + if (not valid) + { + m_error = QCoreApplication::translate("VAbstartMeasurementDimension", "Units are invalid"); + } + + return valid; } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vformat/vdimensions.h b/src/libs/vformat/vdimensions.h index faf81f528..7bb23b7ee 100644 --- a/src/libs/vformat/vdimensions.h +++ b/src/libs/vformat/vdimensions.h @@ -107,23 +107,23 @@ public: void SetCustomName(const QString &newCustomName); protected: - auto IsRangeValid() -> bool; - auto IsStepValid() -> bool; - auto IsBaseValid() -> bool; + auto IsRangeValid() const -> bool; + auto IsStepValid() const -> bool; + auto IsBaseValid() const -> bool; auto IsUnitsValid() const -> bool; private: Q_DISABLE_COPY_MOVE(VAbstartMeasurementDimension) // NOLINT - Unit m_units{Unit::Cm}; - qreal m_minValue{0}; - qreal m_maxValue{0}; - qreal m_step{-1}; - qreal m_baseValue{0}; - QString m_error{}; - DimesionLabels m_labels{}; - bool m_measurement{true}; - QString m_customName{}; + Unit m_units{Unit::Cm}; + qreal m_minValue{0}; + qreal m_maxValue{0}; + qreal m_step{-1}; + qreal m_baseValue{0}; + mutable QString m_error{}; + DimesionLabels m_labels{}; + bool m_measurement{true}; + QString m_customName{}; }; //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vformat/vmeasurements.cpp b/src/libs/vformat/vmeasurements.cpp index ddb917a70..bf632fd4a 100644 --- a/src/libs/vformat/vmeasurements.cpp +++ b/src/libs/vformat/vmeasurements.cpp @@ -41,6 +41,7 @@ #include #include "../ifc/exception/vexceptionemptyparameter.h" +#include "../ifc/exception/vexceptionobjecterror.h" #include "../ifc/xml/vvitconverter.h" #include "../ifc/xml/vvstconverter.h" #include "../ifc/ifcdef.h" @@ -153,7 +154,12 @@ void VMeasurements::setXMLContent(const QString &fileName) VDomDocument::setXMLContent(fileName); type = ReadType(); m_units = ReadUnits(); - m_dimensions = ReadDimensions(); + + if (type == MeasurementsType::Multisize && + VVSTConverter::MeasurementMaxVer == GetFormatVersion(GetFormatVersionStr())) + { + m_dimensions = ReadDimensions(); + } } //--------------------------------------------------------------------------------------------------------------------- @@ -1427,6 +1433,14 @@ auto VMeasurements::ReadDimensions() const -> VDimensions dimension->SetBodyMeasurement(GetParametrBool(dom, AttrMeasurement, trueStr)); dimension->SetCustomName(GetParametrEmptyString(dom, AttrCustomName)); dimension->SetLabels(ReadDimensionLabels(dom)); + + if (not dimension->IsValid()) + { + VExceptionObjectError excep(tr("Dimension is not valid"), dom); + excep.AddMoreInformation(dimension->Error()); + throw excep; + } + dimensions.insert(type, dimension); }