[PATCH][_GLIBCXX_DEBUG] Enhance detection of invalid iterators usage

François Dumont frs.dumont@gmail.com
Mon Feb 1 18:43:09 GMT 2021


At the moment some iterators like std::list<>::end() looks like 
value-init iterators once detached.

I think using an iterator in such a state is wrong so here is a patch to 
detect this.

This patch also add a new iterator state: singular (value-initialized)

Example of the output of the forward_list/iterator2_neg.cc test:

In function:
     bool __gnu_debug::operator!=(const _Self&, const _Self&)

Error: attempt to compare a singular iterator to a
singular (value-initialized) iterator.

Objects involved in the operation:
     iterator "__lhs" @ 0x0x7fffb777d4b0 {
       type = std::__cxx1998::_Fwd_list_iterator<int> (mutable iterator);
       state = singular;
     }
     iterator "__rhs" @ 0x0x7fffb777d4e0 {
       type = std::__cxx1998::_Fwd_list_iterator<int> (mutable iterator);
       state = singular (value-initialized);
     }

     libstdc++: [_GLIBCXX_DEBUG] Do not consider detach iterators as 
value-initialized

     An attach iterator has its _M_version set to something != 0. This 
value shall be preserved
     when detaching it so that the iterator does not look like a value 
initialized one.

     libstdc++-v3/ChangeLog:

             * include/debug/formatter.h (__singular_value_init): New 
_Iterator_state enum entry.
             (_Parameter<>(const _Safe_iterator<>&, const char*, 
_Is_iterator)): Check if iterator
             parameter is value-initialized.
             (_Parameter<>(const _Safe_local_iterator<>&, const char*, 
_Is_iterator)): Likewise.
             * include/debug/safe_iterator.h 
(_Safe_iterator<>::_M_value_initialized()): New. Adapt
             checks.
             * include/debug/safe_local_iterator.h 
(_Safe_local_iterator<>::_M_value_initialized()): New.
             Adapt checks.
             * src/c++11/debug.cc (_Safe_iterator_base::_M_reset): Do 
not reset _M_version.
             (print_field(PrintContext&, const _Parameter&, const 
char*)): Adapt state_names.
             * testsuite/23_containers/deque/debug/iterator1_neg.cc: New 
test.
             * testsuite/23_containers/deque/debug/iterator2_neg.cc: New 
test.
             * 
testsuite/23_containers/forward_list/debug/iterator1_neg.cc: New test.
             * 
testsuite/23_containers/forward_list/debug/iterator2_neg.cc: New test.

Tested under Linux x86_64.

Ok to commit ?

François

-------------- next part --------------
A non-text attachment was scrubbed...
Name: singular_value_init.patch
Type: text/x-patch
Size: 13825 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20210201/8ec72d60/attachment-0001.bin>


More information about the Libstdc++ mailing list