]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
backport: re PR libstdc++/78389 (list::merge and list::sort are not exception safe)
authorVille Voutilainen <ville.voutilainen@gmail.com>
Sun, 15 Jan 2017 16:27:08 +0000 (18:27 +0200)
committerVille Voutilainen <ville@gcc.gnu.org>
Sun, 15 Jan 2017 16:27:08 +0000 (18:27 +0200)
Backport from mainline
2017-01-13  Ville Voutilainen  <ville.voutilainen@gmail.com>

PR libstdc++/78389
* include/bits/list.tcc (merge(list&&)):
Adjust list sizes if the comparator throws.
(merge(list&&, _StrictWeakOrdering)): Likewise.
* testsuite/23_containers/list/operations/78389.cc: New.

From-SVN: r244475

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/list.tcc
libstdc++-v3/testsuite/23_containers/list/operations/78389.cc [new file with mode: 0644]

index 7f9550b958df48f3f46ccb9b7a3a0b83c42b1ab3..2a3c4d53c8e4e6d4722ad5af3c6897c9349f46dc 100644 (file)
@@ -1,3 +1,14 @@
+2017-01-15  Ville Voutilainen  <ville.voutilainen@gmail.com>
+
+       Backport from mainline
+        2017-01-13  Ville Voutilainen  <ville.voutilainen@gmail.com>
+
+       PR libstdc++/78389
+       * include/bits/list.tcc (merge(list&&)):
+       Adjust list sizes if the comparator throws.
+       (merge(list&&, _StrictWeakOrdering)): Likewise.
+       * testsuite/23_containers/list/operations/78389.cc: New.
+
 2017-01-09  Jonathan Wakely  <jwakely@redhat.com>
 
        * testsuite/30_threads/thread/cons/lwg2097.cc: Compile with
index a9c8a550b4ac46a2e377c307795e6f9e0b38f88b..22a5530aa5d65d2de204e976d2485fa456220744 100644 (file)
@@ -338,26 +338,36 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       // 300. list::merge() specification incomplete
       if (this != &__x)
        {
-         _M_check_equal_allocators(__x); 
+         _M_check_equal_allocators(__x);
 
          iterator __first1 = begin();
          iterator __last1 = end();
          iterator __first2 = __x.begin();
          iterator __last2 = __x.end();
-         while (__first1 != __last1 && __first2 != __last2)
-           if (*__first2 < *__first1)
-             {
-               iterator __next = __first2;
-               _M_transfer(__first1, __first2, ++__next);
-               __first2 = __next;
-             }
-           else
-             ++__first1;
-         if (__first2 != __last2)
-           _M_transfer(__last1, __first2, __last2);
+         size_t __orig_size = __x.size();
+         __try {
+           while (__first1 != __last1 && __first2 != __last2)
+             if (*__first2 < *__first1)
+               {
+                 iterator __next = __first2;
+                 _M_transfer(__first1, __first2, ++__next);
+                 __first2 = __next;
+               }
+             else
+               ++__first1;
+           if (__first2 != __last2)
+             _M_transfer(__last1, __first2, __last2);
 
-         this->_M_inc_size(__x._M_get_size());
-         __x._M_set_size(0);
+           this->_M_inc_size(__x._M_get_size());
+           __x._M_set_size(0);
+         }
+         __catch(...)
+           {
+             size_t __dist = distance(__first2, __last2);
+             this->_M_inc_size(__dist);
+             __x._M_set_size(__orig_size - __dist);
+             __throw_exception_again;
+           }
        }
     }
 
@@ -381,20 +391,31 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
            iterator __last1 = end();
            iterator __first2 = __x.begin();
            iterator __last2 = __x.end();
-           while (__first1 != __last1 && __first2 != __last2)
-             if (__comp(*__first2, *__first1))
-               {
-                 iterator __next = __first2;
-                 _M_transfer(__first1, __first2, ++__next);
-                 __first2 = __next;
-               }
-             else
-               ++__first1;
-           if (__first2 != __last2)
-             _M_transfer(__last1, __first2, __last2);
-
-           this->_M_inc_size(__x._M_get_size());
-           __x._M_set_size(0);
+           size_t __orig_size = __x.size();
+           __try
+             {
+               while (__first1 != __last1 && __first2 != __last2)
+                 if (__comp(*__first2, *__first1))
+                   {
+                     iterator __next = __first2;
+                     _M_transfer(__first1, __first2, ++__next);
+                     __first2 = __next;
+                   }
+                 else
+                   ++__first1;
+               if (__first2 != __last2)
+                 _M_transfer(__last1, __first2, __last2);
+
+               this->_M_inc_size(__x._M_get_size());
+               __x._M_set_size(0);
+             }
+           __catch(...)
+             {
+               size_t __dist = distance(__first2, __last2);
+               this->_M_inc_size(__dist);
+               __x._M_set_size(__orig_size - __dist);
+               __throw_exception_again;
+             }
          }
       }
 
diff --git a/libstdc++-v3/testsuite/23_containers/list/operations/78389.cc b/libstdc++-v3/testsuite/23_containers/list/operations/78389.cc
new file mode 100644 (file)
index 0000000..3149a1f
--- /dev/null
@@ -0,0 +1,71 @@
+// { dg-options "-std=gnu++11" }
+
+// Copyright (C) 2017 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/>.
+
+// 23.2.2.4 list operations [lib.list.ops]
+
+#include <testsuite_hooks.h>
+
+#include <list>
+
+struct ThrowingComparator
+{
+    unsigned int throw_after;
+    unsigned int count;
+    bool operator()(int, int) {
+        if (++count >= throw_after) {
+            throw 666;
+        }
+        return true;
+    }
+};
+
+struct X
+{
+  X() = default;
+  X(int) {}
+};
+
+unsigned int throw_after_X = 0;
+unsigned int count_X = 0;
+
+bool operator<(const X&, const X&) {
+  if (++count_X >= throw_after_X) {
+    throw 666;
+  }
+  return true;
+}
+
+
+int main()
+{
+    std::list<int> a{1, 2, 3, 4};
+    std::list<int> b{5, 6, 7, 8, 9, 10, 11, 12};
+    try {
+      a.merge(b, ThrowingComparator{5, 0});
+    } catch (...) {
+    }
+    VERIFY(a.size() == 8 && b.size() == 4);
+    std::list<X> ax{1, 2, 3, 4};
+    std::list<X> bx{5, 6, 7, 8, 9, 10, 11, 12};
+    throw_after_X = 5;
+    try {
+        ax.merge(bx);
+    } catch (...) {
+    }
+}