On Wed, 4 Nov 2026 at 20:59, François Dumont <[email protected]> wrote:
>
> Hi
>
> Here is what I propose to do.
>
>      libstdc++: Fix _Safe_iterator copy/move semantic [PR127540]
>
>      Currently our safe iterators for debug mode fail to meet the
> requirement
>      that they have a "valid but unspecified" state after being moved from.
>      They have a singular and non-copyable state, which means they cannot be
>      copied or passed around by value. This is non-conforming.
>
>      Define the moved-from state for our safe iterators to be the same as
>      their value-initialized state. This means they are copyable after being
>      moved from.
>
>      The downside of this change is that we no longer diagnose equality
>      comparison with a moved-from iterator, which is undefined behaviour.
>      Because value-initialized iterators can be compared, our moved-from
>      iterators are now also comparable. This seems like an acceptable
>      compromise in order to support copying them (which needs to work).
>
>      If we care about diagnosing invalid comparisons, we would need to
>      introduce a new moved-from state to the iterators. That could then be
>      checked in the iterator's constructors and assignment operators so that
>      moved-from iterators are allowed as the source of those operations. The
>      new state would not be checked in comparisons so that using them would
>      abort, as was the case before this commit, e.g.
>
> /home/jwakely/gcc/17/include/c++/17.0.0/debug/safe_iterator.h:546:
>        In function:
>            constexpr bool gnu_debug::operator==(const
> _Safe_iterator<gnu_cxx::
>            normal_iterator<int*, std::vector<int, std::allocator<int> >
>  >, std::
>            debug::vector<int>, std::forward_iterator_tag>::_Self&, const
>            _Safe_iterator<gnu_cxx::normal_iterator<int*, std::vector<int,
>            std::allocator<int> > >, std::debug::vector<int>,
>            std::forward_iterator_tag>::_Self&)
>
>        Error: attempt to compare a singular iterator to a
> dereferenceable iterator.
>
>      Reviewing this behavior also implement properly all the possible
> copy/move
>      instantiations or assignment operations so that only the necessary
> is done.

What does "only the necessary" mean? This is a fairly large and
complex patch which does a lot more than just removing some bad
assertions.

This definitely isn't suitable for backporting, so what would be the
minimal fix suitable for backporting? Your earlier patch?


