From b922fd9cd3233ba73960013d4fd8a34f50401935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Mon, 7 Aug 2023 00:11:07 +0200 Subject: [PATCH] fix: handle EXIT_FAILURE by printing error message with perror --- lib/array_list.h | 1 + lib/character.h | 1 + lib/convert.c | 2 ++ lib/convert.h | 1 + lib/hash_map.h | 1 + lib/linked_list.c | 19 +++++++++++++++++-- lib/linked_list.h | 1 + lib/mathematics.h | 1 + lib/queue.c | 13 ++++++++++++- lib/queue.h | 1 + lib/stack.c | 13 ++++++++++++- lib/stack.h | 1 + lib/string.c | 9 ++++++++- lib/string.h | 1 + lib/terminal.c | 10 ++++++++++ lib/terminal.h | 1 + 16 files changed, 71 insertions(+), 5 deletions(-) diff --git a/lib/array_list.h b/lib/array_list.h index d82894e..7550670 100644 --- a/lib/array_list.h +++ b/lib/array_list.h @@ -1,6 +1,7 @@ #ifndef __LIBCPROJECT_ARRAY_LIST__ #define __LIBCPROJECT_ARRAY_LIST__ +#include #include #include diff --git a/lib/character.h b/lib/character.h index 527d24f..b42a9f1 100644 --- a/lib/character.h +++ b/lib/character.h @@ -1,6 +1,7 @@ #ifndef __LIBCPROJECT_CHARACTER__ #define __LIBCPROJECT_CHARACTER__ +#include #include #include diff --git a/lib/convert.c b/lib/convert.c index b084127..c39bc80 100644 --- a/lib/convert.c +++ b/lib/convert.c @@ -3,6 +3,7 @@ string_t convert_character_to_string(const char character) { string_t string = malloc(sizeof(char) * 2); if (string == NULL) { + perror("Error (convert_character_to_string)"); exit(EXIT_FAILURE); } string[0] = character; @@ -44,6 +45,7 @@ string_t convert_number_to_string(const long long integer) { } string_t string = malloc(sizeof(char) * length); if (string == NULL) { + perror("Error (convert_number_to_string)"); exit(EXIT_FAILURE); } current = mathematics_absolute_value(integer); diff --git a/lib/convert.h b/lib/convert.h index 9fb1cb1..1ebd899 100644 --- a/lib/convert.h +++ b/lib/convert.h @@ -1,6 +1,7 @@ #ifndef __LIBCPROJECT_CONVERT__ #define __LIBCPROJECT_CONVERT__ +#include #include #include diff --git a/lib/hash_map.h b/lib/hash_map.h index 02cab60..217dd83 100644 --- a/lib/hash_map.h +++ b/lib/hash_map.h @@ -1,6 +1,7 @@ #ifndef __LIBCPROJECT_HASH_MAP__ #define __LIBCPROJECT_HASH_MAP__ +#include #include #include #include diff --git a/lib/linked_list.c b/lib/linked_list.c index e505b28..032fcce 100644 --- a/lib/linked_list.c +++ b/lib/linked_list.c @@ -3,6 +3,7 @@ struct linked_list *linked_list_initialization() { struct linked_list *list = malloc(sizeof(*list)); if (list == NULL) { + perror("Error (linked_list_initialization)"); exit(EXIT_FAILURE); } list->head = NULL; @@ -11,8 +12,14 @@ struct linked_list *linked_list_initialization() { } struct linked_list_node *linked_list_add_in_head(struct linked_list *list, void *new_data) { + if (list == NULL) { + errno = EINVAL; + perror("Error (linked_list_add_in_head)"); + exit(EXIT_FAILURE); + } struct linked_list_node *node_new = malloc(sizeof(*node_new)); - if (list == NULL || node_new == NULL) { + if (node_new == NULL) { + perror("Error (linked_list_add_in_head)"); exit(EXIT_FAILURE); } node_new->data = new_data; @@ -24,6 +31,8 @@ struct linked_list_node *linked_list_add_in_head(struct linked_list *list, void void linked_list_delete_in_head(struct linked_list *list) { if (list == NULL) { + errno = EINVAL; + perror("Error (linked_list_delete_in_head)"); exit(EXIT_FAILURE); } if (list->head != NULL) { @@ -35,11 +44,17 @@ void linked_list_delete_in_head(struct linked_list *list) { } struct linked_list_node *linked_list_add_after_last(struct linked_list *list, void *new_data) { + if (list == NULL) { + errno = EINVAL; + perror("Error (linked_list_add_after_last)"); + exit(EXIT_FAILURE); + } if (list->head == NULL) { return linked_list_add_in_head(list, new_data); } struct linked_list_node *node_new = malloc(sizeof(*node_new)); - if (list == NULL || node_new == NULL) { + if (node_new == NULL) { + perror("Error (linked_list_add_after_last)"); exit(EXIT_FAILURE); } node_new->data = new_data; diff --git a/lib/linked_list.h b/lib/linked_list.h index a736325..e79f48d 100644 --- a/lib/linked_list.h +++ b/lib/linked_list.h @@ -1,6 +1,7 @@ #ifndef __LIBCPROJECT_LINKED_LIST__ #define __LIBCPROJECT_LINKED_LIST__ +#include #include #include diff --git a/lib/mathematics.h b/lib/mathematics.h index ab0ac33..41f25ad 100644 --- a/lib/mathematics.h +++ b/lib/mathematics.h @@ -3,6 +3,7 @@ #define MATHEMATICS_FLOAT_PRECISION 0.00000001 +#include #include #include "types.h" diff --git a/lib/queue.c b/lib/queue.c index cc8a464..d353c1d 100644 --- a/lib/queue.c +++ b/lib/queue.c @@ -3,6 +3,7 @@ struct queue *queue_initialization() { struct queue *queue = malloc(sizeof(*queue)); if (queue == NULL) { + perror("Error (queue_initialization)"); exit(EXIT_FAILURE); } queue->first = NULL; @@ -11,8 +12,14 @@ struct queue *queue_initialization() { } void queue_push(struct queue *queue, void *data) { + if (queue == NULL) { + errno = EINVAL; + perror("Error (queue_push)"); + exit(EXIT_FAILURE); + } struct queue_node *node_new = malloc(sizeof(*node_new)); - if (queue == NULL || node_new == NULL) { + if (node_new == NULL) { + perror("Error (queue_push)"); exit(EXIT_FAILURE); } node_new->data = data; @@ -31,6 +38,8 @@ void queue_push(struct queue *queue, void *data) { void *queue_pop(struct queue *queue) { if (queue == NULL) { + errno = EINVAL; + perror("Error (queue_pop)"); exit(EXIT_FAILURE); } struct queue_node *node = queue->first; @@ -46,6 +55,8 @@ void *queue_pop(struct queue *queue) { void queue_free(struct queue *queue) { if (queue == NULL) { + errno = EINVAL; + perror("Error (queue_free)"); exit(EXIT_FAILURE); } struct queue_node *node = queue->first; diff --git a/lib/queue.h b/lib/queue.h index 40327d4..bd6c298 100644 --- a/lib/queue.h +++ b/lib/queue.h @@ -1,6 +1,7 @@ #ifndef __LIBCPROJECT_QUEUE__ #define __LIBCPROJECT_QUEUE__ +#include #include #include diff --git a/lib/stack.c b/lib/stack.c index c8eb356..87746eb 100644 --- a/lib/stack.c +++ b/lib/stack.c @@ -3,6 +3,7 @@ struct stack *stack_initialization() { struct stack *stack = malloc(sizeof(*stack)); if (stack == NULL) { + perror("Error (stack_initialization)"); exit(EXIT_FAILURE); } stack->first = NULL; @@ -11,8 +12,14 @@ struct stack *stack_initialization() { } void stack_push(struct stack *stack, void *data) { + if (stack == NULL) { + errno = EINVAL; + perror("Error (stack_push)"); + exit(EXIT_FAILURE); + } struct stack_node *node_new = malloc(sizeof(*node_new)); - if (stack == NULL || data == NULL) { + if (data == NULL) { + perror("Error (stack_push)"); exit(EXIT_FAILURE); } node_new->data = data; @@ -23,6 +30,8 @@ void stack_push(struct stack *stack, void *data) { void *stack_pop(struct stack *stack) { if (stack == NULL) { + errno = EINVAL; + perror("Error (stack_pop)"); exit(EXIT_FAILURE); } struct stack_node *node = stack->first; @@ -38,6 +47,8 @@ void *stack_pop(struct stack *stack) { void stack_free(struct stack *stack) { if (stack == NULL) { + errno = EINVAL; + perror("Error (stack_free)"); exit(EXIT_FAILURE); } struct stack_node *node = stack->first; diff --git a/lib/stack.h b/lib/stack.h index 1f0d354..724e9b1 100644 --- a/lib/stack.h +++ b/lib/stack.h @@ -1,6 +1,7 @@ #ifndef __LIBCPROJECT_STACK__ #define __LIBCPROJECT_STACK__ +#include #include #include diff --git a/lib/string.c b/lib/string.c index 5ca2ef1..4d3fdbb 100644 --- a/lib/string.c +++ b/lib/string.c @@ -67,6 +67,7 @@ string_t string_copy(const string_t string) { size_t source_length = string_get_length(string); string_t copy = malloc(sizeof(char) * (source_length + 1)); if (copy == NULL) { + perror("Error (string_copy)"); exit(EXIT_FAILURE); } size_t index; @@ -150,6 +151,7 @@ string_t* string_split(const string_t string, char separator, size_t* result_siz string_t current = malloc(sizeof(char) * (string_length + 1)); string_t* result = NULL; if (current == NULL) { + perror("Error (string_split)"); exit(EXIT_FAILURE); } while (index_string < string_length) { @@ -157,6 +159,7 @@ string_t* string_split(const string_t string, char separator, size_t* result_siz current[index_current] = '\0'; result = realloc(result, sizeof(string_t) * (index_result + 1)); if (result == NULL) { + perror("Error (string_split)"); exit(EXIT_FAILURE); } result[index_result] = string_copy(current); @@ -168,10 +171,10 @@ string_t* string_split(const string_t string, char separator, size_t* result_siz } index_string++; } - current[index_current] = '\0'; result = realloc(result, sizeof(string_t) * (index_result + 1)); if (result == NULL) { + perror("Error (string_split)"); exit(EXIT_FAILURE); } result[index_result] = string_copy(current); @@ -188,6 +191,7 @@ string_t string_join(string_t* array, const char separator, size_t array_length) size_t string_length = total_length + (array_length - 1); string_t string = malloc(sizeof(char) * (string_length + 1)); if (string == NULL) { + perror("Error (string_join)"); exit(EXIT_FAILURE); } size_t current_index = 0; @@ -214,6 +218,7 @@ string_t string_concatenate(string_t string1, string_t string2) { size_t result_length = string1_length + string2_length; string_t result = malloc(sizeof(char) * (result_length + 1)); if (result == NULL) { + perror("Error (string_concatenate)"); exit(EXIT_FAILURE); } size_t index_string1 = 0; @@ -293,6 +298,7 @@ string_t string_get_formatted_number(const long long number, string_t separator) size_t formatted_length = number_string_length + (number_string_length - 1) / 3; string_t result = malloc(sizeof(char) * (formatted_length + 1)); if (result == NULL) { + perror("Error (string_get_formatted_number)"); exit(EXIT_FAILURE); } size_t count = 0; @@ -336,6 +342,7 @@ string_t string_get_last_occurence_of_character(const string_t string, char char } string_t result = malloc(sizeof(char) * (string_length - index_last_occurrence + 1)); if (result == NULL) { + perror("Error (string_get_last_occurence_of_character)"); exit(EXIT_FAILURE); } size_t index_result = 0; diff --git a/lib/string.h b/lib/string.h index e596c03..afc6efa 100644 --- a/lib/string.h +++ b/lib/string.h @@ -1,6 +1,7 @@ #ifndef __LIBCPROJECT_STRING__ #define __LIBCPROJECT_STRING__ +#include #include #include #include diff --git a/lib/terminal.c b/lib/terminal.c index 58c3915..2590732 100644 --- a/lib/terminal.c +++ b/lib/terminal.c @@ -5,6 +5,7 @@ string_t terminal_input() { size_t length = 1; string_t string = malloc(length * sizeof(char)); if (string == NULL) { + perror("Error (terminal_input)"); exit(EXIT_FAILURE); } *string = '\0'; @@ -12,6 +13,7 @@ string_t terminal_input() { length++; string = realloc(string, length * sizeof(char)); if (string == NULL) { + perror("Error (terminal_input)"); exit(EXIT_FAILURE); } character_append(string, character); @@ -54,6 +56,8 @@ void terminal_print_string(void* value) { void terminal_print_stack(struct stack* stack, void (*print_element)(void*)) { if (stack == NULL) { + errno = EINVAL; + perror("Error (terminal_print_stack)"); exit(EXIT_FAILURE); } struct stack_node* node_current = stack->first; @@ -68,6 +72,8 @@ void terminal_print_stack(struct stack* stack, void (*print_element)(void*)) { void terminal_print_queue(struct queue* queue, void (*print_element)(void*)) { if (queue == NULL) { + errno = EINVAL; + perror("Error (terminal_print_queue)"); exit(EXIT_FAILURE); } struct queue_node* node_current = queue->first; @@ -82,6 +88,8 @@ void terminal_print_queue(struct queue* queue, void (*print_element)(void*)) { void terminal_print_linked_list(struct linked_list* linked_list, void (*print_element)(void*)) { if (linked_list == NULL) { + errno = EINVAL; + perror("Error (terminal_print_linked_list)"); exit(EXIT_FAILURE); } struct linked_list_node* node_current = linked_list->head; @@ -96,6 +104,8 @@ void terminal_print_linked_list(struct linked_list* linked_list, void (*print_el void terminal_print_hash_map(struct hash_map* hash_map, void (*print_element)(void*)) { if (hash_map == NULL) { + errno = EINVAL; + perror("Error (terminal_print_hash_map)"); exit(EXIT_FAILURE); } printf("{\n"); diff --git a/lib/terminal.h b/lib/terminal.h index 58da19f..b557bf8 100644 --- a/lib/terminal.h +++ b/lib/terminal.h @@ -1,6 +1,7 @@ #ifndef __LIBCPROJECT_TERMINAL__ #define __LIBCPROJECT_TERMINAL__ +#include #include #include #include