REVIEWED: Avoid realloc() calls, small security improvement

This commit is contained in:
Ray
2025-09-01 20:37:23 +02:00
parent 6226abb0d3
commit 1777da9056
4 changed files with 39 additions and 25 deletions

View File

@@ -1920,10 +1920,11 @@ char *LoadUTF8(const int *codepoints, int length)
size += bytes;
}
// Resize memory to text length + string NULL terminator
void *ptr = RL_REALLOC(text, size + 1);
if (ptr != NULL) text = (char *)ptr;
// Create second buffer and copy data manually to it
char *temp = (char *)RL_CALLOC(size + 1, 1);
memcpy(temp, text, size);
RL_FREE(text);
text = temp;
return text;
}
@@ -1951,8 +1952,11 @@ int *LoadCodepoints(const char *text, int *count)
i += codepointSize;
}
// Re-allocate buffer to the actual number of codepoints loaded
codepoints = (int *)RL_REALLOC(codepoints, codepointCount*sizeof(int));
// Create second buffer and copy data manually to it
int *temp = (int *)RL_CALLOC(codepointCount, sizeof(int));
for (int i = 0; i < codepointCount; i++) temp[i] = codepoints[i];
RL_FREE(codepoints);
codepoints = temp;
*count = codepointCount;