From ed06e3eb8ebba4ed9a9e194b7edf0043a05e91da Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Tue, 28 May 2019 17:35:34 +0200 Subject: [PATCH] corrige deux bad read or write due au depassement de la taille de map, en ajoutant total_num dans la struct --- f_bonus_opti.c | 5 +++-- f_handle_errors.c | 21 +++++++++------------ f_parse_input.c | 5 +---- f_read_file.c | 2 +- f_search_map.c | 8 +++++--- fillit.h | 4 ++-- main.c | 9 ++------- 7 files changed, 23 insertions(+), 31 deletions(-) diff --git a/f_bonus_opti.c b/f_bonus_opti.c index 463730d..21b4873 100644 --- a/f_bonus_opti.c +++ b/f_bonus_opti.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/05/24 14:42:46 by hulamy #+# #+# */ -/* Updated: 2019/05/28 13:03:46 by hulamy ### ########.fr */ +/* Updated: 2019/05/28 17:00:32 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,7 +62,7 @@ int check_same_tetri(t_fillist *list, int num) int i; curr_tetri = list; - while (curr_tetri != NULL/* && list->dope[1]*/) + while (curr_tetri != NULL) { i = 0; if (!(curr_tetri->memory = @@ -78,6 +78,7 @@ int check_same_tetri(t_fillist *list, int num) next_tetri->same = curr_tetri; next_tetri = next_tetri->next; } + curr_tetri->total_num = num; curr_tetri = curr_tetri->next; } return (0); diff --git a/f_handle_errors.c b/f_handle_errors.c index 5377be0..9e85e87 100644 --- a/f_handle_errors.c +++ b/f_handle_errors.c @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/01 13:29:05 by vmanzoni #+# #+# */ -/* Updated: 2019/05/28 11:26:25 by hulamy ### ########.fr */ +/* Updated: 2019/05/28 17:27:17 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ void print_error(char *str) { write(1, str, ft_strlen(str)); -// exit(1); + exit(0); } /* @@ -31,22 +31,19 @@ void print_error(char *str) void print_error_extended(int error, int *dope) { if (!dope[3]) - { print_error("error\n"); - exit(0); - } if (error == 1) - ft_putstr("error: File contains char other than '.','#' and '\\n'.\n"); + print_error("error: File contains char other than '.','#' and '\\n'\n"); if (error == 2) - ft_putstr("error: File contains more than 2 '\\n' in a row.\n"); + print_error("error: File contains more than 2 '\\n' in a row\n"); if (error == 3) - ft_putstr("error: File contains less than 1 tetrimino " - "or more than 26.\n"); + print_error("error: File contains less than 1 tetrimino " + "or more than 26\n"); if (error == 4) - ft_putstr("error: Tetrimino has more or less than 4 #.\n"); + print_error("error: Tetrimino has more or less than 4 #\n"); if (error == 5) - ft_putstr("error: Tetrimino # are not all connected.\n"); -// exit(1); + print_error("error: Tetrimino # are not all connected\n"); + print_error("error\n"); } /* diff --git a/f_parse_input.c b/f_parse_input.c index 1594cc1..eded856 100644 --- a/f_parse_input.c +++ b/f_parse_input.c @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/15 14:48:14 by vmanzoni #+# #+# */ -/* Updated: 2019/05/28 10:44:43 by hulamy ### ########.fr */ +/* Updated: 2019/05/28 17:27:44 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -144,10 +144,7 @@ int parse_input(char *input, t_fillist **list, int *dope) while (j < 19) tetri[j++] = input[i++]; tetri[19] = '\0'; - // "wrong tetrimino" utile ? if (check_tetri_errors(tetri)) -// print_error("error\n"); -// print_error("error: Wrong tetrimino.\n"); print_error_extended(check_tetri_errors(tetri), dope); add_to_list(tetri, list, letter++, dope); while (input[i] && input[i] != '.' && input[i] != '#') diff --git a/f_read_file.c b/f_read_file.c index edacbfd..ba3b5d7 100644 --- a/f_read_file.c +++ b/f_read_file.c @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/13 12:09:46 by vmanzoni #+# #+# */ -/* Updated: 2019/05/27 19:46:54 by hulamy ### ########.fr */ +/* Updated: 2019/05/28 17:02:19 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/f_search_map.c b/f_search_map.c index 0eb4b7e..1401ed8 100644 --- a/f_search_map.c +++ b/f_search_map.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/27 20:47:22 by hulamy #+# #+# */ -/* Updated: 2019/05/28 11:56:18 by hulamy ### ########.fr */ +/* Updated: 2019/05/28 17:33:43 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,7 +36,9 @@ unsigned int fit_in_place(unsigned int *map, t_fillist *lst, int size, int i) return (0); if (r >= 32 && ++n) r -= 32; - tmp = (mask & (map[n] << r)) | (mask & (map[n + 1] >> (32 - r))); + tmp = (mask & (map[n] << r)); + if (n + 1 < lst->total_num) + tmp |= (mask & (map[n + 1] >> (32 - r))); tetri <<= lst->width; r += size; } @@ -94,7 +96,7 @@ void add_remove(unsigned int *map, t_fillist *list, int size) while (j >= list->position) { map[(j - 1) / 32] ^= (mask & tetri << (16 + i)) >> (j - 1); - if (map[(j - 1) / 32 + 1]) + if ((j - 1) / 32 + 1 < list->total_num) map[(j - 1) / 32 + 1] ^= (mask & tetri << (16 + i)) << (32 - j) << 1; j -= size; i -= list->width; diff --git a/fillit.h b/fillit.h index 067ab24..2be37b9 100644 --- a/fillit.h +++ b/fillit.h @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/01 13:34:46 by vmanzoni #+# #+# */ -/* Updated: 2019/05/28 12:15:39 by hulamy ### ########.fr */ +/* Updated: 2019/05/28 16:51:44 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,7 +52,6 @@ ** place : position sur l'axe des abscisses de la map (position % size) ** rank : position de 1 a 32 dans l'int du tableau d'int (position % 32) ** num : memorise dans quel int du tableau on se trouve (position / 32) -** total_num : le nombre d'int dans le tableau d'int ** test : ** letter : ** dope : @@ -70,6 +69,7 @@ typedef struct s_fillist int place; int rank; int num; + int total_num; int test; char letter; int *dope; diff --git a/main.c b/main.c index f349f08..88f67f0 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/02/12 13:20:48 by vmanzoni #+# #+# */ -/* Updated: 2019/05/27 18:21:58 by hulamy ### ########.fr */ +/* Updated: 2019/05/28 17:26:07 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -83,12 +83,7 @@ int main(int ac, char **av) if (ac == 2 || mdp) { if (!(input = read_file(av[1]))) - { - if (dope[3]) - print_error("error: Could not read file.\n"); - else - print_error("error\n"); - } + print_error(dope[3] ? "error: Could not read file.\n" : "error\n"); check_file_errors(input, dope); size = parse_input(input, &list, dope); print_final_map(list, size);