From e444092711f132f74a0a76adab9cde50e16ff3fd Mon Sep 17 00:00:00 2001 From: Charles Beattie Date: Fri, 13 Sep 2024 16:08:18 +0100 Subject: [PATCH] and_condition_impl::combine_equal - Remove UB container modification. (#63) * and_condition_impl::combine_equal - Remove UB container modification. The container is modified while iterating it. Switched to indexed based iteration to avoid UB. * Update condition.cpp Sorry missed this line. --- src/condition.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/condition.cpp b/src/condition.cpp index 9bcf235..81e30be 100644 --- a/src/condition.cpp +++ b/src/condition.cpp @@ -115,27 +115,33 @@ namespace detail auto first = subs.front(); auto &fc = first->m_sub; - for (auto c : fc) + for (size_t fc_i = 0; fc_i < fc.size();) { - if (not found_in_range(c, subs.begin() + 1, subs.end())) + auto c = fc[fc_i]; + if (not found_in_range(c, subs.begin() + 1, subs.end())) { + ++fc_i; continue; + } if (and_result == nullptr) and_result = new and_condition_impl(); and_result->m_sub.push_back(c); - fc.erase(remove(fc.begin(), fc.end(), c), fc.end()); + fc.erase(fc.begin() + fc_i); for (auto sub : subs) { auto &ssub = sub->m_sub; - for (auto sc : ssub) + for (size_t ssub_i = 0; ssub_i < ssub.size();) { - if (not sc->equals(c)) + auto sc = ssub[ssub_i]; + if (not sc->equals(c)) { + ++ssub_i; continue; + } - ssub.erase(remove(ssub.begin(), ssub.end(), sc), ssub.end()); + ssub.erase(ssub.begin() + ssub_i); delete sc; break; }