From f534a9a26505487397efa2e2c4678c74fe5884cd Mon Sep 17 00:00:00 2001 From: gulrak Date: Sat, 28 May 2022 10:10:42 +0200 Subject: [PATCH] Ensure utf8 codepoints don't overflow buffer and backspace handles them correctly (#207) --- src/raygui.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/raygui.h b/src/raygui.h index 839a119..dafe437 100644 --- a/src/raygui.h +++ b/src/raygui.h @@ -2021,17 +2021,16 @@ bool GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode) int key = GetCharPressed(); // Returns codepoint as Unicode int keyCount = (int)strlen(text); + int byteSize = 0; + const char *textUTF8 = CodepointToUTF8(key, &byteSize); // Only allow keys in range [32..125] - if (keyCount < (textSize - 1)) + if (keyCount + byteSize < textSize) { float maxWidth = (bounds.width - (GuiGetStyle(TEXTBOX, TEXT_INNER_PADDING)*2)); if ((GetTextWidth(text) < (maxWidth - GuiGetStyle(DEFAULT, TEXT_SIZE))) && (key >= 32)) { - int byteSize = 0; - const char *textUTF8 = CodepointToUTF8(key, &byteSize); - for (int i = 0; i < byteSize; i++) { text[keyCount] = textUTF8[i]; @@ -2047,7 +2046,7 @@ bool GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode) { if (IsKeyPressed(KEY_BACKSPACE)) { - keyCount--; + while (keyCount > 0 && (text[--keyCount] & 0xc0) == 0x80); text[keyCount] = '\0'; } }