[PATCH v1] libstdc++: Fix bug in default ctor of extents.
Tomasz Kaminski
tkaminsk@redhat.com
Mon May 26 07:48:51 GMT 2025
On Sat, May 24, 2025 at 1:29 PM Luc Grosheintz <luc.grosheintz@gmail.com>
wrote:
> The array that stores the dynamic extents used to be default
> initialized. The standard requires value intialization. This
> commit fixes the bug and adds a test.
>
> libstdc++-v3/ChangeLog:
>
> * include/std/mdspan: Value initialize the array storing the
> dynamic extents.
> * testsuite/23_containers/mdspan/extents/ctor_default.cc: New
> test.
>
> Signed-off-by: Luc Grosheintz <luc.grosheintz@gmail.com>
> ---
>
LGTM, thanks for noticing and fixing it.
We also need approval from the maintainer.
> libstdc++-v3/include/std/mdspan | 2 +-
> .../mdspan/extents/ctor_default.cc | 41 +++++++++++++++++++
> 2 files changed, 42 insertions(+), 1 deletion(-)
> create mode 100644
> libstdc++-v3/testsuite/23_containers/mdspan/extents/ctor_default.cc
>
> diff --git a/libstdc++-v3/include/std/mdspan
> b/libstdc++-v3/include/std/mdspan
> index 47cfa405e44..bcf2fa60fea 100644
> --- a/libstdc++-v3/include/std/mdspan
> +++ b/libstdc++-v3/include/std/mdspan
> @@ -146,7 +146,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>
> private:
> using _S_storage = __array_traits<_IndexType,
> _S_rank_dynamic>::_Type;
> - [[no_unique_address]] _S_storage _M_dynamic_extents;
> + [[no_unique_address]] _S_storage _M_dynamic_extents{};
>
We know that these are integral types, so we can use {}.
> };
>
> template<typename _OIndexType, typename _SIndexType>
> diff --git
> a/libstdc++-v3/testsuite/23_containers/mdspan/extents/ctor_default.cc
> b/libstdc++-v3/testsuite/23_containers/mdspan/extents/ctor_default.cc
> new file mode 100644
> index 00000000000..eec300f6896
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/23_containers/mdspan/extents/ctor_default.cc
> @@ -0,0 +1,41 @@
> +// { dg-do run { target c++23 } }
> +#include <mdspan>
> +
> +#include <cstdint>
> +#include <testsuite_hooks.h>
> +
> +constexpr auto dyn = std::dynamic_extent;
> +
> +template<typename Extents>
> + constexpr void
> + test_default_ctor()
> + {
> + Extents exts;
> + for(size_t i = 0; i < Extents::rank(); ++i)
> + if(exts.static_extent(i) == std::dynamic_extent)
> + VERIFY(exts.extent(i) == 0);
> + else
> + VERIFY(exts.extent(i) == Extents::static_extent(i));
> + }
> +
> +constexpr bool
> +test_default_ctor_all()
> +{
> + test_default_ctor<std::extents<int, 1>>();
> + test_default_ctor<std::extents<int, dyn>>();
> + test_default_ctor<std::extents<int, 1, 2>>();
> + test_default_ctor<std::extents<int, dyn, 2>>();
> + test_default_ctor<std::extents<int, dyn, dyn>>();
> + test_default_ctor<std::extents<int, 1, 2, 3>>();
> + test_default_ctor<std::extents<int, dyn, 2, dyn>>();
> + test_default_ctor<std::extents<int, dyn, dyn, dyn>>();
> + return true;
> +}
> +
> +int
> +main()
> +{
> + test_default_ctor_all();
> + static_assert(test_default_ctor_all());
> + return 0;
> +}
> --
> 2.49.0
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20250526/1e809107/attachment.htm>
More information about the Libstdc++
mailing list