public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Make printers detect invalid debug mode iterators
@ 2016-12-14 15:26 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2016-12-14 15:26 UTC (permalink / raw)
  To: libstdc++, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 669 bytes --]

This is a partial fix for PR 59170, using the debug mode iterator's
members to tell when it's invalid. This doesn't detect past-the-end
iterators, and doesn't help for normal mode where we don't have the
extra info (but we can still detect value-initialized iterators for
normal mode and not ry to dereference them).

	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.

Tested x86_64-linux, committed to trunk.



[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 6040 bytes --]

commit 8f761c6a07211a8dc7e56e7a5fdd1cf4799bb08a
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Dec 14 14:14:59 2016 +0000

    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.

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',
diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/debug.cc b/libstdc++-v3/testsuite/libstdc++-prettyprinters/debug.cc
index 941f2dc..cff7113 100644
--- a/libstdc++-v3/testsuite/libstdc++-prettyprinters/debug.cc
+++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/debug.cc
@@ -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
index 0000000..65f187c
--- /dev/null
+++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/debug_cxx11.cc
@@ -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 } }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2016-12-14 15:16 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-14 15:26 [PATCH] Make printers detect invalid debug mode iterators Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).