>      This is a good optimization allowing several tests timeout-ing on
> build to now pass.
>
>      Those operations are also, when involving a singular iterator,
> properly copying
>      the source state so that using the resulting iterator will emit the
> same assertion
>      as if the original iterator had been used.
>
>      libstdc++-v3/ChangeLog:
>
>              PR libstdc++/127540
>              * include/debug/safe_base.h
>              (_Safe_iterator_base): Remove implicit C++20 constexpr
> qualification.
>              (_Safe_iterator_base::operator=(const
> _Safe_iterator_base&)): New, default.
>              (_Safe_iterator_base::_M_get_sequence): New, get sequence
> pointer if not singular.
>              (_Safe_iterator_base::_M_inplace_attach): New.
>              (_Safe_iterator_base(const _Safe_iterator_base&, bool)):
> Only copy input
>              _Safe_iterator_base _M_sequence and _M_version if singular
> without attaching.
>              * include/debug/safe_iterator.h:
>              (_Safe_iterator(const _Safe_iterator&)): Forward to latter
> _Safe_iterator_base
>              constructor.
>              (_Safe_iterator<_MutableIte>(const _Safe_iterator<>&)):
> Likewise.
>              (_Safe_iterator): Add friend declaration to access mutable
> iterator members
>              from the immutable type.
>              (_Safe_iterator::_M_copy_state): New.
>              (_Safe_iterator::_M_move): New, make moved instance
> value-initialized.
>              (_Safe_iterator(_Safe_iterator&&, bool)): New.
>              (_Safe_iterator(_Safe_iterator&&)): Forward to latter.
> (_Safe_iterator<_MutableIte>(_Safe_iterator<_MutableIte, _Seq, _Cat>&&)):
>              Likewise.
>              (_Safe_iterator::_M_copy_assign<_Ite>): New.
>              (_Safe_iterator::operator=(const _Safe_iterator&)): Call
> latter.
>              (_Safe_iterator::operator=<_MutableIte>(const
> _Safe_iterator<>&)): New, likewise.
>              (_Safe_iterator::_M_move_assign): New.
>              (_Safe_iterator::operator=(_Safe_iterator&&)): Call latter.
> (_Safe_iterator::operator=<_MutableIte>(_Safe_iterator<>&&)): New, likewise.
>              (_Safe_iterator::_M_attach): Remove.
>              (_Safe_iterator::_M_attach_single): Remove.
>              * src/c++11/debug.cc (_Safe_iterator_base::_M_attach): Do
> not reset _M_version
>              if attaching to a null sequence.
>              * testsuite/24_iterators/debug/safe_iterator.cc: New test.
>
> 23_containers/vector/* tested with _GLIBCXX_DEBUG mode.
>
> Ok to commit after C++98 tests run for all 23_containers tests ?
>
> François
>
> On 10/29/26 23:05, François Dumont wrote:
> > Hi
> >
> > Here is what I've come up with so far.
> >
> > This is clearly not limited to fix of PR 127540 but it does fix it.
> >
> > This is a big performance improvement for the _GLIBCXX_DEBUG mode. The
> > proof is that I managed to run testsuite/23_containers/flat_* tests.
> > They were all timeout-ing on my laptop without the patch.
> >
> > I still need to work on the ChangeLog entry which I copied from your
> > RFC Jonathan.
> >
> > I've also prepared the same for _Safe_local_iterator.
> >
> > Happy to have some feedback.
> >
> >     libstdc++: Fix _Safe_iterator copy/move semantic PR 127540
> >
> >     Currently our safe iterators for debug mode fail to meet the
> > requirement
> >     that they have a "valid but unspecified" state after being moved
> > from.
> >     They have a singular and non-copyable state, which means they
> > cannot be
> >     copied or passed around by value. This is non-conforming.
> >
> >     Define the moved-from state for our safe iterators to be the same as
> >     their value-initialized state. This means they are copyable after
> > being
> >     moved from.
> >
> >     The downside of this change is that we no longer diagnose equality
> >     comparison with a moved-from iterator, which is undefined behaviour.
> >     Because value-initialized iterators can be compared, our moved-from
> >     iterators are now also comparable. This seems like an acceptable
> >     compromise in order to support copying them (which needs to work).
> >
> >     If we care about diagnosing invalid comparisons, we would need to
> >     introduce a new moved-from state to the iterators. That could then be
> >     checked in the iterator's constructors and assignment operators so
> > that
> >     moved-from iterators are allowed as the source of those
> > operations. The
> >     new state would not be checked in comparisons so that using them
> > would
> >     abort, as was the case before this commit, e.g.
> >
> > /home/jwakely/gcc/17/include/c++/17.0.0/debug/safe_iterator.h:546:
> >       In function:
> >           constexpr bool gnu_debug::operator==(const
> > _Safe_iterator<gnu_cxx::
> >           normal_iterator<int*, std::vector<int, std::allocator<int> >
> > >, std::
> >           debug::vector<int>, std::forward_iterator_tag>::_Self&, const
> >           _Safe_iterator<gnu_cxx::normal_iterator<int*, std::vector<int,
> >           std::allocator<int> > >, std::debug::vector<int>,
> >           std::forward_iterator_tag>::_Self&)
> >
> >       Error: attempt to compare a singular iterator to a
> > dereferenceable iterator.
> >
> >     libstdc++-v3/ChangeLog:
> >
> >             PR libstdc++/127540
> >             * config/abi/pre/gnu.ver (GLIBCXX_3.4.37): Add
> > _Safe_sequence_base::_M_attach
> >             and _Safe_sequence_base::_M_attach_single symbols so that
> > freshly instantiated
> >             safe iterators can be attached without going through a
> > detach attempt.
> >             * include/debug/safe_base.h
> >             (_Safe_iterator_base): Remove implicit C++20 constexpr
> > qualification.
> >             (_Safe_iterator_base::operator=(const
> > _Safe_iterator_base&)): New, default.
> >             (_Safe_iterator_base::_M_get_sequence): New, get sequence
> > pointer if not singular.
> >             (_Safe_iterator_base::_M_attach_only): New.
> >             (_Safe_iterator_base(const _Safe_sequence_base*, bool)):
> > Use latter.
> >             (_Safe_iterator_base(const _Safe_iterator_base&, bool)):
> > Likewise and only copy
> >             input _Safe_iterator_base _M_sequence and _M_version if
> > singular.
> >             * include/debug/safe_iterator.h:
> >             (_Safe_iterator(const _Safe_iterator&)): Forward to latter
> > _Safe_iterator_base
> >             constructor.
> >             (_Safe_iterator<_MutableIte>(const _Safe_iterator<>&)):
> > Likewise.
> >             (_Safe_iterator): Add friend declaration to access mutable
> > iterator members
> >             from the immutable type.
> >             (_Safe_iterator::_M_move): New, make moved instance
> > value-initialized.
> >             (_Safe_iterator(_Safe_iterator&&, bool)): New.
> >             (_Safe_iterator(_Safe_iterator&&)): Forward to latter.
> > (_Safe_iterator<_MutableIte>(_Safe_iterator<_MutableIte, _Seq, _Cat>&&)):
> >             Likewise.
> >             (_Safe_iterator::_M_copy_assign<_Ite>): New.
> >             (_Safe_iterator::operator=(const _Safe_iterator&)): Call
> > latter.
> >             (_Safe_iterator::operator=<_MutableIte>(const
> > _Safe_iterator<>&)): New, likewise.
> >             (_Safe_iterator::_M_move_assign): New.
> >             (_Safe_iterator::operator=(_Safe_iterator&&)): Call latter.
> > (_Safe_iterator::operator=<_MutableIte>(_Safe_iterator<>&&)): New,
> > likewise.
> >             (_Safe_iterator::_M_attach): Remove.
> >             (_Safe_iterator::_M_attach_single): Remove.
> >             * src/c++11/debug.cc (_Safe_iterator_base::_M_attach): Do
> > not reset _M_version
> >             if attaching to a null sequence.
> >             (_Safe_iterator_base::_M_attach_single): Likewise.
> >             * testsuite/24_iterators/debug/safe_iterator.cc: New test.
> >
> > Tested under Linux x86_64 23_containers and 24_iterators tests.
> >
> > François

Reply via email to