1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-11-08 22:31:31 +01:00

fix: more memory issues thanks to -fsanitize=address flag

Work In Progress #5
This commit is contained in:
Théo LUDWIG 2023-08-03 23:55:14 +02:00
parent a0a1310f53
commit 209440588d
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
7 changed files with 131 additions and 35 deletions

View File

@ -56,7 +56,9 @@ string_t convert_number_to_string(const long long integer) {
string_value[index++] = '-';
}
string_value[index] = '\0';
return string_reverse(string_value);
char* result = string_reverse(string_value);
free(string_value);
return result;
}
string_t convert_number_from_base_10_to_base(unsigned long long number, unsigned int base) {

View File

@ -221,6 +221,7 @@ string_t *hash_map_get_keys(struct hash_map *hash_map) {
void hash_map_free(struct hash_map *hash_map) {
for (size_t index = 0; index < hash_map->capacity; index++) {
if (hash_map->items[index] != NULL) {
free(hash_map->items[index]->head->data);
linked_list_free(hash_map->items[index]);
}
}

View File

@ -68,6 +68,7 @@ struct linked_list *linked_list_reverse(struct linked_list *list) {
linked_list_add_after_last(linked_list_reversed, stack_node_current->data);
stack_node_current = stack_node_current->next;
}
stack_free(stack);
return linked_list_reversed;
}

View File

@ -192,11 +192,9 @@ string_t* string_split(const string_t string_value, char separator, size_t* resu
size_t index_result = 0;
string_t current = malloc(sizeof(char) * (string_length + 1));
string_t* result = NULL;
if (current == NULL) {
exit(EXIT_FAILURE);
}
while (index_string < string_length) {
if (string_value[index_string] == separator) {
current[index_current] = '\0';
@ -206,7 +204,7 @@ string_t* string_split(const string_t string_value, char separator, size_t* resu
}
result[index_result] = string_copy(current);
index_result++;
index_current = 0; // Reset index_current for the next substring
index_current = 0;
} else {
current[index_current] = string_value[index_string];
index_current++;
@ -281,6 +279,7 @@ bool string_get_has_unique_characters(const string_t string_value) {
string_t key = convert_character_to_string(character);
if (hash_map_contains_key(characters_already_seen, key)) {
has_unique = false;
free(key);
} else {
hash_map_add(characters_already_seen, key, (void*)true);
}
@ -342,6 +341,7 @@ string_t string_get_formatted_number(const long long number, string_t separator)
count = 0;
}
}
free(number_string);
result[formatted_length] = '\0';
result = string_reverse(result);
if (is_negative) {

View File

@ -19,5 +19,6 @@ int main(int argc, string_t* argv) {
return EXIT_FAILURE;
}
printf("Success: Version set to %s.\n", argv[1]);
free(content);
return EXIT_SUCCESS;
}

View File

@ -10,11 +10,25 @@ void convert_test() {
}
void convert_character_to_string_test() {
assert(assert_string_equal(convert_character_to_string('a'), "a"));
assert(assert_string_equal(convert_character_to_string('A'), "A"));
assert(assert_string_equal(convert_character_to_string('0'), "0"));
assert(assert_string_equal(convert_character_to_string(' '), " "));
assert(assert_string_equal(convert_character_to_string('\0'), ""));
char* result = convert_character_to_string('a');
assert(assert_string_equal(result, "a"));
free(result);
result = convert_character_to_string('A');
assert(assert_string_equal(result, "A"));
free(result);
result = convert_character_to_string('0');
assert(assert_string_equal(result, "0"));
free(result);
result = convert_character_to_string(' ');
assert(assert_string_equal(result, " "));
free(result);
result = convert_character_to_string('\0');
assert(assert_string_equal(result, ""));
free(result);
}
void convert_character_to_digit_test() {
@ -63,33 +77,109 @@ void convert_string_to_number_test() {
}
void convert_number_to_string_test() {
assert(assert_string_equal(convert_number_to_string(0), "0"));
assert(assert_string_equal(convert_number_to_string(1), "1"));
assert(assert_string_equal(convert_number_to_string(2), "2"));
assert(assert_string_equal(convert_number_to_string(3), "3"));
assert(assert_string_equal(convert_number_to_string(4), "4"));
assert(assert_string_equal(convert_number_to_string(5), "5"));
assert(assert_string_equal(convert_number_to_string(6), "6"));
assert(assert_string_equal(convert_number_to_string(7), "7"));
assert(assert_string_equal(convert_number_to_string(8), "8"));
assert(assert_string_equal(convert_number_to_string(9), "9"));
assert(assert_string_equal(convert_number_to_string(10), "10"));
assert(assert_string_equal(convert_number_to_string(11), "11"));
assert(assert_string_equal(convert_number_to_string(20), "20"));
assert(assert_string_equal(convert_number_to_string(-0), "0"));
assert(assert_string_equal(convert_number_to_string(-1), "-1"));
assert(assert_string_equal(convert_number_to_string(-20), "-20"));
char* result = convert_number_to_string(0);
assert(assert_string_equal(result, "0"));
free(result);
result = convert_number_to_string(1);
assert(assert_string_equal(result, "1"));
free(result);
result = convert_number_to_string(2);
assert(assert_string_equal(result, "2"));
free(result);
result = convert_number_to_string(3);
assert(assert_string_equal(result, "3"));
free(result);
result = convert_number_to_string(4);
assert(assert_string_equal(result, "4"));
free(result);
result = convert_number_to_string(5);
assert(assert_string_equal(result, "5"));
free(result);
result = convert_number_to_string(6);
assert(assert_string_equal(result, "6"));
free(result);
result = convert_number_to_string(7);
assert(assert_string_equal(result, "7"));
free(result);
result = convert_number_to_string(8);
assert(assert_string_equal(result, "8"));
free(result);
result = convert_number_to_string(9);
assert(assert_string_equal(result, "9"));
free(result);
result = convert_number_to_string(10);
assert(assert_string_equal(result, "10"));
free(result);
result = convert_number_to_string(11);
assert(assert_string_equal(result, "11"));
free(result);
result = convert_number_to_string(20);
assert(assert_string_equal(result, "20"));
free(result);
result = convert_number_to_string(-0);
assert(assert_string_equal(result, "0"));
free(result);
result = convert_number_to_string(-1);
assert(assert_string_equal(result, "-1"));
free(result);
result = convert_number_to_string(-20);
assert(assert_string_equal(result, "-20"));
free(result);
}
void convert_number_from_base_to_another_test() {
assert(assert_string_equal(convert_number_from_base_to_another("15", 10, 16), "F"));
assert(assert_string_equal(convert_number_from_base_to_another("100000000", 2, 16), "100"));
assert(assert_string_equal(convert_number_from_base_to_another("FFFFFF", 16, 10), "16777215"));
assert(assert_string_equal(convert_number_from_base_to_another("1D57", 17, 35), "75C"));
assert(assert_string_equal(convert_number_from_base_to_another("80E", 20, 5), "100324"));
assert(assert_string_equal(convert_number_from_base_to_another("99", 10, 10), "99"));
assert(assert_string_equal(convert_number_from_base_to_another("3433024", 6, 28), "8008"));
assert(assert_string_equal(convert_number_from_base_to_another("30288G3A", 17, 36), "KF12OI"));
assert(assert_string_equal(convert_number_from_base_to_another("10", 9, 9), "10"));
assert(assert_string_equal(convert_number_from_base_to_another("10E", 23, 8), "1037"));
char* result = convert_number_from_base_to_another("15", 10, 16);
assert(assert_string_equal(result, "F"));
free(result);
result = convert_number_from_base_to_another("100000000", 2, 16);
assert(assert_string_equal(result, "100"));
free(result);
result = convert_number_from_base_to_another("FFFFFF", 16, 10);
assert(assert_string_equal(result, "16777215"));
free(result);
result = convert_number_from_base_to_another("1D57", 17, 35);
assert(assert_string_equal(result, "75C"));
free(result);
result = convert_number_from_base_to_another("80E", 20, 5);
assert(assert_string_equal(result, "100324"));
free(result);
result = convert_number_from_base_to_another("99", 10, 10);
assert(assert_string_equal(result, "99"));
free(result);
result = convert_number_from_base_to_another("3433024", 6, 28);
assert(assert_string_equal(result, "8008"));
free(result);
result = convert_number_from_base_to_another("30288G3A", 17, 36);
assert(assert_string_equal(result, "KF12OI"));
free(result);
result = convert_number_from_base_to_another("10", 9, 9);
assert(assert_string_equal(result, "10"));
free(result);
result = convert_number_from_base_to_another("10E", 23, 8);
assert(assert_string_equal(result, "1037"));
free(result);
}

View File

@ -79,6 +79,7 @@ void linked_list_reverse_test() {
assert((list_reversed->head->next->next->data) == (void *)'B');
assert((list_reversed->head->next->next->next->data) == (void *)'A');
linked_list_free(list);
linked_list_free(list_reversed);
}
void linked_list_reverse_mutate_test() {