False negative, C++ erase in range-for over the container being erased from

C++ code:

#include <map>
#include <string>
#include <string_view>

void f(std::map<std::string, int>& m, std::string_view needle)
{
    for (const auto& [name, value] : m)
    {
        if (name == needle) m.erase(name);
    }
}

m.erase(name) invalidates the (hidden) iterator from the range-for loop, making the whole loop UB and likely to crash. Sonar does not report this bug.

Hello @rollbear,

I’m happy to see you on our community forum! I came across many of your C++ talks that I found really helpful :pray:

You are right about the false negative.
We have a rule specification to detect iterator invalidation in general, but it has not been implemented yet. I think it is easy to start small and detect the common pattern like your example. Here is a Jira ticket to track progress on the topic.

Thanks for the feedback,

2 Likes