GCC Newspaper
JULY 29, 2026
Date
/
Architectures
Components
Topics
News & Policy
libstdc++

libstdc++ validates stride values for layout_stride

layout_stride now validates user-provided strides to prevent negative or unrepresentable values, improving robustness.

The layout_stride class in libstdc++ now validates user-provided stride values. The conversion of these strides using __index_type_cast now includes assertions to ensure they are non-negative and fit within the index_type, preventing potential errors and undefined behavior. A new test case stride_neg.cc has been added to verify this validation.

In Details

A mdspan layout stride validation enhancement. The layout_stride::mapping constructor now uses __index_type_cast for its __strides argument, which asserts non-negativity and representability in __index_type. This guards against UB in cases where strides do not meet these criteria.

For Context
libstdc++
The GNU Standard C++ Library, a fundamental component of GCC that provides the C++ standard library implementation.
mdspan
A C++23 library feature providing a non-owning view over multi-dimensional arrays, decoupling the data from its layout and access patterns.
layout_stride
A specific layout policy for mdspan that defines multi-dimensional extents using strides, allowing for non-contiguous data arrangements.
index_type
An unsigned integer type used to represent indices and sizes within C++ standard library containers and related features like mdspan.
Filed Under: libstdc++mdspanvalidation