Skip to content

Commit

Permalink
and_condition_impl::combine_equal - Remove UB container modification. (
Browse files Browse the repository at this point in the history
…#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.
  • Loading branch information
charlesbeattie authored Sep 13, 2024
1 parent a96b1e0 commit e444092
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit e444092

Please sign in to comment.