fix(utf-8 string): added missing error checks for memory allocation

This commit is contained in:
Mineplay 2025-07-11 05:49:12 -05:00
parent f27562c23e
commit 8714361e85

View file

@ -24,7 +24,6 @@
* -----------------------------------------------------------------------------
*/
#include "../../Include/Fledasty/Strings/UTF8String.h"
#include "Fledasty/Utils/Error.h"
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
@ -57,6 +56,10 @@ FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, un
if (current_string->capacity <= current_string->size + character_string_size) {
current_string->capacity += (current_string->capacity > character_string_size) ? current_string->capacity : character_string_size + 1;
current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
if (current_string->character_string == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
hallocy_copy_memory(current_string->character_string + current_string->size, character_string, character_string_size);
@ -83,6 +86,10 @@ FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_s
if (current_string->capacity <= current_string->size + character_string_size) {
current_string->capacity += (current_string->capacity > character_string_size) ? current_string->capacity : character_string_size;
current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
if (current_string->character_string == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
hallocy_move_memory(current_string->character_string + (index + character_string_size), current_string->character_string + index, current_string->size - index);
@ -108,6 +115,10 @@ FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *curr
if (current_string->capacity <= current_string->size + character_string_size) {
current_string->capacity += (current_string->capacity > character_string_size) ? current_string->capacity : character_string_size + 1;
current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
if (current_string->character_string == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
hallocy_move_memory(current_string->character_string + (index + character_string_size), current_string->character_string + index, current_string->size - index);
@ -138,6 +149,10 @@ FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *curre
if (current_string->capacity <= current_string->size + character_string_size) {
current_string->capacity += (current_string->capacity > character_string_size) ? current_string->capacity : character_string_size + 1;
current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
if (current_string->character_string == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
index += after_character_string_size;
@ -231,6 +246,10 @@ FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_st
if (current_string->capacity <= new_size) {
current_string->capacity += (current_string->capacity > character_string_size) ? current_string->capacity : character_string_size;
current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
if (current_string->character_string == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
hallocy_move_memory(current_string->character_string + index + character_string_size, current_string->character_string + index + replace_character_string_size, current_string->size - (index + replace_character_string_size));
@ -266,6 +285,10 @@ FledastyError fledasty_utf8_string_shrink_to_fit(FledastyUtf8String *current_str
current_string->capacity = current_string->size + 1;
current_string->character_string = (unsigned char*)hallocy_malloc(current_string->capacity);
if (current_string->character_string == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
hallocy_copy_memory(current_string->character_string, previous_string, current_string->size);
current_string->character_string[current_string->size] = '\0';
@ -306,6 +329,10 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si
if (utf8_string.capacity <= string_index) {
utf8_string.capacity += (utf8_string.capacity > FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE) ? utf8_string.capacity : FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE;
utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
if (utf8_string.character_string == NULL) {
return utf8_string;
}
}
utf8_string.character_string[string_index] = unicode[index];
@ -314,6 +341,10 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si
if (utf8_string.capacity <= string_index + 2) {
utf8_string.capacity += (utf8_string.capacity > FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE) ? utf8_string.capacity : FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE;
utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
if (utf8_string.character_string == NULL) {
return utf8_string;
}
}
utf8_string.character_string[string_index] = 0xC0 | ((unicode[index] >> 6) & 0x07);
@ -323,6 +354,10 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si
if (utf8_string.capacity <= string_index + 3) {
utf8_string.capacity += (utf8_string.capacity > FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE) ? utf8_string.capacity : FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE;
utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
if (utf8_string.character_string == NULL) {
return utf8_string;
}
}
utf8_string.character_string[string_index] = 0xE0 | ((unicode[index] >> 12) & 0x07);
@ -333,6 +368,10 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si
if (utf8_string.capacity <= string_index + 4) {
utf8_string.capacity += (utf8_string.capacity > FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE) ? utf8_string.capacity : FLEDASTY_STRING_DEFAULT_ALLOCATION_SIZE;
utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
if (utf8_string.character_string == NULL) {
return utf8_string;
}
}
utf8_string.character_string[string_index] = 0xF0 | ((unicode[index] >> 18) & 0x07);
@ -361,6 +400,10 @@ uint32_t *fledasty_utf8_string_decode(const FledastyUtf8String *current_string,
(*unicode_string_size) = 0;
size_t index = 0;
uint32_t *unicode_string = (uint32_t*)hallocy_malloc(current_string->size * sizeof(uint32_t));
if (unicode_string == NULL) {
return NULL;
}
while (index < current_string->size) {
if ((current_string->character_string[index] & 0xF0) == 0xF0) {
unicode_string[*unicode_string_size] = ((current_string->character_string[index] & 0x07) << 18) | ((current_string->character_string[index + 1] & 0x3F) << 12) | ((current_string->character_string[index + 2] & 0x3F) << 6) | (current_string->character_string[index + 3] & 0x3F);