From 9ad441dfbe82e291fd0bd21b9eee09578e082245 Mon Sep 17 00:00:00 2001 From: Tristan Penman Date: Fri, 8 Apr 2016 08:02:29 +1000 Subject: [PATCH] Remove potentially risky overloads of addConstraint function and make existing impl more exception safe --- include/valijson/subschema.hpp | 42 +++++++--------------------------- 1 file changed, 8 insertions(+), 34 deletions(-) diff --git a/include/valijson/subschema.hpp b/include/valijson/subschema.hpp index 8ac4a0c..7d88f03 100644 --- a/include/valijson/subschema.hpp +++ b/include/valijson/subschema.hpp @@ -91,41 +91,15 @@ public: */ void addConstraint(const Constraint &constraint) { - constraints.push_back(constraint.clone(allocFn, freeFn)); - } - - /** - * @brief move a constraint to this sub-schema - * - * The constraint will be added to the list of constraints for this - * Subschema. - * - * @param constraint pointer to the constraint to be added. Ownership - * assumed. - */ - void addConstraint(const Constraint *constraint) - { - constraints.push_back(constraint); - } - - //#if _cplusplus >=201103L - #if 0 - /** - * @brief Add a constraint to this sub-schema - * - * The constraint will be copied before being added to the list of - * constraints for this Subschema. Note that constraints will be copied - * only as deep as references to other Subschemas - e.g. copies of - * constraints that refer to sub-schemas, will continue to refer to the - * same Subschema instances. - * - * @param constraint Reference to the constraint to copy - */ - void addConstraint(std::unique_ptrconstraint) - { - constraints.push_back(constraint.release()); + Constraint *newConstraint = constraint.clone(allocFn, freeFn); + try { + constraints.push_back(newConstraint); + } catch (...) { + newConstraint->~Constraint(); + freeFn(newConstraint); + throw; } - #endif + } /** * @brief Invoke a function on each child Constraint