diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index b3a1fe773..fe22fa6ef 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -64,6 +64,8 @@ Other Changes: triggered by some widgets e.g. Checkbox(), Selectable() and many others, which cleared ActiveId at the same time as editing. (#9028) Note that IsItemDeactivatedAfterEdit() was not affected, only IsItemEdited). +- Debug Tools: fixed DebugTextEncoding() potentially reading out of bounds + if provided a trailing truncated UTF-8 sequence. - Backends: - GLFW: fixed building on Linux platforms where Wayland headers are not available. (#9024, #8969, #8921, #8920) [@jagot] @@ -78,7 +80,6 @@ Other Changes: (note: examples application were not updated yet) - Win32: Revert 1.92.4 change of comparing dwPacketNumber, which prevents refreshing accurate gamepad info after focus-out + io.ClearInputKeys(). (#8556) - - Examples: - GLFW+WebGPU: removed unnecessary ImGui_ImplWGPU_InvalidateDeviceObjects() call during surface resize. (#8381) diff --git a/imgui.cpp b/imgui.cpp index 14e90ada1..8a446fc4c 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -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(); diff --git a/imgui_draw.cpp b/imgui_draw.cpp index b7613ef5a..549b4f332 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -5023,7 +5023,9 @@ const ImWchar* ImFontAtlas::GetGlyphRangesVietnamese() void ImFontGlyphRangesBuilder::AddText(const char* text, const char* text_end) { - while (text_end ? (text < text_end) : *text) + if (text_end == NULL) + text_end = text + strlen(text); + while (text < text_end) { unsigned int c = 0; int c_len = ImTextCharFromUtf8(&c, text, text_end); diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index 0d2c9507b..d7c0df486 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -5151,12 +5151,13 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { // Filter pasted buffer const int clipboard_len = (int)ImStrlen(clipboard); + const char* clipboard_end = clipboard + clipboard_len; ImVector clipboard_filtered; clipboard_filtered.reserve(clipboard_len + 1); for (const char* s = clipboard; *s != 0; ) { unsigned int c; - int in_len = ImTextCharFromUtf8(&c, s, NULL); + int in_len = ImTextCharFromUtf8(&c, s, clipboard_end); s += in_len; if (!InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data, true)) continue;