Skip to content

Commit

Permalink
GuiTextBox(): Fix buffer overrun from cursor index (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
segcore authored May 31, 2024
1 parent 76f006b commit 4b3d94f
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/raygui.h
Original file line number Diff line number Diff line change
Expand Up @@ -2464,7 +2464,10 @@ int GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode)
int wrapMode = GuiGetStyle(DEFAULT, TEXT_WRAP_MODE);

Rectangle textBounds = GetTextBounds(TEXTBOX, bounds);
int textWidth = GetTextWidth(text) - GetTextWidth(text + textBoxCursorIndex);
int textLength = (int)strlen(text); // Get current text length
int thisCursorIndex = textBoxCursorIndex;
if (thisCursorIndex > textLength) thisCursorIndex = textLength;
int textWidth = GetTextWidth(text) - GetTextWidth(text + thisCursorIndex);
int textIndexOffset = 0; // Text index offset to start drawing in the box

// Cursor rectangle
Expand Down Expand Up @@ -2513,6 +2516,8 @@ int GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode)
{
state = STATE_PRESSED;

if (textBoxCursorIndex > textLength) textBoxCursorIndex = textLength;

// If text does not fit in the textbox and current cursor position is out of bounds,
// we add an index offset to text for drawing only what requires depending on cursor
while (textWidth >= textBounds.width)
Expand All @@ -2525,12 +2530,9 @@ int GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode)
textWidth = GetTextWidth(text + textIndexOffset) - GetTextWidth(text + textBoxCursorIndex);
}

int textLength = (int)strlen(text); // Get current text length
int codepoint = GetCharPressed(); // Get Unicode codepoint
if (multiline && IsKeyPressed(KEY_ENTER)) codepoint = (int)'\n';

if (textBoxCursorIndex > textLength) textBoxCursorIndex = textLength;

// Encode codepoint as UTF-8
int codepointSize = 0;
const char *charEncoded = CodepointToUTF8(codepoint, &codepointSize);
Expand Down Expand Up @@ -2572,6 +2574,7 @@ int GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode)
for (int i = textBoxCursorIndex; i < textLength; i++) text[i] = text[i + nextCodepointSize];

textLength -= codepointSize;
if (textBoxCursorIndex > textLength) textBoxCursorIndex = textLength;

// Make sure text last character is EOL
text[textLength] = '\0';
Expand Down Expand Up @@ -2663,7 +2666,7 @@ int GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode)
if (GetMousePosition().x >= (textBounds.x + textEndWidth - glyphWidth/2))
{
mouseCursor.x = textBounds.x + textEndWidth;
mouseCursorIndex = (int)strlen(text);
mouseCursorIndex = textLength;
}

// Place cursor at required index on mouse click
Expand Down Expand Up @@ -2695,7 +2698,7 @@ int GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode)

if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
textBoxCursorIndex = (int)strlen(text); // GLOBAL: Place cursor index to the end of current text
textBoxCursorIndex = textLength; // GLOBAL: Place cursor index to the end of current text
result = 1;
}
}
Expand Down

0 comments on commit 4b3d94f

Please sign in to comment.