]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix reverse iteration in _Utf16_view
authorJonathan Wakely <jwakely@redhat.com>
Fri, 10 Oct 2025 22:56:43 +0000 (23:56 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Mon, 13 Oct 2025 10:04:28 +0000 (11:04 +0100)
When iterating over a range of char16_t in reverse the _Utf_view was
incorrectly treating U+DC00 as a valid high surrogate that can precede
the low surrogate. But U+DC00 is a low surrogate, and so should not be
allowed before another low surrogate. The check should be u2 >= 0xDC00
rather than u2 > 0xDC00.

libstdc++-v3/ChangeLog:

* include/bits/unicode.h (_Utf_view::_M_read_reverse_utf16):
Fix check for high surrogate preceding low surrogate.
* testsuite/ext/unicode/view.cc: Check unpaired low surrogates.

Reviewed-by: Tomasz KamiƄski <tkaminsk@redhat.com>
libstdc++-v3/include/bits/unicode.h
libstdc++-v3/testsuite/ext/unicode/view.cc

index 00efbe89ca8e117a8a943734760dd95abd4c8200..44872db4ed68a4287e98ffb46822117918b13092 100644 (file)
@@ -527,7 +527,7 @@ namespace __unicode
          {
            // read a low surrogate, expect a high surrogate before it.
            uint16_t __u2 = *--_M_curr();
-           if (__u2 < 0xD800 || __u2 > 0xDC00) [[unlikely]]
+           if (__u2 < 0xD800 || __u2 >= 0xDC00) [[unlikely]]
              __c = _S_error(); // unpaired low surrogate
            else
              {
index 4ccf646e09459f3ee6c83b96e560390ad021106d..40c8fcf34fb761aff80400f84416fec90aff5d22 100644 (file)
@@ -94,6 +94,11 @@ test_illformed_utf16()
   compare(uc::_Utf16_view(s4), u"\uFFFD\uFFFD"sv);
   std::array s5{ s[1], s[0], s[1] };
   compare(uc::_Utf16_view(s5), u"\uFFFD\N{CLOWN FACE}"sv);
+
+  std::array<char16_t, 2> s6{ 0xDC00, 0xDC01 };
+  compare(uc::_Utf16_view(s6), u"\uFFFD\uFFFD"sv);
+  std::array<char16_t, 2> s7{ 0xD7FF, 0xDC00 };
+  compare(uc::_Utf16_view(s7), u"\uD7FF\uFFFD"sv);
 }
 
 constexpr void