https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121598
Bug ID: 121598 Summary: Missing precondition checks in copy/copy_backward/move/move_backward algos Product: gcc Version: 16.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: redi at gcc dot gnu.org Target Milestone: --- #define _GLIBCXX_DEBUG #include <algorithm> #include <vector> int main() { std::vector<int> v(1); std::copy(v.begin(), v.end(), v.begin()); // 1a std::move(v.begin(), v.end(), v.begin()); // 1b std::copy_backward(v.begin(), v.end(), v.end()); // 2a std::move_backward(v.begin(), v.end(), v.end()); // 2b } All of the calls to algos here are undefined due to violating this for 1a and 1b: Preconditions: result is not in the range [first, last). and this for 2a and 2b: Preconditions: result is not in the range (first, last]. For random access iterators we can check the full preconditions, for non-random access maybe for _GLIBCXX_ASSERTIONS we should only to check that result != first for copy and move and result != last for copy_backward and move_backward. For _GLIBCXX_DEBUG we can check the full precondition for non-input iterators. Obviously we can only check the preconditions when equality is well-formed and meaningful for the input and output iterators, so maybe only when they're similar types (although ideally we want vector<T>::iterator and vector<T>::const_iterator to be checked).