From 209440588d755b211fc50433c05d5f5c879c5291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Thu, 3 Aug 2023 23:55:14 +0200 Subject: [PATCH] fix: more memory issues thanks to -fsanitize=address flag Work In Progress #5 --- lib/convert.c | 4 +- lib/hash_map.c | 1 + lib/linked_list.c | 1 + lib/string.c | 6 +- set_version.c | 1 + test/convert_test.c | 152 ++++++++++++++++++++++++++++++++-------- test/linked_list_test.c | 1 + 7 files changed, 131 insertions(+), 35 deletions(-) diff --git a/lib/convert.c b/lib/convert.c index 9e535c9..3ef577c 100644 --- a/lib/convert.c +++ b/lib/convert.c @@ -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) { diff --git a/lib/hash_map.c b/lib/hash_map.c index 67a781e..3ba70d1 100644 --- a/lib/hash_map.c +++ b/lib/hash_map.c @@ -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]); } } diff --git a/lib/linked_list.c b/lib/linked_list.c index 5fd98cc..e505b28 100644 --- a/lib/linked_list.c +++ b/lib/linked_list.c @@ -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; } diff --git a/lib/string.c b/lib/string.c index b076e38..2876405 100644 --- a/lib/string.c +++ b/lib/string.c @@ -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) { diff --git a/set_version.c b/set_version.c index 961fbcd..79d831c 100644 --- a/set_version.c +++ b/set_version.c @@ -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; } diff --git a/test/convert_test.c b/test/convert_test.c index df5ee6f..b373fd6 100644 --- a/test/convert_test.c +++ b/test/convert_test.c @@ -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); } diff --git a/test/linked_list_test.c b/test/linked_list_test.c index 197e1ca..e56c90a 100644 --- a/test/linked_list_test.c +++ b/test/linked_list_test.c @@ -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() {