From patchwork Mon Jul 8 19:42:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1958104 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=b8Z2T523; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WHvg024yXz1xpP for ; Tue, 9 Jul 2024 05:43:08 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 56324393BA58 for ; Mon, 8 Jul 2024 19:43:05 +0000 (GMT) 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 ESMTPS id 42CAD3841FF3 for ; Mon, 8 Jul 2024 19:42:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 42CAD3841FF3 Authentication-Results: sourceware.org; dmarc=pass (p=none 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 42CAD3841FF3 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720467762; cv=none; b=wT9qoMpIvfWqIcuMmNbtktkdP1D/le641jXmuhNHx6tn9o1TpCO1cYsuGWf46SfHDEcYjsavYB9EWgkG7LbD09ZDUpODxA4bGFCb8jozOHRuWtcEzNjLyQExUFeRamKwscArPdGLwkXPTj0cB0QugvHsHxQR0Z/bT0aY9fRUqUQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720467762; c=relaxed/simple; bh=uzgeOJqsfjN+xrycaJDK/Cm3zEBvVK1W6xYDZxxm764=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=XZP/33eN8XcbXLNocKcrVdRhgL/BZVuLS573sZJuy5fTRcZuvQW8qsanv+t+zaR9hFvPaJZ3pVvklMgbPSKDX+7e2OMirXYxSMs90b794UqYoIYLNCbYb+rJ4nf+FA0Dx9iNEf0rhhqwpIFVktNBtQpObEqiLlxp+QVNNfoXlaE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1720467757; 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=5QZEeZVVf2CmVdGkFKJppT9N+NUiAA5lIqys8ky3qmY=; b=b8Z2T5238gr8lAFqXzFsjv9M9CG1u4oZHB8oAqTnEc0FrEVqrJXINE15C5wmdadiPu/n+2 lW/J0J+DJKXFVMAP0z+L7geguiDZ/LhPpxd2c3ISr9I0mVV4OjQY0RtBuIL5p5E8KDccTm v1appwuHz2XjqVLXiD7XTpUKPeSGjew= 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-614-QGVhB1MTNrmhBFvzV5yB7A-1; Mon, 08 Jul 2024 15:42:35 -0400 X-MC-Unique: QGVhB1MTNrmhBFvzV5yB7A-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 BA77C1936161; Mon, 8 Jul 2024 19:42:33 +0000 (UTC) Received: from localhost (unknown [10.42.28.19]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 237C91955E70; Mon, 8 Jul 2024 19:42:32 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: ranges::find needs explicit conversion to size_t [PR115799] Date: Mon, 8 Jul 2024 20:42:03 +0100 Message-ID: <20240708194231.494096-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 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, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org This fixes another problem with my recent changes to use memchr in std::find. Tested x86_64-linux. -- >8 -- For an integer-class type we need to use an explicit conversion to size_t. libstdc++-v3/ChangeLog: PR libstdc++/115799 * include/bits/ranges_util.h (__find_fn): Make conversion from difference type ti size_t explicit. * testsuite/25_algorithms/find/bytes.cc: Check ranges::find with __gnu_test::test_contiguous_range. * testsuite/std/ranges/range.cc: Adjust expected difference_type for __gnu_test::test_contiguous_range. * testsuite/util/testsuite_iterators.h (contiguous_iterator_wrapper): Use __max_diff_type as difference type. (test_range::sentinel, test_sized_range_sized_sent::sentinel): Ensure that operator- returns difference_type. --- libstdc++-v3/include/bits/ranges_util.h | 3 +- .../testsuite/25_algorithms/find/bytes.cc | 10 +++++ libstdc++-v3/testsuite/std/ranges/range.cc | 5 ++- .../testsuite/util/testsuite_iterators.h | 42 ++++++++++++++----- 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/libstdc++-v3/include/bits/ranges_util.h b/libstdc++-v3/include/bits/ranges_util.h index a1f42875b11..e6d96073e87 100644 --- a/libstdc++-v3/include/bits/ranges_util.h +++ b/libstdc++-v3/include/bits/ranges_util.h @@ -506,9 +506,10 @@ namespace ranges if (static_cast<_Vt>(__value) == __value) [[likely]] if (__n > 0) { + const size_t __nu = static_cast(__n); const int __ival = static_cast(__value); const void* __p0 = std::to_address(__first); - if (auto __p1 = __builtin_memchr(__p0, __ival, __n)) + if (auto __p1 = __builtin_memchr(__p0, __ival, __nu)) __n = (const char*)__p1 - (const char*)__p0; } return __first + __n; diff --git a/libstdc++-v3/testsuite/25_algorithms/find/bytes.cc b/libstdc++-v3/testsuite/25_algorithms/find/bytes.cc index e1d6c01ab21..03dada0fec7 100644 --- a/libstdc++-v3/testsuite/25_algorithms/find/bytes.cc +++ b/libstdc++-v3/testsuite/25_algorithms/find/bytes.cc @@ -114,9 +114,19 @@ test_non_characters() #endif } +#if __cpp_lib_ranges +void +test_pr115799c0(__gnu_test::test_contiguous_range r) +{ + // Non-common range with integer-class type as difference_type. + (void) std::ranges::find(r, 'a'); +} +#endif + void test_pr115799c2(__gnu_test::input_iterator_wrapper i) { + // Non-contiguous range of character type. (void) std::find(i, i, 'a'); } diff --git a/libstdc++-v3/testsuite/std/ranges/range.cc b/libstdc++-v3/testsuite/std/ranges/range.cc index 760f6ffacfd..5464a9bf66b 100644 --- a/libstdc++-v3/testsuite/std/ranges/range.cc +++ b/libstdc++-v3/testsuite/std/ranges/range.cc @@ -56,6 +56,7 @@ static_assert( std::ranges::range&> ); using std::same_as; using C = test_contiguous_range; +using R = test_random_access_range; using I = test_input_range; using O = test_output_range; @@ -69,7 +70,9 @@ static_assert( same_as, static_assert( same_as, decltype(std::declval().end())> ); -static_assert( same_as, +static_assert( ! same_as, + std::ptrdiff_t> ); // __detail::__max_diff_type +static_assert( same_as, std::ptrdiff_t> ); static_assert( same_as, std::ptrdiff_t> ); diff --git a/libstdc++-v3/testsuite/util/testsuite_iterators.h b/libstdc++-v3/testsuite/util/testsuite_iterators.h index ec2971284b4..e7f7abe222d 100644 --- a/libstdc++-v3/testsuite/util/testsuite_iterators.h +++ b/libstdc++-v3/testsuite/util/testsuite_iterators.h @@ -34,6 +34,10 @@ #include #endif +#if __cplusplus > 201703L +#include +#endif + #ifndef _TESTSUITE_ITERATORS #define _TESTSUITE_ITERATORS @@ -675,6 +679,9 @@ namespace __gnu_test using iterator_concept = std::contiguous_iterator_tag; + // Use an integer-class type to try and break the library code. + using difference_type = std::ranges::__detail::__max_diff_type; + contiguous_iterator_wrapper& operator++() { @@ -706,27 +713,42 @@ namespace __gnu_test } contiguous_iterator_wrapper& - operator+=(std::ptrdiff_t n) + operator+=(difference_type n) { - random_access_iterator_wrapper::operator+=(n); + auto d = static_cast(n); + random_access_iterator_wrapper::operator+=(d); return *this; } friend contiguous_iterator_wrapper - operator+(contiguous_iterator_wrapper iter, std::ptrdiff_t n) + operator+(contiguous_iterator_wrapper iter, difference_type n) { return iter += n; } friend contiguous_iterator_wrapper - operator+(std::ptrdiff_t n, contiguous_iterator_wrapper iter) + operator+(difference_type n, contiguous_iterator_wrapper iter) { return iter += n; } contiguous_iterator_wrapper& - operator-=(std::ptrdiff_t n) + operator-=(difference_type n) { return *this += -n; } friend contiguous_iterator_wrapper - operator-(contiguous_iterator_wrapper iter, std::ptrdiff_t n) + operator-(contiguous_iterator_wrapper iter, difference_type n) { return iter -= n; } + + friend difference_type + operator-(contiguous_iterator_wrapper l, contiguous_iterator_wrapper r) + { + const random_access_iterator_wrapper& lbase = l; + const random_access_iterator_wrapper& rbase = r; + return static_cast(lbase - rbase); + } + + decltype(auto) operator[](difference_type n) const + { + auto d = static_cast(n); + return random_access_iterator_wrapper::operator[](d); + } }; template @@ -788,11 +810,11 @@ namespace __gnu_test friend auto operator-(const sentinel& s, const I& i) noexcept requires std::random_access_iterator - { return s.end - i.ptr; } + { return std::iter_difference_t(s.end - i.ptr); } friend auto operator-(const I& i, const sentinel& s) noexcept requires std::random_access_iterator - { return i.ptr - s.end; } + { return std::iter_difference_t(i.ptr - s.end); } }; protected: @@ -890,11 +912,11 @@ namespace __gnu_test friend std::iter_difference_t operator-(const sentinel& s, const I& i) noexcept - { return s.end - i.ptr; } + { return std::iter_difference_t(s.end - i.ptr); } friend std::iter_difference_t operator-(const I& i, const sentinel& s) noexcept - { return i.ptr - s.end; } + { return std::iter_difference_t(i.ptr - s.end); } }; auto end() &