Avoid a potential underflow when iterating over invalid inputs

The forward iteration logic already bounds-check for m_it != m_string->end(),
do the same for the backward iteration. The issue with the assert is
that the assert() might not be compiled in, and it is happening after
the dereference, so it was too late.
This commit is contained in:
Dirk Müller
2026-05-14 10:58:09 +02:00
committed by Martin Hořeňovský
parent 6ee0826dca
commit 69e0473f6e
2 changed files with 21 additions and 2 deletions
@@ -437,6 +437,25 @@ TEST_CASE( "TextFlow::AnsiSkippingString iterates UTF-8 codepoints",
}
}
TEST_CASE( "TextFlow::AnsiSkippingString handles invalid UTF-8",
"[TextFlow][ansiskippingstring][approvals]" ) {
SECTION( "Continuation byte at the start" ) {
// 0x80 is a continuation byte
AnsiSkippingString str( "\x80" );
auto it = str.end();
--it;
CHECK( it == str.begin() );
CHECK( *it == static_cast<char>( 0x80 ) );
}
SECTION( "Multiple continuation bytes at the start" ) {
AnsiSkippingString str( "\x80\x80\x80" );
auto it = str.end();
--it;
CHECK( it == str.begin() );
CHECK( *it == static_cast<char>( 0x80 ) );
}
}
TEST_CASE( "TextFlow::Column wraps UTF-8 text correctly",
"[TextFlow][column][approvals]" ) {
// "äöü äöü äöü" = 11 codepoints, 17 bytes