Skip to content

Commit

Permalink
Avoid erasing an element from a loop iterating the map
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Sun <[email protected]>
  • Loading branch information
stephenxs committed Jan 31, 2025
1 parent 4810046 commit f5a8dc5
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion syncd/FlexCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ class CounterContext : public BaseCounterContext
_In_ sai_object_id_t vid)
{
SWSS_LOG_ENTER();
std::set<std::vector<StatType>> bulkContextsToBeRemoved;
bool found = false;
for (auto iter = m_bulkContexts.begin(); iter != m_bulkContexts.end(); iter++)
{
Expand All @@ -1127,7 +1128,9 @@ class CounterContext : public BaseCounterContext
ctx.object_vids.erase(vid_iter);
if (ctx.object_vids.empty())
{
m_bulkContexts.erase(iter);
// It can change the order of the map to erase an element in a loop iterating the map
// which can cause some elements to be skipped or iterated for multiple times
bulkContextsToBeRemoved.insert(iter->first);
}
else
{
Expand All @@ -1148,6 +1151,11 @@ class CounterContext : public BaseCounterContext
}
}

for (auto iter : bulkContextsToBeRemoved)
{
m_bulkContexts.erase(iter);
}

return found;
}

Expand Down

0 comments on commit f5a8dc5

Please sign in to comment.