TextSubtext fixes (#4759)
Fix buffer write overflow Fix reading past the end of text
This commit is contained in:
parent
a1de60f3ba
commit
1d87932d93
1 changed files with 6 additions and 6 deletions
12
src/rtext.c
12
src/rtext.c
|
@ -1540,21 +1540,21 @@ const char *TextSubtext(const char *text, int position, int length)
|
||||||
|
|
||||||
if (position >= textLength)
|
if (position >= textLength)
|
||||||
{
|
{
|
||||||
position = textLength - 1;
|
return buffer; //First char is already '\0' by memset
|
||||||
length = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (length >= textLength) length = textLength;
|
int maxLength = textLength - position;
|
||||||
|
if (length > maxLength) length = maxLength;
|
||||||
|
if (length >= MAX_TEXT_BUFFER_LENGTH) length = MAX_TEXT_BUFFER_LENGTH - 1;
|
||||||
|
|
||||||
// NOTE: Alternative: memcpy(buffer, text + position, length)
|
// NOTE: Alternative: memcpy(buffer, text + position, length)
|
||||||
|
|
||||||
for (int c = 0 ; c < length ; c++)
|
for (int c = 0 ; c < length ; c++)
|
||||||
{
|
{
|
||||||
*(buffer + c) = *(text + position);
|
buffer[c] = text[position + c];
|
||||||
text++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*(buffer + length) = '\0';
|
buffer[length] = '\0';
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue