libstdc++: Fix reverse iteration in _Utf16_view

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>
This commit is contained in:
Jonathan Wakely 2025-10-10 23:56:43 +01:00 committed by Jonathan Wakely
parent fc74f4f0a2
commit 8adda95093
No known key found for this signature in database
2 changed files with 6 additions and 1 deletions

View 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
{

View 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