fixed potential memory leak and used R_MALLOC

This commit is contained in:
DissolveDZ 2023-09-30 20:48:40 +02:00
parent d62d4c3309
commit 9d4011832c

View File

@ -1356,6 +1356,12 @@ unsigned int TextLength(const char *text)
// Formatting of text with variables to 'embed' // Formatting of text with variables to 'embed'
const char *TextFormat(const char *format, ...) const char *TextFormat(const char *format, ...)
{ {
if (!format)
return "";
static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 };
memset(buffer, 0, MAX_TEXT_BUFFER_LENGTH);
va_list args; va_list args;
va_start(args, format); va_start(args, format);
@ -1367,8 +1373,7 @@ const char *TextFormat(const char *format, ...)
return NULL; return NULL;
} }
char *result = (char *)RL_MALLOC(length + 1); // Allocate memory for the formatted string (+1 for null terminator) if (buffer == NULL)
if (result == NULL)
{ {
// Handle memory allocation failure // Handle memory allocation failure
va_end(args); va_end(args);
@ -1378,9 +1383,10 @@ const char *TextFormat(const char *format, ...)
va_end(args); // Reset the va_list va_end(args); // Reset the va_list
va_start(args, format); // Start again for the actual formatting va_start(args, format); // Start again for the actual formatting
vsnprintf(result, length + 1, format, args); // Format the string and copy it to the result buffer vsnprintf(buffer, length + 1, format, args); // Format the string and copy it to the result buffer
va_end(args); va_end(args);
return result;
return buffer;
} }
// Get integer value from text // Get integer value from text
@ -1405,23 +1411,13 @@ int TextToInteger(const char *text)
// Copy one string to another, returns bytes copied // Copy one string to another, returns bytes copied
int TextCopy(char *dst, const char *src) int TextCopy(char *dst, const char *src)
{ {
int bytes = 0; if (!dst || !src)
return 0;
if ((src != NULL) && (dst != NULL)) // use strcpy since it uses vector operations
{ unsigned int length = strcpy(dst, src);
while (*src != '\0')
{ return length;
*dst = *src;
dst++;
src++;
bytes++;
}
*dst = '\0';
}
return bytes;
} }
// Check if two text string are equal // Check if two text string are equal
@ -1453,12 +1449,14 @@ const char *TextSubtext(const char *text, int position, int length)
if (length > textLength) if (length > textLength)
length = textLength; length = textLength;
char *buffer = RL_MALLOC(length + 1); static char buffer[512] = {0};
memcpy(buffer, text + position, length); memset(buffer, 0, length + 1);
buffer[length] = '\0'; memcpy(buffer, text+position, length);
// strcpy(text, buffer);
// free(buffer); *(buffer + length) = '\0';
return text; *(buffer + length + 1) = 0;
return buffer;
} }
// Replace text string // Replace text string