From patchwork Thu Feb 29 17:49:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1906429 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=fzNTZbAh; 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 4TlzM315wcz23fC for ; Fri, 1 Mar 2024 04:52:15 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1B1AC3858C53 for ; Thu, 29 Feb 2024 17:52:13 +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 F37863858431 for ; Thu, 29 Feb 2024 17:51:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F37863858431 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 F37863858431 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=1709229100; cv=none; b=Io3r/QT4udgIL1RpbTKXvzltU3WtsOidAEh+qdjGN/+Oiah8YkkGCinJLafxWn8nnqk7G7YdMwyC+uAm0V5B5f7rgBhQsnvLKAPlxQaLt5yJvJwVc4okwnbn1ASS9xOhXBtuVMbrLLQfAsCfa1M3jvdDni+8UPlw/2zrgIbQfio= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709229100; c=relaxed/simple; bh=gkMo4s5SMDMEy7hqW47KkIPrdUWy+R059vG5qS1c2CQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=l4TCXw8i3ycBp//SZpXWjRkpRACFxUEHSZP34O07DRlv7PTswkcQ2pDnpbVBbNk6Li1zrVrJrZXyGAsFR5Tsnz7WzbNQOTCA4ggk05CfvHWMD7PH29h9tVg0OLiMhwy0idx8uB3AHGVwoGhqX3QskIv9ACJsx0NfLFDPz2tIEMs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709229096; 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=1pYcsx1kUwevvY9TEe6itxwezx+kfsHBTp+J6fiBTOY=; b=fzNTZbAh0zB+DmMRK1ObXSAeZPxhzGEapUHJi7NWQc30Ts3olwL1xDo4sZrlIFUtb+ujkc 2/bWUvEd2PyFDn1zaTNt/VhzEZYvir/vPBMH1l8QvZK56opkdWI5M6WTcu4rGAOKuMEyTh nZJIX0ypG7dkhjp+tRpRcwJ6/kfRhmM= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-466-YVg2lFe2OyuhmUhvm2y9ZA-1; Thu, 29 Feb 2024 12:51:34 -0500 X-MC-Unique: YVg2lFe2OyuhmUhvm2y9ZA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 24C42106D022; Thu, 29 Feb 2024 17:51:34 +0000 (UTC) Received: from localhost (unknown [10.42.28.150]) by smtp.corp.redhat.com (Postfix) with ESMTP id E5B91112132A; Thu, 29 Feb 2024 17:51:33 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix conditions for using memcmp in std::lexicographical_compare_three_way [PR113960] Date: Thu, 29 Feb 2024 17:49:59 +0000 Message-ID: <20240229175133.26905-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, URI_HEX 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 Tested aarch64-linux, powerpc-linux (power 7 BE), x86_64-linux. The bug reporter tested it on s390x too. Pushed to trunk. This should be backported too. -- >8 -- The change in r11-2981-g2f983fa69005b6 meant that std::lexicographical_compare_three_way started to use memcmp for unsigned integers on big endian targets, but for that to be valid we need the two value types to have the same size and we need to use that size to compute the length passed to memcmp. I already defined a __is_memcmp_ordered_with trait that does the right checks, std::lexicographical_compare_three_way just needs to use it. libstdc++-v3/ChangeLog: PR libstdc++/113960 * include/bits/stl_algobase.h (__is_byte_iter): Replace with ... (__memcmp_ordered_with): New concept. (lexicographical_compare_three_way): Use __memcmp_ordered_with instead of __is_byte_iter. Use correct length for memcmp. * testsuite/25_algorithms/lexicographical_compare_three_way/113960.cc: New test. --- libstdc++-v3/include/bits/stl_algobase.h | 41 ++++++++++--------- .../113960.cc | 15 +++++++ 2 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 libstdc++-v3/testsuite/25_algorithms/lexicographical_compare_three_way/113960.cc diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h index d534e02871f..74ff42d4f39 100644 --- a/libstdc++-v3/include/bits/stl_algobase.h +++ b/libstdc++-v3/include/bits/stl_algobase.h @@ -1806,11 +1806,14 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO } #if __cpp_lib_three_way_comparison - // Iter points to a contiguous range of unsigned narrow character type - // or std::byte, suitable for comparison by memcmp. - template - concept __is_byte_iter = contiguous_iterator<_Iter> - && __is_memcmp_ordered>::__value; + // Both iterators refer to contiguous ranges of unsigned narrow characters, + // or std::byte, or big-endian unsigned integers, suitable for comparison + // using memcmp. + template + concept __memcmp_ordered_with + = (__is_memcmp_ordered_with, + iter_value_t<_Iter2>>::__value) + && contiguous_iterator<_Iter1> && contiguous_iterator<_Iter2>; // Return a struct with two members, initialized to the smaller of x and y // (or x if they compare equal) and the result of the comparison x <=> y. @@ -1860,20 +1863,20 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO if (!std::__is_constant_evaluated()) if constexpr (same_as<_Comp, __detail::_Synth3way> || same_as<_Comp, compare_three_way>) - if constexpr (__is_byte_iter<_InputIter1>) - if constexpr (__is_byte_iter<_InputIter2>) - { - const auto [__len, __lencmp] = _GLIBCXX_STD_A:: - __min_cmp(__last1 - __first1, __last2 - __first2); - if (__len) - { - const auto __c - = __builtin_memcmp(&*__first1, &*__first2, __len) <=> 0; - if (__c != 0) - return __c; - } - return __lencmp; - } + if constexpr (__memcmp_ordered_with<_InputIter1, _InputIter2>) + { + const auto [__len, __lencmp] = _GLIBCXX_STD_A:: + __min_cmp(__last1 - __first1, __last2 - __first2); + if (__len) + { + const auto __blen = __len * sizeof(*__first1); + const auto __c + = __builtin_memcmp(&*__first1, &*__first2, __blen) <=> 0; + if (__c != 0) + return __c; + } + return __lencmp; + } while (__first1 != __last1) { diff --git a/libstdc++-v3/testsuite/25_algorithms/lexicographical_compare_three_way/113960.cc b/libstdc++-v3/testsuite/25_algorithms/lexicographical_compare_three_way/113960.cc new file mode 100644 index 00000000000..d51ae1a3d50 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/lexicographical_compare_three_way/113960.cc @@ -0,0 +1,15 @@ +// { dg-do run { target c++20 } } + +// PR libstdc++/113960 +// std::map with std::vector as input overwrites itself with c++20, on s390x + +#include +#include + +int main() +{ + unsigned short a1[] { 1, 2, 3 }; + unsigned short a2[] { 1, 2, 4 }; + // Incorrect memcmp comparison for big endian targets. + VERIFY( std::lexicographical_compare_three_way(a1, a1+3, a2, a2+3) < 0 ); +}