fix(linked list): fixed memory leaks that happen when failed allocation error is thrown

This commit is contained in:
Mineplay 2025-05-19 03:21:11 -05:00
parent 4cfd56bcea
commit 8dd77f8e39

View file

@ -48,6 +48,7 @@ FledastyError fledasty_linked_list_initialize(FledastyLinkedList *new_linked_lis
new_linked_list->start->value = hallocy_malloc(new_linked_list->element_byte_size);
if (new_linked_list->start->value == NULL) {
hallocy_free(new_linked_list->start);
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
@ -58,11 +59,33 @@ FledastyError fledasty_linked_list_initialize(FledastyLinkedList *new_linked_lis
for (size_t index = new_linked_list->element_byte_size; index < new_linked_list->size * new_linked_list->element_byte_size; index += new_linked_list->element_byte_size) {
FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode));
if (new_node == NULL) {
FledastyLinkedListNode *previous_node = NULL;
FledastyLinkedListNode *current_node = new_linked_list->start;
while (current_node != NULL) {
previous_node = current_node;
current_node = current_node->next;
hallocy_free(previous_node->value);
hallocy_free(previous_node);
}
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
new_node->value = hallocy_malloc(new_linked_list->element_byte_size);
if (new_node->value == NULL) {
hallocy_free(new_node);
FledastyLinkedListNode *previous_node = NULL;
FledastyLinkedListNode *current_node = new_linked_list->start;
while (current_node != NULL) {
previous_node = current_node;
current_node = current_node->next;
hallocy_free(previous_node->value);
hallocy_free(previous_node);
}
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
@ -114,6 +137,7 @@ FledastyError fledasty_linked_list_append(FledastyLinkedList *current_linked_lis
new_node->value = hallocy_malloc(current_linked_list->element_byte_size);
if (new_node->value == NULL) {
hallocy_free(new_node);
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
@ -143,6 +167,7 @@ FledastyError fledasty_linked_list_insert_at_index(FledastyLinkedList *current_l
new_node->value = hallocy_malloc(current_linked_list->element_byte_size);
if (new_node->value == NULL) {
hallocy_free(new_node);
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
@ -191,6 +216,7 @@ FledastyError fledasty_linked_list_insert_before_value(FledastyLinkedList *curre
new_node->value = hallocy_malloc(current_linked_list->element_byte_size);
if (new_node->value == NULL) {
hallocy_free(new_node);
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
@ -228,6 +254,7 @@ FledastyError fledasty_linked_list_insert_after_value(FledastyLinkedList *curren
new_node->value = hallocy_malloc(current_linked_list->element_byte_size);
if (new_node->value == NULL) {
hallocy_free(new_node);
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
@ -262,7 +289,7 @@ FledastyError fledasty_linked_list_remove_at_index(FledastyLinkedList *current_l
current_node = current_node->next;
}
if (current_node->next == NULL) {
if (current_node == current_linked_list->end) {
current_linked_list->end = previous_node;
}
@ -300,7 +327,7 @@ FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_link
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
if (current_node->next == NULL) {
if (current_node == current_linked_list->end) {
current_linked_list->end = previous_node;
}