Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V2024. Potential capacity overflow...
menu mobile close menu
Analyzing projects
Additional information
toggle menu Contents

V2024. Potential capacity overflow of a fixed-capacity container.

Dec 04 2026

This diagnostic rule was added at the users' request.

The analyzer has detected a potential overflow of a fixed-capacity container. Inside the loop, one of the container member functions (push_back, emplace_back, or insert) is called without checking whether the container size after the insertion exceeds its capacity.

Such containers include boost::container::static_vector and std::inplace_vector. Their capacity is specified at compile time via the N template parameter and cannot be increased at runtime.

The example:

void AddData(boost::container::static_vector<int, 10> &dst,
             const std::vector<int> &src)
{
  for (auto const &item : src)
  {
    dst.push_back(item); // <= V2024
  }
}

In this example, the dst capacity is 10 elements, while the number of elements in src is not checked. If src contains more than 10 elements, inserting them into the full dst container results in an exception or undefined behavior.

The fixed code:

void AddData(boost::container::static_vector<int, 10> &dst,
             const std::vector<int> &src)
{
  for (auto const &item : src)
  {
    if (dst.size() < dst.capacity())
    {
      dst.push_back(item); // ok
    }
  }
}

The dst.size() < dst.capacity() check prevents adding more elements to a full container.