| Message ID | 20260707102055.512253-1-tkaminsk@redhat.com |
|---|---|
| State | New |
| Headers |
Return-Path: <gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 578564BA5436 for <patchwork@sourceware.org>; Tue, 7 Jul 2026 10:22:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 578564BA5436 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=dx7zD9ga X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 3BA274BA2E2D for <gcc-patches@gcc.gnu.org>; Tue, 7 Jul 2026 10:21:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3BA274BA2E2D Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3BA274BA2E2D Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783419661; cv=none; b=DRTkKYz17THRr6vJM5V8VLpc4w2P98c0zEi4cGgKHFBKbHCbWyEPCvDVAvxKITZrZnsowz4D+9LgTBoAkxFP21bP98MoDAItjsaztQAMKTz0aW204Lir/dzYgxBerB5hNt+60Xz9kfpuJiTjchxAGWkz/5LkxyyXIM+qpZDDsVc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783419661; c=relaxed/simple; bh=CUdyxGfCHn+2ie5g7NiENolsI356i2RrRh271GSBZx0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=a4JailtsnEGv8EjDlsDkRhavvDRuQOA5imevME4t7EI/zK1jEWH8N/5vRipiIc6uTg+CWN9JIztRggzTNfB8Ug+TbnGFFuyqgophSq0v3QW/URiDRGPzIxv8vwCjavaJgWTtU/Rh8+kcHYbsaePwcYTPsd9N2VmRf5w7dAY4MtU= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=dx7zD9ga DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3BA274BA2E2D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1783419660; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=D0Y4Tn2kZ+IgaNCRNXys6NY68XsK3qdoAPhui0XjIKA=; b=dx7zD9gaah9e/q7TqbYuwfy+mxuq4aI4DkdfRpBKuSlc2SZtvOg4MBxGCrTTQ3+hpuZsD6 fLb8THBvgRm2byXiefFeGiVqwOP2PUvZrFPXJTQyMd+CAAgoWvHCjtqqH9/1ZPCIBy9fFT 7PdyBkBYzYp4nNHL3ENVted3Go5qkxk= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-104-vHGTdeSVNeGcG7hBy6v58A-1; Tue, 07 Jul 2026 06:20:59 -0400 X-MC-Unique: vHGTdeSVNeGcG7hBy6v58A-1 X-Mimecast-MFC-AGG-ID: vHGTdeSVNeGcG7hBy6v58A_1783419658 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 47CE01956064; Tue, 7 Jul 2026 10:20:58 +0000 (UTC) Received: from localhost (unknown [10.44.49.91]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7D22D3000C00; Tue, 7 Jul 2026 10:20:57 +0000 (UTC) From: =?utf-8?q?Tomasz_Kami=C5=84ski?= <tkaminsk@redhat.com> To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 1/2] libstdc++: Validate user-provided stride values for layout_stride. Date: Tue, 7 Jul 2026 12:18:31 +0200 Message-ID: <20260707102055.512253-1-tkaminsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: CgMnUQ7RdDmeRregD2mKH3JgULivugK781Rr0ytzgtA_1783419658 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_PASS, SPF_NONE, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org |
| Series |
[1/2] libstdc++: Validate user-provided stride values for layout_stride.
|
|
Commit Message
Tomasz Kaminski
July 7, 2026, 10:18 a.m. UTC
Converting the __strides values using __index_type_cast asserts that they are non-negative and each value is representable as index_type. libstdc++-v3/ChangeLog: * include/std/mdspan (layout_stride::mapping::mapping(const extent_type&, span<...>): Convert strides using __index_type_cast, which bring asserts for negative and unrepresentable values. * testsuite/23_containers/mdspan/layouts/stride_neg.cc: New test. --- Tested on x86_64-linux locally. OK for trunk? libstdc++-v3/include/std/mdspan | 3 +- .../mdspan/layouts/stride_neg.cc | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride_neg.cc
Comments
On Tue, 7 Jul 2026 at 11:21, Tomasz Kamiński <tkaminsk@redhat.com> wrote: > > Converting the __strides values using __index_type_cast asserts > that they are non-negative and each value is representable as > index_type. > > libstdc++-v3/ChangeLog: > > * include/std/mdspan > (layout_stride::mapping::mapping(const extent_type&, span<...>): > Convert strides using __index_type_cast, which bring asserts > for negative and unrepresentable values. > * testsuite/23_containers/mdspan/layouts/stride_neg.cc: New test. > --- > Tested on x86_64-linux locally. OK for trunk? OK for trunk. I think this makes sense for gcc-16 too? > > libstdc++-v3/include/std/mdspan | 3 +- > .../mdspan/layouts/stride_neg.cc | 31 +++++++++++++++++++ > 2 files changed, 33 insertions(+), 1 deletion(-) > create mode 100644 libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride_neg.cc > > diff --git a/libstdc++-v3/include/std/mdspan b/libstdc++-v3/include/std/mdspan > index f5556f35fa1..5e1c4d8b1a4 100644 > --- a/libstdc++-v3/include/std/mdspan > +++ b/libstdc++-v3/include/std/mdspan > @@ -1891,7 +1891,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION > : _M_extents(__exts) > { > for (size_t __i = 0; __i < extents_type::rank(); ++__i) > - _M_strides[__i] = index_type(as_const(__strides[__i])); > + _M_strides[__i] > + = __mdspan::__index_type_cast<index_type>(__strides[__i]); > } > > template<typename _OIndexType> > diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride_neg.cc b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride_neg.cc > new file mode 100644 > index 00000000000..153560bc82f > --- /dev/null > +++ b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride_neg.cc > @@ -0,0 +1,31 @@ > +// { dg-do compile { target c++23 } } > +#include <mdspan> > + > +#include "../layout_traits.h" > +#include <cstdint> > + > +constexpr size_t dyn = std::dynamic_extent; > + > +constexpr bool > +test_stride_overflow() > +{ > + auto exts = std::extents<uint8_t, dyn, dyn>(1, 3); > + auto n = size_t(1) << 9; > + auto m = std::layout_stride::mapping(exts, std::array{n, 1zu}); // { dg-error "expansion of" } > + (void) m; > + return true; > +} > +static_assert(test_stride_overflow()); // { dg-error "expansion of" } > + > +constexpr bool > +test_stride_negative() > +{ > + auto exts = std::extents<std::size_t, dyn, dyn>(1, 3); > + auto m = std::layout_stride::mapping(exts, std::array{1, -4}); // { dg-error "expansion of" } > + (void) m; > + return true; > +} > +static_assert(test_stride_negative()); // { dg-error "expansion of" } > + > +// { dg-prune-output "non-constant condition for static assertion" } > +// { dg-prune-output "__glibcxx_assert_fail()" } > -- > 2.54.0 >
On Tue, Jul 7, 2026 at 1:59 PM Jonathan Wakely <jwakely@redhat.com> wrote: > On Tue, 7 Jul 2026 at 11:21, Tomasz Kamiński <tkaminsk@redhat.com> wrote: > > > > Converting the __strides values using __index_type_cast asserts > > that they are non-negative and each value is representable as > > index_type. > > > > libstdc++-v3/ChangeLog: > > > > * include/std/mdspan > > (layout_stride::mapping::mapping(const extent_type&, span<...>): > > Convert strides using __index_type_cast, which bring asserts > > for negative and unrepresentable values. > > * testsuite/23_containers/mdspan/layouts/stride_neg.cc: New test. > > --- > > Tested on x86_64-linux locally. OK for trunk? > > OK for trunk. I think this makes sense for gcc-16 too? > Yes, once LWG4603 is approved. I will submit PR for it this week. > > > > > libstdc++-v3/include/std/mdspan | 3 +- > > .../mdspan/layouts/stride_neg.cc | 31 +++++++++++++++++++ > > 2 files changed, 33 insertions(+), 1 deletion(-) > > create mode 100644 > libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride_neg.cc > > > > diff --git a/libstdc++-v3/include/std/mdspan > b/libstdc++-v3/include/std/mdspan > > index f5556f35fa1..5e1c4d8b1a4 100644 > > --- a/libstdc++-v3/include/std/mdspan > > +++ b/libstdc++-v3/include/std/mdspan > > @@ -1891,7 +1891,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION > > : _M_extents(__exts) > > { > > for (size_t __i = 0; __i < extents_type::rank(); ++__i) > > - _M_strides[__i] = index_type(as_const(__strides[__i])); > > + _M_strides[__i] > > + = __mdspan::__index_type_cast<index_type>(__strides[__i]); > > } > > > > template<typename _OIndexType> > > diff --git > a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride_neg.cc > b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride_neg.cc > > new file mode 100644 > > index 00000000000..153560bc82f > > --- /dev/null > > +++ b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride_neg.cc > > @@ -0,0 +1,31 @@ > > +// { dg-do compile { target c++23 } } > > +#include <mdspan> > > + > > +#include "../layout_traits.h" > > +#include <cstdint> > > + > > +constexpr size_t dyn = std::dynamic_extent; > > + > > +constexpr bool > > +test_stride_overflow() > > +{ > > + auto exts = std::extents<uint8_t, dyn, dyn>(1, 3); > > + auto n = size_t(1) << 9; > > + auto m = std::layout_stride::mapping(exts, std::array{n, 1zu}); // { > dg-error "expansion of" } > > + (void) m; > > + return true; > > +} > > +static_assert(test_stride_overflow()); // { dg-error "expansion of" } > > + > > +constexpr bool > > +test_stride_negative() > > +{ > > + auto exts = std::extents<std::size_t, dyn, dyn>(1, 3); > > + auto m = std::layout_stride::mapping(exts, std::array{1, -4}); // { > dg-error "expansion of" } > > + (void) m; > > + return true; > > +} > > +static_assert(test_stride_negative()); // { dg-error "expansion of" } > > + > > +// { dg-prune-output "non-constant condition for static assertion" } > > +// { dg-prune-output "__glibcxx_assert_fail()" } > > -- > > 2.54.0 > > > >
On Tue, Jul 7, 2026 at 12:21 PM Tomasz Kamiński <tkaminsk@redhat.com> wrote: > Converting the __strides values using __index_type_cast asserts > that they are non-negative and each value is representable as > index_type. > > libstdc++-v3/ChangeLog: > > * include/std/mdspan > (layout_stride::mapping::mapping(const extent_type&, span<...>): > Convert strides using __index_type_cast, which bring asserts > for negative and unrepresentable values. > * testsuite/23_containers/mdspan/layouts/stride_neg.cc: New test. > --- > Tested on x86_64-linux locally. OK for trunk? > > libstdc++-v3/include/std/mdspan | 3 +- > .../mdspan/layouts/stride_neg.cc | 31 +++++++++++++++++++ > 2 files changed, 33 insertions(+), 1 deletion(-) > create mode 100644 > libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride_neg.cc > > diff --git a/libstdc++-v3/include/std/mdspan > b/libstdc++-v3/include/std/mdspan > index f5556f35fa1..5e1c4d8b1a4 100644 > --- a/libstdc++-v3/include/std/mdspan > +++ b/libstdc++-v3/include/std/mdspan > @@ -1891,7 +1891,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION > : _M_extents(__exts) > { > for (size_t __i = 0; __i < extents_type::rank(); ++__i) > - _M_strides[__i] = index_type(as_const(__strides[__i])); > + _M_strides[__i] > + = __mdspan::__index_type_cast<index_type>(__strides[__i]); > Before committing, I will restore as_const call here, to match types that do not have const-conversion, to match standard to the letter. > } > > template<typename _OIndexType> > diff --git > a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride_neg.cc > b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride_neg.cc > new file mode 100644 > index 00000000000..153560bc82f > --- /dev/null > +++ b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride_neg.cc > @@ -0,0 +1,31 @@ > +// { dg-do compile { target c++23 } } > +#include <mdspan> > + > +#include "../layout_traits.h" > +#include <cstdint> > + > +constexpr size_t dyn = std::dynamic_extent; > + > +constexpr bool > +test_stride_overflow() > +{ > + auto exts = std::extents<uint8_t, dyn, dyn>(1, 3); > + auto n = size_t(1) << 9; > + auto m = std::layout_stride::mapping(exts, std::array{n, 1zu}); // { > dg-error "expansion of" } > + (void) m; > + return true; > +} > +static_assert(test_stride_overflow()); // { dg-error "expansion of" } > + > +constexpr bool > +test_stride_negative() > +{ > + auto exts = std::extents<std::size_t, dyn, dyn>(1, 3); > + auto m = std::layout_stride::mapping(exts, std::array{1, -4}); // { > dg-error "expansion of" } > + (void) m; > + return true; > +} > +static_assert(test_stride_negative()); // { dg-error "expansion of" } > + > +// { dg-prune-output "non-constant condition for static assertion" } > +// { dg-prune-output "__glibcxx_assert_fail()" } > -- > 2.54.0 > >
diff --git a/libstdc++-v3/include/std/mdspan b/libstdc++-v3/include/std/mdspan index f5556f35fa1..5e1c4d8b1a4 100644 --- a/libstdc++-v3/include/std/mdspan +++ b/libstdc++-v3/include/std/mdspan @@ -1891,7 +1891,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : _M_extents(__exts) { for (size_t __i = 0; __i < extents_type::rank(); ++__i) - _M_strides[__i] = index_type(as_const(__strides[__i])); + _M_strides[__i] + = __mdspan::__index_type_cast<index_type>(__strides[__i]); } template<typename _OIndexType> diff --git a/libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride_neg.cc b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride_neg.cc new file mode 100644 index 00000000000..153560bc82f --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/mdspan/layouts/stride_neg.cc @@ -0,0 +1,31 @@ +// { dg-do compile { target c++23 } } +#include <mdspan> + +#include "../layout_traits.h" +#include <cstdint> + +constexpr size_t dyn = std::dynamic_extent; + +constexpr bool +test_stride_overflow() +{ + auto exts = std::extents<uint8_t, dyn, dyn>(1, 3); + auto n = size_t(1) << 9; + auto m = std::layout_stride::mapping(exts, std::array{n, 1zu}); // { dg-error "expansion of" } + (void) m; + return true; +} +static_assert(test_stride_overflow()); // { dg-error "expansion of" } + +constexpr bool +test_stride_negative() +{ + auto exts = std::extents<std::size_t, dyn, dyn>(1, 3); + auto m = std::layout_stride::mapping(exts, std::array{1, -4}); // { dg-error "expansion of" } + (void) m; + return true; +} +static_assert(test_stride_negative()); // { dg-error "expansion of" } + +// { dg-prune-output "non-constant condition for static assertion" } +// { dg-prune-output "__glibcxx_assert_fail()" }