Debug Tools: fixed DebugTextEncoding() potentially reading out of bounds if provided a trailing truncated UTF-8 sequence.

This commit is contained in:
ocornut
2025-10-29 17:34:02 +01:00
parent 2a194e21a0
commit 8df962a6ed
4 changed files with 14 additions and 7 deletions

View File

@@ -1642,14 +1642,15 @@ void ImGuiIO::AddInputCharacterUTF16(ImWchar16 c)
AddInputCharacter((unsigned)cp);
}
void ImGuiIO::AddInputCharactersUTF8(const char* utf8_chars)
void ImGuiIO::AddInputCharactersUTF8(const char* str)
{
if (!AppAcceptingEvents)
return;
while (*utf8_chars != 0)
const char* str_end = str + strlen(str);
while (*str != 0)
{
unsigned int c = 0;
utf8_chars += ImTextCharFromUtf8(&c, utf8_chars, NULL);
str += ImTextCharFromUtf8(&c, str, str_end);
AddInputCharacter(c);
}
}
@@ -2502,6 +2503,7 @@ int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char*
int len = lengths[*(const unsigned char*)in_text >> 3];
int wanted = len + (len ? 0 : 1);
// IMPORTANT: if in_text_end == NULL it assume we have enough space!
if (in_text_end == NULL)
in_text_end = in_text + wanted; // Max length, nulls will be taken into account.
@@ -15998,10 +16000,11 @@ void ImGui::DebugTextEncoding(const char* str)
TableSetupColumn("Glyph");
TableSetupColumn("Codepoint");
TableHeadersRow();
const char* str_end = str + strlen(str); // As we may receive malformed UTF-8, pass an explicit end instead of relying on ImTextCharFromUtf8() assuming enough space.
for (const char* p = str; *p != 0; )
{
unsigned int c;
const int c_utf8_len = ImTextCharFromUtf8(&c, p, NULL);
const int c_utf8_len = ImTextCharFromUtf8(&c, p, str_end);
TableNextColumn();
Text("%d", (int)(p - str));
TableNextColumn();