https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127540
Bug ID: 127540
Summary: std::swap(i, i) with debug iterators aborts
Product: gcc
Version: 14.3.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: antti at nietosvaara dot fi
Target Milestone: ---
It seems that swapping an iterator with itself in debug mode causes SIGABRT:
#define _GLIBCXX_DEBUG
#include <vector>
int main() {
std::vector<int> v{1};
auto i = v.begin();
std::swap(i, i);
}
Program returned: 134
Program stderr
/cefs/69/692c84ccdcad0e663cd73918_gcc-trunk-20260920/include/c++/17.0.0/debug/safe_iterator.h:307:
In function:
constexpr gnu_debug::_Safe_iterator<_Iterator, _Sequence, _Category>&
gnu_debug::_Safe_iterator<_Iterator, _Sequence, _Category>::operator=(
gnu_debug::_Safe_iterator<_Iterator, _Sequence, _Category>&&) [with
_Iterator = gnu_cxx::normal_iterator<int*, std::vector<int,
std::allocator<int> > >; _Sequence = std::debug::vector<int>; _Category
= std::forward_iterator_tag]
Error: attempt to copy from a singular iterator.
Objects involved in the operation:
iterator "this" @ 0x7ffda56bda80 {
type = gnu_cxx::normal_iterator<int*, std::vector<int,
std::allocator<int> > > (mutable iterator);
state = singular;
}
iterator "other" @ 0x7ffda56bda80 {
type = gnu_cxx::normal_iterator<int*, std::vector<int,
std::allocator<int> > > (mutable iterator);
state = singular;
}
Program terminated with signal SIGABRT (6)
Reproduced in godbolt.org using x86-64 gcc (trunk) and in AlmaLinux 10 with gcc
14.3.1 20251022 (Red Hat 14.3.1-4).