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: 208454 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp572132dyb; Thu, 29 Feb 2024 09:52:45 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV+eZzIXbPdThuWF346JexCPg08woLG7Yh8D9G1ydx4n+D7tHfC2k9Bsr1tHi2Ekj0LNeoC/f07mctk7T0qWj67/vWqjg== X-Google-Smtp-Source: AGHT+IEMO2gPGr/vJvQtPbEZQPQu0zuyrZFbvuiYIBC9dAURYCqFurL5lFz6Q9100eJ9mlSY6AjE X-Received: by 2002:a05:622a:56:b0:42e:6100:8bc9 with SMTP id y22-20020a05622a005600b0042e61008bc9mr3154552qtw.1.1709229165198; Thu, 29 Feb 2024 09:52:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709229165; cv=pass; d=google.com; s=arc-20160816; b=bKBh0VK+psXn87uAGHtvpn7+sMb0/O5DhgjrJt4a11BF7yOPiGqpbGRXjHnw2oZRiJ Di0BZ+zMfL9Cs7DpILpxO/ryLIFVD9HaBxEG0AwRqpklNbBNJphIGbQJ1Pm5q/s/Tuph 2Kg+etTmohJNUZwIBtIXDnI2zObmEN+l/x0du3zm3vGA9+fXhdspg4U+9RHZ33sPHi4x 8oAwKeV+ulcrFX5y1rGEPqcCNvVTKNElMQOmwW0BGY1ZbYVBdl3sBPdeg+lVNAEOzdnR EVemH4Gr7RoN0r3ZAX7zd0HPbYOaS6hdyTY3PZcpXbYwREOhJY9lYwjLEQN2qn4HKj9d b7pA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=1pYcsx1kUwevvY9TEe6itxwezx+kfsHBTp+J6fiBTOY=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=b8MTJES/9d8WcpQ/bFrHmOmmrwC1ZZyHF/MCt93NNNf2z13AKqMgKLkOg+RzOKDjTe Lu6EKQLoPd9XXxyOjafK8nOCgGC3GbSC2Z944SVEWtmJMgEpPoiO4yh/rVsdcLs4gtYc YG0YHhAzBIbQ8w5oRJjH5w50hhQBAml/rzCotbG6UNuUSzE8MfmzKoDlB+hvK1NgV6IH 5EMBxPi1UG+ddxfyoasLC/1izsMkubgQHMP6tB0DkIPGFpUDt0i3Lz/SO43aRbfCYQaE F0UMJNq6HtkI19ulCYHmcVV3A7OG9hC4d9FEkmrzCEHPOwlM8ZXMk07mT/i2gALqVPcJ ajzg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fzNTZbAh; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id po25-20020a05620a385900b007873af4a4e7si1781811qkn.764.2024.02.29.09.52.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 09:52:45 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fzNTZbAh; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D897C3858401 for ; Thu, 29 Feb 2024 17:52:44 +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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792256681027457953 X-GMAIL-MSGID: 1792256681027457953 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 ); +}