Add assertion to _Rb_tree::erase to check for end iterators
authorJonathan Wakely <jwakely@redhat.com>
Fri, 16 Dec 2016 18:13:09 +0000 (18:13 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 16 Dec 2016 18:13:09 +0000 (18:13 +0000)
* include/bits/stl_tree.h (_Rb_tree::_M_erase_aux(const_iterator)):
Add assertion for undefined argument.
(_Rb_tree::_M_erase_aux(const_iterator, const_iterator)): Call
_M_erase_aux directly instead of through erase.
(_Rb_tree::_M_erase_aux(const Key&)): Likewise.
* testsuite/23_containers/map/modifiers/erase/end_neg.cc: New test.

From-SVN: r243757

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/stl_tree.h
libstdc++-v3/testsuite/23_containers/map/modifiers/erase/end_neg.cc [new file with mode: 0644]

index a0804e7d92035ba4c654c00119048d52f246f743..0cf14b550ae7b5a8030a8d094cab7e4581edc404 100644 (file)
@@ -1,3 +1,12 @@
+2016-12-16  Jonathan Wakely  <jwakely@redhat.com>
+
+       * include/bits/stl_tree.h (_Rb_tree::_M_erase_aux(const_iterator)):
+       Add assertion for undefined argument.
+       (_Rb_tree::_M_erase_aux(const_iterator, const_iterator)): Call
+       _M_erase_aux directly instead of through erase.
+       (_Rb_tree::_M_erase_aux(const Key&)): Likewise.
+       * testsuite/23_containers/map/modifiers/erase/end_neg.cc: New test.
+
 2016-12-16  Ville Voutilainen  <ville.voutilainen@gmail.com>
 
        Implement LWG 2769, Redundant const in the return type of
index 925066cf4720f827ddb1998d5d0f6644fdd46d49..735fadac75b6fa9dd01d4b6ec6c0b7f18d92fc90 100644 (file)
@@ -1104,6 +1104,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       iterator
       erase(const_iterator __position)
       {
+       __glibcxx_assert(__position != end());
        const_iterator __result = __position;
        ++__result;
        _M_erase_aux(__position);
@@ -1115,6 +1116,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       iterator
       erase(iterator __position)
       {
+       __glibcxx_assert(__position != end());
        iterator __result = __position;
        ++__result;
        _M_erase_aux(__position);
@@ -1123,11 +1125,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #else
       void
       erase(iterator __position)
-      { _M_erase_aux(__position); }
+      {
+       __glibcxx_assert(__position != end());
+       _M_erase_aux(__position);
+      }
 
       void
       erase(const_iterator __position)
-      { _M_erase_aux(__position); }
+      {
+       __glibcxx_assert(__position != end());
+       _M_erase_aux(__position);
+      }
 #endif
       size_type
       erase(const key_type& __x);
@@ -2477,7 +2485,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        clear();
       else
        while (__first != __last)
-         erase(__first++);
+         _M_erase_aux(__first++);
     }
 
   template<typename _Key, typename _Val, typename _KeyOfValue,
@@ -2488,7 +2496,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     {
       pair<iterator, iterator> __p = equal_range(__x);
       const size_type __old_size = size();
-      erase(__p.first, __p.second);
+      _M_erase_aux(__p.first, __p.second);
       return __old_size - size();
     }
 
diff --git a/libstdc++-v3/testsuite/23_containers/map/modifiers/erase/end_neg.cc b/libstdc++-v3/testsuite/23_containers/map/modifiers/erase/end_neg.cc
new file mode 100644 (file)
index 0000000..f01a99b
--- /dev/null
@@ -0,0 +1,35 @@
+// Copyright (C) 2016 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 "-D_GLIBCXX_ASSERTIONS" }
+// { dg-do run { xfail *-*-* } }
+
+#include <map>
+
+void
+test01()
+{
+  std::map<int, int> m;
+  m[0];
+  m.erase(m.end());
+}
+
+int
+main()
+{
+  test01();
+}