From ee21985e254fe4c9370f32f065a1c63f475cbf77 Mon Sep 17 00:00:00 2001 From: Abdullah Tahiri Date: Sun, 28 Jun 2020 19:09:29 +0200 Subject: [PATCH] Sketcher: PropertyConstraintList input data validation ====================================================== PropertyConstraintList is provided with the ability to check its constraints indices against a minimum and a maximum, and set an invalidindex status. In this status, the getValues returns an emptylist, as in the case with invalid geometry types. --- .../Sketcher/App/PropertyConstraintList.cpp | 36 +++++++++++++++++++ src/Mod/Sketcher/App/PropertyConstraintList.h | 7 ++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/Mod/Sketcher/App/PropertyConstraintList.cpp b/src/Mod/Sketcher/App/PropertyConstraintList.cpp index f886b33e8a..aa1e07127e 100644 --- a/src/Mod/Sketcher/App/PropertyConstraintList.cpp +++ b/src/Mod/Sketcher/App/PropertyConstraintList.cpp @@ -61,6 +61,7 @@ PropertyConstraintList::PropertyConstraintList() : validGeometryKeys(0) , invalidGeometry(true) , restoreFromTransaction(false) + , invalidIndices(false) { } @@ -400,6 +401,41 @@ bool PropertyConstraintList::checkGeometry(const std::vector & return invalidGeometry; } +bool PropertyConstraintList::checkConstraintIndices(int geomax, int geomin) +{ + int mininternalgeoid = std::numeric_limits::max(); + int maxinternalgeoid = Constraint::GeoUndef; + + auto cmin = [] (int previousmin, int cindex) { + if( cindex == Constraint::GeoUndef ) + return previousmin; + + return ( cindex < previousmin )? cindex : previousmin; + }; + + auto cmax = [] (int previousmax, int cindex) { + return ( cindex > previousmax )? cindex : previousmax; + }; + + for (const auto &v : _lValueList) { + + mininternalgeoid = cmin(mininternalgeoid, v->First); + mininternalgeoid = cmin(mininternalgeoid, v->Second); + mininternalgeoid = cmin(mininternalgeoid, v->Third); + + maxinternalgeoid = cmax(maxinternalgeoid, v->First); + maxinternalgeoid = cmax(maxinternalgeoid, v->Second); + maxinternalgeoid = cmax(maxinternalgeoid, v->Third); + } + + if(maxinternalgeoid > geomax || mininternalgeoid < geomin) + invalidIndices = true; + else + invalidIndices = false; + + return invalidIndices; +} + /*! * \brief PropertyConstraintList::scanGeometry tests if the supplied geometry * is the same (all elements are of the same type as they used to be). diff --git a/src/Mod/Sketcher/App/PropertyConstraintList.h b/src/Mod/Sketcher/App/PropertyConstraintList.h index 1e16aca60d..10c6c0bee2 100644 --- a/src/Mod/Sketcher/App/PropertyConstraintList.h +++ b/src/Mod/Sketcher/App/PropertyConstraintList.h @@ -99,11 +99,11 @@ public: returns null. This must be checked by the caller. */ const Constraint *operator[] (const int idx) const { - return invalidGeometry ? 0 : _lValueList[idx]; + return (invalidGeometry || invalidIndices) ? 0 : _lValueList[idx]; } const std::vector &getValues(void) const { - return invalidGeometry ? _emptyValueList : _lValueList; + return (invalidGeometry || invalidIndices) ? _emptyValueList : _lValueList; } const std::vector &getValuesForce(void) const {//to suppress check for invalid geometry, to be used for sketch repairing. return _lValueList; @@ -124,6 +124,8 @@ public: bool checkGeometry(const std::vector &GeoList); bool scanGeometry(const std::vector &GeoList) const; + bool checkConstraintIndices(int geomax, int geomin); + /// Return status of geometry for better error reporting bool hasInvalidGeometry() const { return invalidGeometry; } @@ -162,6 +164,7 @@ private: std::vector validGeometryKeys; bool invalidGeometry; bool restoreFromTransaction; + bool invalidIndices; void applyValues(std::vector&&); void applyValidGeometryKeys(const std::vector &keys);