From 6633e88bbac10192b0894be5de9ee7177467e0bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-No=C3=ABl=20Grad?= Date: Wed, 16 Jun 2021 17:32:42 +0200 Subject: [PATCH] core: Sanitize script interface shared pointers --- src/core/accumulators.cpp | 8 ++++++++ src/core/constraints/Constraints.hpp | 5 +++++ src/core/grid_based_algorithms/lb_boundaries.cpp | 4 ++++ 3 files changed, 17 insertions(+) diff --git a/src/core/accumulators.cpp b/src/core/accumulators.cpp index acc5a31e2a1..5064327b24e 100644 --- a/src/core/accumulators.cpp +++ b/src/core/accumulators.cpp @@ -62,9 +62,17 @@ int auto_update_next_update() { void auto_update_add(AccumulatorBase *acc) { assert(acc); + assert(std::find_if(auto_update_accumulators.begin(), + auto_update_accumulators.end(), [acc](auto const &a) { + return a.acc == acc; + }) == auto_update_accumulators.end()); auto_update_accumulators.emplace_back(acc); } void auto_update_remove(AccumulatorBase *acc) { + assert(std::find_if(auto_update_accumulators.begin(), + auto_update_accumulators.end(), [acc](auto const &a) { + return a.acc == acc; + }) != auto_update_accumulators.end()); auto_update_accumulators.erase( boost::remove_if( auto_update_accumulators, diff --git a/src/core/constraints/Constraints.hpp b/src/core/constraints/Constraints.hpp index 250aa017d13..ebe1294a24e 100644 --- a/src/core/constraints/Constraints.hpp +++ b/src/core/constraints/Constraints.hpp @@ -23,6 +23,7 @@ #include "grid.hpp" #include "statistics.hpp" +#include #include #include @@ -51,11 +52,15 @@ template class Constraints { if (not c->fits_in_box(box_geo.length())) { throw std::runtime_error("Constraint not compatible with box size."); } + assert(std::find(m_constraints.begin(), m_constraints.end(), c) == + m_constraints.end()); m_constraints.emplace_back(c); on_constraint_change(); } void remove(std::shared_ptr const &c) { + assert(std::find(m_constraints.begin(), m_constraints.end(), c) != + m_constraints.end()); m_constraints.erase( std::remove(m_constraints.begin(), m_constraints.end(), c), m_constraints.end()); diff --git a/src/core/grid_based_algorithms/lb_boundaries.cpp b/src/core/grid_based_algorithms/lb_boundaries.cpp index 841eb24bab8..d6ab424a51e 100644 --- a/src/core/grid_based_algorithms/lb_boundaries.cpp +++ b/src/core/grid_based_algorithms/lb_boundaries.cpp @@ -59,12 +59,16 @@ std::vector> lbboundaries; #if defined(LB_BOUNDARIES) || defined(LB_BOUNDARIES_GPU) void add(const std::shared_ptr &b) { + assert(std::find(lbboundaries.begin(), lbboundaries.end(), b) == + lbboundaries.end()); lbboundaries.emplace_back(b); on_lbboundary_change(); } void remove(const std::shared_ptr &b) { + assert(std::find(lbboundaries.begin(), lbboundaries.end(), b) != + lbboundaries.end()); lbboundaries.erase(std::remove(lbboundaries.begin(), lbboundaries.end(), b), lbboundaries.end());