Fix some bugs
authorLukas Jiriste <ljiriste@student.42prague.com>
Sat, 15 Jun 2024 12:34:03 +0000 (14:34 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sat, 15 Jun 2024 12:34:03 +0000 (14:34 +0200)
These bugs include:
 infinite loop
 not freeing alocated memory
 not initializing a struct before it may be returned

ft_parse/ft_parse.c

index 259a826c60fa209b17a0a963ea9216bc838f25b7..ca4180c63cc44a6eb36602d97aa2e7e764e8e038 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/20 20:51:36 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/06/15 10:06:32 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/06/15 14:32:50 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -132,7 +132,7 @@ static size_t       get_lookahead_size(t_vec *tokens)
        while (i < tokens->size)
        {
                token = (t_token *)ft_vec_access(tokens, i);
-               if (ft_strcmp(token->type, "$"))
+               if (!ft_strcmp(token->type, "$"))
                        return (i + 1);
                ++i;
        }
@@ -202,10 +202,13 @@ static t_vec      parse_header(const char *header)
                if (!token.type || ft_vec_append(&tokens, &token) != success)
                {
                        free(token.type);
+                       free(condensed_line);
                        ft_vec_free(&tokens, free_token);
                        return (tokens);
                }
+               ++i;
        }
+       free(condensed_line);
        return (tokens);
 }
 
@@ -222,10 +225,7 @@ t_parsing_table    ft_load_parsing_table(const char *filename,
        if (load_rules(&table.rules, rules_filename))
                return (table);
        fd = open(filename, O_RDONLY);
-       if (fd < 0 ||
-               ft_vec_init(&table.rules, sizeof(t_grammar_rule)) ||
-               ft_vec_init(&table.states, sizeof(t_parser_state))  ||
-               load_rules(&table.rules, rules_filename))
+       if (fd < 0)
                return (table);
        line = get_next_line(fd);
        table.tokens = parse_header(line);