GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
libstdc++

Fix inplace_vector capacity checks for oversized ranges

Corrects `inplace_vector` to accurately check capacity when dealing with oversized input ranges.

This commit fixes a bug in std::inplace_vector where capacity checks could incorrectly succeed for oversized ranges. Previously, the size of the range was unconditionally cast to size_type, potentially truncating values and leading to the acceptance of ranges larger than the vector’s capacity. The fix introduces _S_distance_if_fits and _S_check_size to perform proper bounds checking, ensuring that ranges exceeding capacity are rejected.

In Details

This patch refactors the capacity checking mechanism in std::inplace_vector. It replaces the unconditional std::distance call with _S_distance_if_fits, which integrates bounds checking by stopping iteration at capacity() - size(). It also introduces _S_check_size templated on integer types that verifies if a range size fits in size_type before casting. Runtime checks for input iterators are replaced with if constexpr for better compile-time evaluation. A new concept, __any_sizeable_iterator, is defined to mirror __any_input_iterator.

For Context
inplace_vector
A fixed-size contiguous sequence container that stores its elements directly within its own storage, typically used when the maximum size is known at compile time.
size_type
An unsigned integer type used by standard library containers to represent sizes and counts of elements.
range
A pair of iterators defining a sequence of elements.
iterator
An object that enables traversal through a container and access to its elements.
if constexpr
A C++17 feature that allows conditional compilation of code blocks based on compile-time conditions, improving efficiency by discarding unused code paths.
concept
A C++20 feature that specifies the requirements on template arguments, improving compile-time error messages and code clarity.
Filed Under: libstdc++bugfixstl