[libcxx-commits] [libcxx] 5b4a98e - [libcxx] Qualify make_pair in searcher implementations to prevent ADL

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Thu May 7 08:57:21 PDT 2020


Author: Logan Smith
Date: 2020-05-07T11:57:10-04:00
New Revision: 5b4a98eb58aa7672bbdc5e31e573f8f1220fc9c7

URL: https://github.com/llvm/llvm-project/commit/5b4a98eb58aa7672bbdc5e31e573f8f1220fc9c7
DIFF: https://github.com/llvm/llvm-project/commit/5b4a98eb58aa7672bbdc5e31e573f8f1220fc9c7.diff

LOG: [libcxx] Qualify make_pair in searcher implementations to prevent ADL

This patch adds `_VSTD::` to some calls to `make_pair` inside the
implementations of searchers, to prevent things exploding if there is
a make_pair in an associated namespace of a user-defined type.
https://godbolt.org/z/xAFG98

Differential Revision: https://reviews.llvm.org/D72640

Added: 
    

Modified: 
    libcxx/include/functional
    libcxx/test/std/algorithms/alg.nonmodifying/alg.search/search.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/functional b/libcxx/include/functional
index 67be9ee86a64..fa55864bc5d6 100644
--- a/libcxx/include/functional
+++ b/libcxx/include/functional
@@ -3050,14 +3050,14 @@ __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
          forward_iterator_tag, forward_iterator_tag)
 {
     if (__first2 == __last2)
-        return make_pair(__first1, __first1);  // Everything matches an empty sequence
+        return _VSTD::make_pair(__first1, __first1);  // Everything matches an empty sequence
     while (true)
     {
         // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
         while (true)
         {
             if (__first1 == __last1)  // return __last1 if no element matches *__first2
-                return make_pair(__last1, __last1);
+                return _VSTD::make_pair(__last1, __last1);
             if (__pred(*__first1, *__first2))
                 break;
             ++__first1;
@@ -3068,9 +3068,9 @@ __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
         while (true)
         {
             if (++__m2 == __last2)  // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
-                return make_pair(__first1, __m1);
+                return _VSTD::make_pair(__first1, __m1);
             if (++__m1 == __last1)  // Otherwise if source exhaused, pattern not found
-                return make_pair(__last1, __last1);
+                return _VSTD::make_pair(__last1, __last1);
             if (!__pred(*__m1, *__m2))  // if there is a mismatch, restart with a new __first1
             {
                 ++__first1;
@@ -3092,10 +3092,10 @@ __search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
     // Take advantage of knowing source and pattern lengths.  Stop short when source is smaller than pattern
     const _D2 __len2 = __last2 - __first2;
     if (__len2 == 0)
-        return make_pair(__first1, __first1);
+        return _VSTD::make_pair(__first1, __first1);
     const _D1 __len1 = __last1 - __first1;
     if (__len1 < __len2)
-        return make_pair(__last1, __last1);
+        return _VSTD::make_pair(__last1, __last1);
     const _RandomAccessIterator1 __s = __last1 - (__len2 - 1);  // Start of pattern match can't go beyond here
 
     while (true)
@@ -3103,7 +3103,7 @@ __search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
         while (true)
         {
             if (__first1 == __s)
-                return make_pair(__last1, __last1);
+                return _VSTD::make_pair(__last1, __last1);
             if (__pred(*__first1, *__first2))
                 break;
             ++__first1;
@@ -3114,7 +3114,7 @@ __search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
          while (true)
          {
              if (++__m2 == __last2)
-                 return make_pair(__first1, __first1 + __len2);
+                 return _VSTD::make_pair(__first1, __first1 + __len2);
              ++__m1;          // no need to check range on __m1 because __s guarantees we have enough source
              if (!__pred(*__m1, *__m2))
              {

diff  --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.search/search.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.search/search.pass.cpp
index 5aaa832ded02..cb1bd1cf40e9 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.search/search.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.search/search.pass.cpp
@@ -56,6 +56,19 @@ struct MySearcher {
     }
 };
 
+namespace User {
+struct S {
+    S(int x) : x(x) {}
+    int x;
+};
+bool operator==(S lhs, S rhs)
+{
+    return lhs.x == rhs.x;
+}
+template <class T, class U>
+void make_pair(T&&, U&&) = delete;
+} // namespace User
+
 template <class Iter1, class Iter2>
 void
 test()
@@ -95,6 +108,14 @@ test()
     assert(std::search(Iter1(ij), Iter1(ij+sj), Iter2(ik), Iter2(ik+sk)) == Iter1(ij+6));
 }
 
+template <class Iter>
+void
+adl_test()
+{
+    User::S ua[] = {1};
+    assert(std::search(Iter(ua), Iter(ua), Iter(ua), Iter(ua)) == Iter(ua));
+}
+
 int main(int, char**)
 {
     test<forward_iterator<const int*>, forward_iterator<const int*> >();
@@ -107,6 +128,9 @@ int main(int, char**)
     test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >();
     test<random_access_iterator<const int*>, random_access_iterator<const int*> >();
 
+    adl_test<forward_iterator<User::S*> >();
+    adl_test<random_access_iterator<User::S*> >();
+
 #if TEST_STD_VER > 14
 {
     typedef int * RI;


        


More information about the libcxx-commits mailing list