public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/ranger] libstdc++: Fix std::lexicographic_compare for unsigned char (PR 93972)
@ 2020-06-17 19:21 Aldy Hernandez
  0 siblings, 0 replies; only message in thread
From: Aldy Hernandez @ 2020-06-17 19:21 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:d112e173ea093f55a16a14b26ef65088381ee09c

commit d112e173ea093f55a16a14b26ef65088381ee09c
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Mar 2 17:03:28 2020 +0000

    libstdc++: Fix std::lexicographic_compare for unsigned char (PR 93972)
    
    The new 25_algorithms/lexicographical_compare/93972.cc test fails on
    targets where char is unsigned, revealing an existing regression with
    the std::__memcmp helper that had gone unnoticed in
    std::lexicographical_compare. When comparing char and unsigned char, the
    memcmp optimisation is enabled, but the new std::__memcmp function fails
    to compile for mismatched types.
    
            PR libstdc++/93972
            * include/bits/stl_algobase.h (__memcmp): Allow pointer types to
            differ.
            * testsuite/25_algorithms/lexicographical_compare/uchar.cc: New test.

Diff:
---
 libstdc++-v3/ChangeLog                             |  5 ++
 libstdc++-v3/include/bits/stl_algobase.h           |  7 ++-
 .../25_algorithms/lexicographical_compare/uchar.cc | 61 ++++++++++++++++++++++
 3 files changed, 71 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index d810b0887b5..28957dc5c99 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,5 +1,10 @@
 2020-03-02  Jonathan Wakely  <jwakely@redhat.com>
 
+	PR libstdc++/93972
+	* include/bits/stl_algobase.h (__memcmp): Allow pointer types to
+	differ.
+	* testsuite/25_algorithms/lexicographical_compare/uchar.cc: New test.
+
 	* include/std/ranges (__detail::__maybe_empty_t): Rename to
 	__maybe_present_t.
 	(__adaptor::_RangeAdaptor, join_view, split_view): Use new name.
diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h
index 5ec2f25424d..7a9d932b421 100644
--- a/libstdc++-v3/include/bits/stl_algobase.h
+++ b/libstdc++-v3/include/bits/stl_algobase.h
@@ -84,11 +84,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
    * A constexpr wrapper for __builtin_memcmp.
    * @param __num The number of elements of type _Tp (not bytes).
    */
-  template<typename _Tp>
+  template<typename _Tp, typename _Up>
     _GLIBCXX14_CONSTEXPR
     inline int
-    __memcmp(const _Tp* __first1, const _Tp* __first2, size_t __num)
+    __memcmp(const _Tp* __first1, const _Up* __first2, size_t __num)
     {
+#if __cplusplus >= 201103L
+      static_assert(sizeof(_Tp) == sizeof(_Up), "can be compared with memcmp");
+#endif
 #ifdef __cpp_lib_is_constant_evaluated
       if (std::is_constant_evaluated())
 	{
diff --git a/libstdc++-v3/testsuite/25_algorithms/lexicographical_compare/uchar.cc b/libstdc++-v3/testsuite/25_algorithms/lexicographical_compare/uchar.cc
new file mode 100644
index 00000000000..990bb1e7489
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/lexicographical_compare/uchar.cc
@@ -0,0 +1,61 @@
+// 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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-fchar8_t" }
+// { dg-do run }
+
+#include <algorithm>
+#include <testsuite_hooks.h>
+
+// Check that the memcmp optimization in std::lexicographical_compare
+// still works for mixed-type comparisons.
+
+void
+test01()
+{
+  unsigned char a = 0;
+  char8_t b = 1;
+  VERIFY( std::lexicographical_compare(&a, &a + 1, &b, &b + 1) );
+  VERIFY( ! std::lexicographical_compare(&b, &b + 1, &a, &a + 1) );
+}
+
+void
+test02()
+{
+  // N.B. if char is signed this won't actually use the memcmp optimization.
+  unsigned char a = 0;
+  char b = 1;
+  VERIFY( std::lexicographical_compare(&a, &a + 1, &b, &b + 1) );
+  VERIFY( ! std::lexicographical_compare(&b, &b + 1, &a, &a + 1) );
+}
+
+void
+test03()
+{
+  // N.B. if char is signed this won't actually use the memcmp optimization.
+  char a = 0;
+  char8_t b = 1;
+  VERIFY( std::lexicographical_compare(&a, &a + 1, &b, &b + 1) );
+  VERIFY( ! std::lexicographical_compare(&b, &b + 1, &a, &a + 1) );
+}
+
+int main()
+{
+  test01();
+  test02();
+  test03();
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-06-17 19:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-17 19:21 [gcc/devel/ranger] libstdc++: Fix std::lexicographic_compare for unsigned char (PR 93972) Aldy Hernandez

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).