Fix condition for trailing bytes count in UTF-8 decoder. Test added to check if a replacement characters is added to output.

This commit is contained in:
Jan Wojciechowski 2023-03-05 09:40:34 +01:00 committed by Lukas Dürrenberger
parent 230c6a4d57
commit 692fe84331
2 changed files with 25 additions and 13 deletions

View File

@ -69,7 +69,7 @@ In Utf<8>::decode(In begin, In end, std::uint32_t& output, std::uint32_t replace
// decode the character // decode the character
int trailingBytes = trailing[static_cast<std::uint8_t>(*begin)]; int trailingBytes = trailing[static_cast<std::uint8_t>(*begin)];
if (begin + trailingBytes < end) if (trailingBytes < std::distance(begin, end))
{ {
output = 0; output = 0;

View File

@ -229,6 +229,8 @@ TEST_CASE("[System] sf::String")
} }
SUBCASE("fromUtf8()") SUBCASE("fromUtf8()")
{
SUBCASE("Nominal")
{ {
constexpr std::array<std::uint8_t, 4> characters{'w', 'x', 'y', 'z'}; constexpr std::array<std::uint8_t, 4> characters{'w', 'x', 'y', 'z'};
const sf::String string = sf::String::fromUtf8(characters.begin(), characters.end()); const sf::String string = sf::String::fromUtf8(characters.begin(), characters.end());
@ -244,6 +246,16 @@ TEST_CASE("[System] sf::String")
CHECK(string.getData() != nullptr); CHECK(string.getData() != nullptr);
} }
SUBCASE("Insufficient input")
{
constexpr std::array<std::uint8_t, 1> characters{251};
const sf::String string = sf::String::fromUtf8(characters.begin(), characters.end());
constexpr char32_t defaultReplacementCharacter = 0;
CHECK(string.getSize() == 1);
CHECK(string[0] == defaultReplacementCharacter);
}
}
SUBCASE("fromUtf16()") SUBCASE("fromUtf16()")
{ {
constexpr std::array<std::uint16_t, 4> characters{0xF1, 'x', 'y', 'z'}; constexpr std::array<std::uint16_t, 4> characters{0xF1, 'x', 'y', 'z'};