Ensure utf8 codepoints don't overflow buffer and backspace handles them correctly (#207)

This commit is contained in:
gulrak 2022-05-28 10:10:42 +02:00 committed by GitHub
parent 694e770351
commit f534a9a265
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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';
}
}