GuiTextBox(): Fix buffer overrun from cursor index (#405)
This commit is contained in:
parent
76f006b9d0
commit
4b3d94f5df
15
src/raygui.h
15
src/raygui.h
@ -2464,7 +2464,10 @@ int GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode)
|
|||||||
int wrapMode = GuiGetStyle(DEFAULT, TEXT_WRAP_MODE);
|
int wrapMode = GuiGetStyle(DEFAULT, TEXT_WRAP_MODE);
|
||||||
|
|
||||||
Rectangle textBounds = GetTextBounds(TEXTBOX, bounds);
|
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
|
int textIndexOffset = 0; // Text index offset to start drawing in the box
|
||||||
|
|
||||||
// Cursor rectangle
|
// Cursor rectangle
|
||||||
@ -2513,6 +2516,8 @@ int GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode)
|
|||||||
{
|
{
|
||||||
state = STATE_PRESSED;
|
state = STATE_PRESSED;
|
||||||
|
|
||||||
|
if (textBoxCursorIndex > textLength) textBoxCursorIndex = textLength;
|
||||||
|
|
||||||
// If text does not fit in the textbox and current cursor position is out of bounds,
|
// 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
|
// we add an index offset to text for drawing only what requires depending on cursor
|
||||||
while (textWidth >= textBounds.width)
|
while (textWidth >= textBounds.width)
|
||||||
@ -2525,12 +2530,9 @@ int GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode)
|
|||||||
textWidth = GetTextWidth(text + textIndexOffset) - GetTextWidth(text + textBoxCursorIndex);
|
textWidth = GetTextWidth(text + textIndexOffset) - GetTextWidth(text + textBoxCursorIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
int textLength = (int)strlen(text); // Get current text length
|
|
||||||
int codepoint = GetCharPressed(); // Get Unicode codepoint
|
int codepoint = GetCharPressed(); // Get Unicode codepoint
|
||||||
if (multiline && IsKeyPressed(KEY_ENTER)) codepoint = (int)'\n';
|
if (multiline && IsKeyPressed(KEY_ENTER)) codepoint = (int)'\n';
|
||||||
|
|
||||||
if (textBoxCursorIndex > textLength) textBoxCursorIndex = textLength;
|
|
||||||
|
|
||||||
// Encode codepoint as UTF-8
|
// Encode codepoint as UTF-8
|
||||||
int codepointSize = 0;
|
int codepointSize = 0;
|
||||||
const char *charEncoded = CodepointToUTF8(codepoint, &codepointSize);
|
const char *charEncoded = CodepointToUTF8(codepoint, &codepointSize);
|
||||||
@ -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];
|
for (int i = textBoxCursorIndex; i < textLength; i++) text[i] = text[i + nextCodepointSize];
|
||||||
|
|
||||||
textLength -= codepointSize;
|
textLength -= codepointSize;
|
||||||
|
if (textBoxCursorIndex > textLength) textBoxCursorIndex = textLength;
|
||||||
|
|
||||||
// Make sure text last character is EOL
|
// Make sure text last character is EOL
|
||||||
text[textLength] = '\0';
|
text[textLength] = '\0';
|
||||||
@ -2663,7 +2666,7 @@ int GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode)
|
|||||||
if (GetMousePosition().x >= (textBounds.x + textEndWidth - glyphWidth/2))
|
if (GetMousePosition().x >= (textBounds.x + textEndWidth - glyphWidth/2))
|
||||||
{
|
{
|
||||||
mouseCursor.x = textBounds.x + textEndWidth;
|
mouseCursor.x = textBounds.x + textEndWidth;
|
||||||
mouseCursorIndex = (int)strlen(text);
|
mouseCursorIndex = textLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Place cursor at required index on mouse click
|
// Place cursor at required index on mouse click
|
||||||
@ -2695,7 +2698,7 @@ int GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode)
|
|||||||
|
|
||||||
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
|
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;
|
result = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user