From patchwork Mon Apr 27 21:34:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1277972 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=tyryh+xC; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 499yjZ1MCvz9sSK for ; Tue, 28 Apr 2020 07:34:56 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E9E303897803; Mon, 27 Apr 2020 21:34:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E9E303897803 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1588023293; bh=hu6BZUUnCLE6Y4UK0Hw3FaJMLA8LJTvKb1KhoY474EM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=tyryh+xC4kU+EchbdhUsaQ8T6asdGRjtFCmyRkGm6SUZskIkczTSWuuyDtuXaj3xs WjVDym8WgnROb4vYJ2+5V7Fqosse9H8314UZOXAdiRqpybbsIaQKWXI0pac5uLOWSJ FNREvIxZHPEP8qCY1Jo0OIxGgi8LFzARbnrxGVlo= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by sourceware.org (Postfix) with ESMTP id 98CE03858D32 for ; Mon, 27 Apr 2020 21:34:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 98CE03858D32 Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-326-YpgpnK4uMAiZCLL4l32pCA-1; Mon, 27 Apr 2020 17:34:44 -0400 X-MC-Unique: YpgpnK4uMAiZCLL4l32pCA-1 Received: by mail-qv1-f71.google.com with SMTP id f4so20252362qvu.19 for ; Mon, 27 Apr 2020 14:34:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=hd3GjcHiPyh1yEE2DbBfIC+wZ/TL64CaHCFDFjcIkLk=; b=LD+H624BQpmrtBu6bZ3fL4TvGJvWcbcTGEC+4/i4fARne4X22LdTGS3GV4vkJoMIBO 6EX43Yx3mOMH3finuLPSiQV6DDYmVdhn0pr/BlvQaQsCKN1mP07WQCqOqStbQaxA/LHY TKlN354tdLWXVO41ENOr6lP1Emh5h+f/VtlMA8IoyRW1MlUPKO7VHpY/SGuSgpbFBlmp 4TGv+zk2yRMhZTg7i9B0eoXhn5Tg21PQZncYZmUvNHc2/im2pg3ChYdBoRUrsJViqFfH OPGsM2KMNNZAEtNoU0CQjmAGkB0QuSnl12EUnN7reY/1ZD3eFXvs+E+KkGa38hWN1ZgD 1Esw== X-Gm-Message-State: AGi0PuZ9DHoJtEvaK2hkvKdgFFag4w8Fj2Nn1O4Jt/CoH/uPegkvKtK+ zrmLrfPL30B8xq7978siKqMujzrFKzwCh1pXxmItfP3RTefe/ji5030cxbQwNfk9XTGnxu08lK8 Y8Kjzqr2TdTTBfJDbvA== X-Received: by 2002:a05:620a:a43:: with SMTP id j3mr23709555qka.295.1588023283598; Mon, 27 Apr 2020 14:34:43 -0700 (PDT) X-Google-Smtp-Source: APiQypLnXfmC6qxFxR1Zo+IYf4N2EBfhoKFPhAd/YzhW/W5nTP1/Xe7ULx5ZFuW4bf8Jt7nJFoNGYw== X-Received: by 2002:a05:620a:a43:: with SMTP id j3mr23709523qka.295.1588023283100; Mon, 27 Apr 2020 14:34:43 -0700 (PDT) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id 70sm11854816qkh.67.2020.04.27.14.34.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2020 14:34:42 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Fix subrange::advance and subrange::prev (LWG 3433) Date: Mon, 27 Apr 2020 17:34:32 -0400 Message-Id: <20200427213432.3502548-1-ppalka@redhat.com> X-Mailer: git-send-email 2.26.2.266.ge870325ee8 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-28.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Cc: jwakely@redhat.com, libstdc++@gcc.gnu.org Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" This implements the proposed resolution of LWG 3433, which fixes subrange::advance when called with a negative argument. Tested on x86_64-pc-linux-gnu, does this look OK to commit? libstdc++-v3/ChangeLog: LWG 3433 subrange::advance(n) has UB when n < 0 * include/std/ranges (subrange::prev): Fix typo. (subrange::advance): Handle a negative argument as per the proposed resolution of LWG 3433. * testsuite/std/ranges/subrange/lwg3433.cc: New test. --- libstdc++-v3/include/std/ranges | 25 +++-- .../testsuite/std/ranges/subrange/lwg3433.cc | 96 +++++++++++++++++++ 2 files changed, 111 insertions(+), 10 deletions(-) create mode 100644 libstdc++-v3/testsuite/std/ranges/subrange/lwg3433.cc diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index 8f91598c26e..565366a8d2f 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -353,23 +353,28 @@ namespace ranges requires bidirectional_iterator<_It> { auto __tmp = *this; - __tmp.advance(--__n); + __tmp.advance(-__n); return __tmp; } constexpr subrange& advance(iter_difference_t<_It> __n) { + // We incorporate the proposed resolution of LWG 3433 here, + // avoiding undefined behavior when __n < 0. + if constexpr (bidirectional_iterator<_It>) + if (__n < 0) + { + ranges::advance(_M_begin, __n); + if constexpr (_S_store_size) + _M_size._M_size += __detail::__to_unsigned_like(-__n); + return *this; + } + + __glibcxx_assert(__n >= 0); + auto __d = __n - ranges::advance(_M_begin, __n, _M_end); if constexpr (_S_store_size) - { - auto __d = __n - ranges::advance(_M_begin, __n, _M_end); - if (__d >= 0) - _M_size._M_size -= __detail::__to_unsigned_like(__d); - else - _M_size._M_size += __detail::__to_unsigned_like(-__d); - } - else - ranges::advance(_M_begin, __n, _M_end); + _M_size._M_size -= __detail::__to_unsigned_like(__d); return *this; } }; diff --git a/libstdc++-v3/testsuite/std/ranges/subrange/lwg3433.cc b/libstdc++-v3/testsuite/std/ranges/subrange/lwg3433.cc new file mode 100644 index 00000000000..2b01729b40f --- /dev/null +++ b/libstdc++-v3/testsuite/std/ranges/subrange/lwg3433.cc @@ -0,0 +1,96 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include +#include +#include + +using __gnu_test::bidirectional_iterator_wrapper; +using __gnu_test::forward_iterator_wrapper; +using __gnu_test::test_range; +using __gnu_test::test_sized_range; +using __gnu_test::test_sized_range_sized_sent; + +namespace ranges = std::ranges; + +template +void +test01() +{ + int x[] = {1,2,3,4,5}; + Container r{x}; + ranges::subrange sr = r; + constexpr bool sized_range_p = ranges::sized_range; + constexpr bool bidirectional_p = ranges::bidirectional_range; + VERIFY( ranges::equal(sr, (int[]){1,2,3,4,5}) ); + if constexpr (sized_range_p) + VERIFY( sr.size() == 5 ); + + sr = sr.next(); + VERIFY( ranges::equal(sr, (int[]){2,3,4,5}) ); + if constexpr (sized_range_p) + VERIFY( sr.size() == 4 ); + + sr = std::move(sr.next(2)); + VERIFY( ranges::equal(sr, (int[]){4,5}) ); + if constexpr (sized_range_p) + VERIFY( sr.size() == 2 ); + + if constexpr (bidirectional_p) + { + sr = sr.prev(2); + VERIFY( ranges::equal(sr, (int[]){2,3,4,5}) ); + if constexpr (sized_range_p) + VERIFY( sr.size() == 4 ); + + sr = sr.prev(1); + VERIFY( ranges::equal(sr, (int[]){1,2,3,4,5}) ); + if constexpr (sized_range_p) + VERIFY( sr.size() == 5 ); + } + else + sr = r; + + sr.advance(1); + VERIFY( ranges::equal(sr, (int[]){2,3,4,5}) ); + if constexpr (sized_range_p) + VERIFY( sr.size() == 4 ); + + if constexpr (bidirectional_p) + { + sr.advance(-1); + VERIFY( ranges::equal(sr, (int[]){1,2,3,4,5}) ); + if constexpr (sized_range_p) + VERIFY( sr.size() == 5 ); + } +} + +int +main() +{ + test01>(); + test01>(); + test01>(); + + test01>(); + test01>(); + test01>(); +}