From 3c760f4a79c221408122b3f265a8039b04ce697e Mon Sep 17 00:00:00 2001
From: Jonathan Wakely <jwakely@redhat.com>
Date: Wed, 14 Dec 2016 15:17:57 +0000
Subject: Make printers detect invalid debug mode iterators

	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/python/libstdcxx/v6/printers.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

(limited to 'libstdc++-v3/python')

diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py
index ff428e8..8ac4a37 100644
--- a/libstdc++-v3/python/libstdcxx/v6/printers.py
+++ b/libstdc++-v3/python/libstdcxx/v6/printers.py
@@ -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',
-- 
cgit v1.1