From 798da1b52243c1eed1b0093af7bdf83d145d5410 Mon Sep 17 00:00:00 2001 From: gulrak Date: Thu, 9 Jun 2022 21:07:10 +0200 Subject: [PATCH] Remove two potential out of bounds crashes in GuiTextBoxMulti (#211) --- src/raygui.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/raygui.h b/src/raygui.h index 818d001..d97fe3f 100644 --- a/src/raygui.h +++ b/src/raygui.h @@ -2319,9 +2319,11 @@ bool GuiTextBoxMulti(Rectangle bounds, char *text, int textSize, bool editMode) // We get an Unicode codepoint int codepoint = GetCharPressed(); int textLength = (int)strlen(text); // Length in bytes (UTF-8 string) + int byteSize = 0; + const char *textUTF8 = CodepointToUTF8(key, &byteSize) // Introduce characters - if (textLength < (textSize - 1)) + if ((textLength + byteSize) < textSize) { if (IsKeyPressed(KEY_ENTER)) { @@ -2353,7 +2355,7 @@ bool GuiTextBoxMulti(Rectangle bounds, char *text, int textSize, bool editMode) { // Remove latest UTF-8 unicode character introduced (n bytes) int charUTF8Length = 0; - while (((unsigned char)text[textLength - 1 - charUTF8Length] & 0b01000000) == 0) charUTF8Length++; + while (charUTF8Length < textLength && ((unsigned char)text[textLength - 1 - charUTF8Length] & 0b01000000) == 0) charUTF8Length++; textLength -= (charUTF8Length + 1); text[textLength] = '\0';