[libc] Fixed StringConverter Error Edge Case (#149356)

Fixed StringConverter edge case related to destination limit

If we call pop() but there is no space in the dest array, we should
always return the "no space in destination" error even if the following
character is invalid (since we shouldn't really have to look at the next
character)
This commit is contained in:
Uzair Nawaz 2025-07-17 10:32:11 -07:00 committed by GitHub
parent 13549fd90a
commit 6f28eec6dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 63 additions and 0 deletions

View File

@ -56,6 +56,9 @@ public:
// TODO: following functions are almost identical
// look into templating CharacterConverter pop functions
ErrorOr<char32_t> popUTF32() {
if (num_to_write == 0)
return Error(-1);
if (cr.isEmpty() || src_idx == 0) {
auto src_elements_read = pushFullCharacter();
if (!src_elements_read.has_value())
@ -79,6 +82,9 @@ public:
}
ErrorOr<char8_t> popUTF8() {
if (num_to_write == 0)
return Error(-1);
if (cr.isEmpty() || src_idx == 0) {
auto src_elements_read = pushFullCharacter();
if (!src_elements_read.has_value())

View File

@ -245,6 +245,63 @@ TEST(LlvmLibcStringConverterTest, UTF8To32ErrorHandling) {
ASSERT_EQ(static_cast<int>(sc.getSourceIndex()), 4);
}
TEST(LlvmLibcStringConverterTest, InvalidCharacterOutsideBounds) {
// if an invalid character exists in the source string but we don't have space
// to write it, we should return a "stop converting" error rather than an
// invalid character error
// first 4 bytes are clown emoji (🤡)
// next 3 form an invalid character
const char *src1 = "\xF0\x9F\xA4\xA1\x90\x88\x30";
LIBC_NAMESPACE::internal::mbstate ps1;
LIBC_NAMESPACE::internal::StringConverter<char8_t> sc1(
reinterpret_cast<const char8_t *>(src1), &ps1, 1);
auto res1 = sc1.popUTF32();
ASSERT_TRUE(res1.has_value());
ASSERT_EQ(static_cast<int>(res1.value()), 0x1f921);
ASSERT_EQ(static_cast<int>(sc1.getSourceIndex()), 4);
res1 = sc1.popUTF32();
ASSERT_FALSE(res1.has_value());
// no space to write error NOT invalid character error (EILSEQ)
ASSERT_EQ(static_cast<int>(res1.error()), -1);
ASSERT_EQ(static_cast<int>(sc1.getSourceIndex()), 4);
const wchar_t src2[] = {
static_cast<wchar_t>(0x1f921), static_cast<wchar_t>(0xffffff),
static_cast<wchar_t>(0x0)}; // clown emoji, invalid utf32
LIBC_NAMESPACE::internal::mbstate ps2;
LIBC_NAMESPACE::internal::StringConverter<char32_t> sc2(
reinterpret_cast<const char32_t *>(src2), &ps2, 4);
auto res2 = sc2.popUTF8();
ASSERT_TRUE(res2.has_value());
ASSERT_EQ(static_cast<int>(res2.value()), 0xF0);
ASSERT_EQ(static_cast<int>(sc2.getSourceIndex()), 1);
res2 = sc2.popUTF8();
ASSERT_TRUE(res2.has_value());
ASSERT_EQ(static_cast<int>(res2.value()), 0x9F);
ASSERT_EQ(static_cast<int>(sc2.getSourceIndex()), 1);
res2 = sc2.popUTF8();
ASSERT_TRUE(res2.has_value());
ASSERT_EQ(static_cast<int>(res2.value()), 0xA4);
ASSERT_EQ(static_cast<int>(sc2.getSourceIndex()), 1);
res2 = sc2.popUTF8();
ASSERT_TRUE(res2.has_value());
ASSERT_EQ(static_cast<int>(res2.value()), 0xA1);
ASSERT_EQ(static_cast<int>(sc2.getSourceIndex()), 1);
res2 = sc2.popUTF8();
ASSERT_FALSE(res2.has_value());
// no space to write error NOT invalid character error (EILSEQ)
ASSERT_EQ(static_cast<int>(res2.error()), -1);
ASSERT_EQ(static_cast<int>(sc2.getSourceIndex()), 1);
}
TEST(LlvmLibcStringConverterTest, MultipleStringConverters32To8) {
/*
We do NOT test partially popping a character and expecting the next