Make printers detect invalid debug mode iterators
authorJonathan Wakely <jwakely@redhat.com>
Wed, 14 Dec 2016 15:17:57 +0000 (15:17 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Wed, 14 Dec 2016 15:17:57 +0000 (15:17 +0000)
PR libstdc++/59170
* python/libstdcxx/v6/printers.py (StdDebugIteratorPrinter): Use
_M_sequence and _M_version to detect invalid iterators.
* testsuite/libstdc++-prettyprinters/debug.cc: Test debug mode vector
and test invalid iterators.
* testsuite/libstdc++-prettyprinters/debug_cxx11.cc: New test.

From-SVN: r243650

libstdc++-v3/ChangeLog
libstdc++-v3/python/libstdcxx/v6/printers.py
libstdc++-v3/testsuite/libstdc++-prettyprinters/debug.cc
libstdc++-v3/testsuite/libstdc++-prettyprinters/debug_cxx11.cc [new file with mode: 0644]

index b2cd76c96bb5b7ca0a9ab7050d5910efb1014663..635629b78378fdac184138aea261e6be526d9432 100644 (file)
@@ -1,3 +1,12 @@
+2016-12-14  Jonathan Wakely  <jwakely@redhat.com>
+
+       PR libstdc++/59170
+       * python/libstdcxx/v6/printers.py (StdDebugIteratorPrinter): Use
+       _M_sequence and _M_version to detect invalid iterators.
+       * testsuite/libstdc++-prettyprinters/debug.cc: Test debug mode vector
+       and test invalid iterators.
+       * testsuite/libstdc++-prettyprinters/debug_cxx11.cc: New test.
+
 2016-12-12  Kyrylo Tkachov  <kyrylo.tkachov@arm.com>
 
        * testsuite/21_strings/basic_string/numeric_conversions/char/stod.cc:
index ff428e8f7e9a93fc501af24f7ffaa8cdaa01b787..8ac4a3757cb12bb7e28052e274ec7039a8db6001 100644 (file)
@@ -501,6 +501,10 @@ class StdDebugIteratorPrinter:
     # Just strip away the encapsulating __gnu_debug::_Safe_iterator
     # and return the wrapped iterator value.
     def to_string (self):
+        base_type = gdb.lookup_type('__gnu_debug::_Safe_iterator_base')
+        safe_seq = self.val.cast(base_type)['_M_sequence']
+        if not safe_seq or self.val['_M_version'] != safe_seq['_M_version']:
+            return "invalid iterator"
         itype = self.val.type.template_argument(0)
         return self.val.cast(itype)
 
@@ -1507,8 +1511,8 @@ def build_libstdcxx_dictionary ():
                                   Tr1UnorderedSetPrinter)
 
     # These are the C++11 printer registrations for -D_GLIBCXX_DEBUG cases.
-    # The tr1 namespace printers do not seem to have any debug
-    # equivalents, so do no register them.
+    # The tr1 namespace containers do not have any debug equivalents,
+    # so do not register printers for them.
     libstdcxx_printer.add('std::__debug::unordered_map',
                           Tr1UnorderedMapPrinter)
     libstdcxx_printer.add('std::__debug::unordered_set',
index 941f2dc0276fa0f36618433d681c63b5543a6a52..cff71132ceca60442166af2cbfdb879e3bac8272 100644 (file)
@@ -27,6 +27,7 @@
 #include <list>
 #include <map>
 #include <set>
+#include <vector>
 #include <ext/slist>
 
 int
@@ -90,6 +91,20 @@ main()
   sll.push_front(47);
 // { dg-final { note-test sll {__gnu_cxx::slist = {[0] = 47, [1] = 23}} } }
 
+  std::vector<int> v;
+  v.push_back(1);
+  v.push_back(2);
+  std::vector<int>::iterator viter0;
+// { dg-final { note-test viter0 {invalid iterator} } }
+  std::vector<int>::iterator viter1 = v.begin();
+  std::vector<int>::iterator viter2 = viter1 + 1;
+  v.erase(viter1);
+// { dg-final { note-test v {std::__debug::vector of length 1, capacity 2 = {2}} } }
+// { dg-final { note-test viter1 {invalid iterator} } }
+// { dg-final { note-test viter2 {invalid iterator} } }
+  std::vector<int>::iterator viter3 = v.begin();
+// { dg-final { note-test viter3 {2} } }
+
   __gnu_cxx::slist<int>::iterator slliter = sll.begin();
 // { dg-final { note-test slliter {47} } }
 
diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/debug_cxx11.cc b/libstdc++-v3/testsuite/libstdc++-prettyprinters/debug_cxx11.cc
new file mode 100644 (file)
index 0000000..65f187c
--- /dev/null
@@ -0,0 +1,71 @@
+// { dg-do run { target c++11 } }
+// { dg-options "-g -O0" }
+
+// 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/>.
+
+#define _GLIBCXX_DEBUG
+
+#include <forward_list>
+#include <unordered_map>
+#include <unordered_set>
+#include <iostream>
+
+int
+main()
+{
+  std::forward_list<std::string> flst;
+  std::forward_list<std::string>::iterator flstiter0;
+// { dg-final { note-test flstiter0 {invalid iterator}} }
+  flst.push_front("dum");
+  std::forward_list<std::string>::iterator flstiter1 = flst.begin();
+// { dg-final { note-test *flstiter1 {"dum"}} }
+  flst.push_front("dee");
+  std::forward_list<std::string>::iterator flstiter2 = flst.begin();
+// { dg-final { note-test *flstiter2 {"dee"}} }
+// { dg-final { note-test flst {std::__debug::forward_list = {[0] = "dee", [1] = "dum"}} } }
+
+  std::forward_list<std::string>::const_iterator flstciter = flst.begin();
+// { dg-final { note-test *flstciter {"dee"}} }
+
+  std::unordered_map<std::string, int> um{ {"zardoz", 23} };
+// { dg-final { note-test um {std::__debug::unordered_map with 1 elements = {["zardoz"] = 23}} } }
+
+  std::unordered_map<std::string, int>::iterator umiter = um.begin();
+// { dg-final { note-test umiter->first {"zardoz"} } }
+
+  std::unordered_set<std::string> us{"barrel"};
+// { dg-final { note-test us {std::__debug::unordered_set with 1 elements = {[0] = "barrel"}} } }
+
+  std::unordered_set<std::string>::const_iterator usciter = us.begin();
+// { dg-final { note-test *usciter {"barrel"} } }
+
+  // N.B. printers.py does not define printers for the iterator types
+  // that belong to C++11 containers, so tests above dereference the
+  // iterators, and to make that work we need to ensure the operator
+  // definitions are in the debug info:
+  std::string tem;
+  tem = *flstiter1;
+  tem = *flstciter;
+  tem = umiter->first;
+  tem = *usciter;
+
+  std::cout << "\n";
+  return 0;                    // Mark SPOT
+}
+
+// { dg-final { gdb-test SPOT } }