From b2129d81b38a22c9102a8fb3b66d317fc77c5c07 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 30 Mar 2017 13:40:59 +0200 Subject: [PATCH] TP_13 exo1: Fix a memleak on the prompt error handling case MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP_13/exo1/lib/io.c | 13 +++++++------ TP_13/exo1/lib/io.h | 2 +- TP_13/exo1/src/main.c | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/TP_13/exo1/lib/io.c b/TP_13/exo1/lib/io.c index b0a66a0..b6cc52c 100644 --- a/TP_13/exo1/lib/io.c +++ b/TP_13/exo1/lib/io.c @@ -17,7 +17,7 @@ int* prompt_array(int array[], unsigned* size) { for (unsigned i = 0; i < *size; i++) { errno += prompt_value("Valeur?", &array[i]); /* error might have occured */ - handle_prompt_error(errno); + handle_prompt_error(errno, array); } return array; } @@ -59,7 +59,7 @@ void do_sort(int array[], unsigned size) { printf("4) Croissant impairs en premier.\n"); do { errno = prompt_value("Choix?", &choice); - handle_prompt_error(errno); + handle_prompt_error(errno, array); done = true; if (1 > choice || 4 < choice) { printf("\nFaire un choix compris entre 1 et 4\n"); @@ -92,7 +92,7 @@ void do_count(int array[], unsigned size) { int search_value = 0; errno = prompt_value("\nValeur a chercher?", &search_value); - handle_prompt_error(errno); + handle_prompt_error(errno, array); printf("La valeur %d est presente %d fois dans le tableau\n", search_value, count_tab_element(array, size, search_value)); } @@ -101,22 +101,23 @@ void do_resize(int array[], unsigned* old_size) { unsigned new_size = 0; errno = prompt_value("\nNouvelle taille?", (int*)&new_size); - handle_prompt_error(errno); + handle_prompt_error(errno, array); array = resize_tab(array, *old_size, new_size); if (new_size > *old_size) { printf("Saisie des valeurs supplementaires du tableau\n"); for (unsigned i = *old_size; i < new_size; i++) { errno += prompt_value("Valeur?", &array[i]); - handle_prompt_error(errno); + handle_prompt_error(errno, array); } } *old_size = new_size; } -void handle_prompt_error(int errno) { +void handle_prompt_error(int errno, int* tab) { if (errno != 0) { printf("\nMerci de saisir un nombre entier, exiting\n"); /* it's somewhat violent but better than looping forever */ + free_tab(tab); exit(EXIT_FAILURE); } } diff --git a/TP_13/exo1/lib/io.h b/TP_13/exo1/lib/io.h index 6dfc77a..fd5c484 100644 --- a/TP_13/exo1/lib/io.h +++ b/TP_13/exo1/lib/io.h @@ -3,7 +3,7 @@ int prompt_value(const char* msg, int* result); int* prompt_array(int array[], unsigned* size); -void handle_prompt_error(int errno); +void handle_prompt_error(int errno, int* tab); void display_choice_menu(); diff --git a/TP_13/exo1/src/main.c b/TP_13/exo1/src/main.c index ef4d545..a68e73c 100644 --- a/TP_13/exo1/src/main.c +++ b/TP_13/exo1/src/main.c @@ -17,7 +17,7 @@ int main() { do { display_choice_menu(); errno = prompt_value("Choix?", &choice); - handle_prompt_error(errno); + handle_prompt_error(errno, tab); if (1 > choice || 8 < choice) { printf("\nFaire un choix compris entre 1 et 8\n"); continue; -- 2.34.1