From 3c760f4a79c221408122b3f265a8039b04ce697e Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Wed, 14 Dec 2016 15:17:57 +0000 Subject: [PATCH] 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/ChangeLog | 9 +++ libstdc++-v3/python/libstdcxx/v6/printers.py | 8 ++- .../libstdc++-prettyprinters/debug.cc | 15 ++++ .../libstdc++-prettyprinters/debug_cxx11.cc | 71 +++++++++++++++++++ 4 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 libstdc++-v3/testsuite/libstdc++-prettyprinters/debug_cxx11.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index b2cd76c96bb..635629b7837 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,12 @@ +2016-12-14 Jonathan Wakely + + 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 * testsuite/21_strings/basic_string/numeric_conversions/char/stod.cc: diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py index ff428e8f7e9..8ac4a3757cb 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 941f2dc0276..cff71132cec 100644 --- a/libstdc++-v3/testsuite/libstdc++-prettyprinters/debug.cc +++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/debug.cc @@ -27,6 +27,7 @@ #include #include #include +#include #include int @@ -90,6 +91,20 @@ main() sll.push_front(47); // { dg-final { note-test sll {__gnu_cxx::slist = {[0] = 47, [1] = 23}} } } + std::vector v; + v.push_back(1); + v.push_back(2); + std::vector::iterator viter0; +// { dg-final { note-test viter0 {invalid iterator} } } + std::vector::iterator viter1 = v.begin(); + std::vector::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::iterator viter3 = v.begin(); +// { dg-final { note-test viter3 {2} } } + __gnu_cxx::slist::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 00000000000..65f187c2ce3 --- /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 +// . + +#define _GLIBCXX_DEBUG + +#include +#include +#include +#include + +int +main() +{ + std::forward_list flst; + std::forward_list::iterator flstiter0; +// { dg-final { note-test flstiter0 {invalid iterator}} } + flst.push_front("dum"); + std::forward_list::iterator flstiter1 = flst.begin(); +// { dg-final { note-test *flstiter1 {"dum"}} } + flst.push_front("dee"); + std::forward_list::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::const_iterator flstciter = flst.begin(); +// { dg-final { note-test *flstciter {"dee"}} } + + std::unordered_map um{ {"zardoz", 23} }; +// { dg-final { note-test um {std::__debug::unordered_map with 1 elements = {["zardoz"] = 23}} } } + + std::unordered_map::iterator umiter = um.begin(); +// { dg-final { note-test umiter->first {"zardoz"} } } + + std::unordered_set us{"barrel"}; +// { dg-final { note-test us {std::__debug::unordered_set with 1 elements = {[0] = "barrel"}} } } + + std::unordered_set::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 } } -- 2.30.2