﻿# V2024\. Potential capacity overflow of a fixed\-capacity container\.

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`](https://www.boost.org/doc/libs/latest/doc/html/doxygen/boost_container_header_reference/classboost_1_1container_1_1static__vector.html) and [`std::inplace_vector`](https://en.cppreference.com/w/cpp/container/inplace_vector)\. Their capacity is specified at compile time via the `N` template parameter and cannot be increased at runtime\.

The example:

```cpp
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:

```cpp
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\.