https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90857

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This is definitely undefined. The range (position,last) goes from
one-past-position to one-before-last, but that's an invalid range. The iterator
one-before-last is not reachable from one-past-position.

We could stop it crashing, with something like this:

--- a/libstdc++-v3/include/bits/forward_list.h
+++ b/libstdc++-v3/include/bits/forward_list.h
@@ -1016,10 +1017,14 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        */
       iterator
       erase_after(const_iterator __pos, const_iterator __last)
-      { return iterator(this->_M_erase_after(const_cast<_Node_base*>
+      {
+       if (__builtin_expect(__pos == __last, 0))
+         return iterator(const_cast<_Node_base*>(__last._M_node));
+       return iterator(this->_M_erase_after(const_cast<_Node_base*>
                                             (__pos._M_node),
                                             const_cast<_Node_base*>
-                                            (__last._M_node))); }
+                                            (__last._M_node)));
+      }

       /**

But I'm not sure if we want to add extra code that slows down valid code to
defend against invalid code.

In any case, we want to keep the debug mode assertion which aborts when this
happens.

Reply via email to