]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix typos in iterator increment for std::text_encoding [PR117520]
authorJonathan Wakely <jwakely@redhat.com>
Mon, 11 Nov 2024 11:54:00 +0000 (11:54 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Tue, 12 Nov 2024 09:33:51 +0000 (09:33 +0000)
The intended behaviour for std::text_encoding::aliases_view's iterator
is that it incrementing or decrementing too far sets it to a
value-initialized state, or fails an assertion when those are enabled.
There were typos that used == instead of = which meant that instead of
becoming singular or aborting, an out-of-range increment just did
nothing. This meant erroneous operations were well-defined and didn't
produce any undefined behaviour, but were not diagnosed with assertions
enabled, as had been intended.

This change fixes the bugs and adds more tests to verify the intended
behaviour.

libstdc++-v3/ChangeLog:

PR libstdc++/117520
* include/std/text_encoding (aliases_view:_Iterator::operator+=):
Fix typos that caused == to be used instead of =.
(aliases_view::_Iterator): Fix friend declaration.
* testsuite/std/text_encoding/members.cc: Adjust expected
behaviour of invalid subscript. Add tests for other erroneous
operations on iterators.

libstdc++-v3/include/std/text_encoding
libstdc++-v3/testsuite/std/text_encoding/members.cc

index 83d023bc71bd78ed7ba388645c26f67581e98f40..515f6e52345f1b10e7660d3d61498d8745d8922d 100644 (file)
@@ -573,7 +573,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
                          && _M_rep[__n - 1]._M_id == _M_id) [[likely]]
                 _M_rep += __n;
              else
-               *this == _Iterator{};
+               *this = _Iterator{};
            }
          else if (__n < 0)
            {
@@ -581,7 +581,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
                    && _M_rep[__n]._M_id == _M_id) [[likely]]
                 _M_rep += __n;
              else
-               *this == _Iterator{};
+               *this = _Iterator{};
            }
        }
       if (__n != 0)
@@ -643,7 +643,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     }
 
   private:
-    friend class text_encoding;
+    friend struct text_encoding;
 
     constexpr explicit
     _Iterator(const _Rep* __r) noexcept
index adbd74ab85ea52c582c78ffc4547965797ba4797..253653250c8aee437e106cddc674695d6144bb71 100644 (file)
@@ -70,10 +70,25 @@ test_every_id()
     auto end = aliases.end();
     VERIFY( (begin + std::ranges::distance(aliases)) == end );
 #ifndef _GLIBCXX_ASSERTIONS
-    // This is an error, but with assertions disabled is guaranteed safe:
+    // These ops violate preconditions, but as libstdc++ extensions they are
+    // guaranteed to either assert or have well-defined behaviour.
+
+    // This erroneously returns ""sv:
     VERIFY( begin[std::ranges::distance(aliases)] == ""sv );
     // Likewise:
-    VERIFY( begin[999999] == *begin );
+    VERIFY( begin[999999] == ""sv );
+
+    auto iter = begin;
+    std::ranges::advance(iter, end);
+    // Erroneously sets iter to a value-initialized state.
+    ++iter;
+    VERIFY( iter == decltype(iter){} );
+    VERIFY( *iter == ""sv );
+
+    iter = begin;
+    // Erroneously sets iter to a value-initialized state.
+    --iter;
+    VERIFY( iter == decltype(iter){} );
 #endif
   }
 }