From 2a5c8706c87769c79aefdbe45180d7294b2552d8 Mon Sep 17 00:00:00 2001 From: SIGMazer Date: Thu, 16 Jan 2025 22:41:47 +0200 Subject: [PATCH] fix(array): memory leaks in ExpressionList Signed-off-by: SIGMazer --- ast.c | 12 ++++++++---- lang.y | 2 ++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/ast.c b/ast.c index ac3e03f..b8da176 100644 --- a/ast.c +++ b/ast.c @@ -2926,12 +2926,16 @@ size_t count_expression_list(ExpressionList* list) void free_expression_list(ExpressionList* list) { - while (list) + if (!list) + return; + ExpressionList* current = list->next; + while (current != list) { - ExpressionList* next = list->next; - free(list); - list = next; + ExpressionList* next = current->next; + free(current); + current = next; } + free(list); } void populate_array_varialbe(char* name, ExpressionList* list) diff --git a/lang.y b/lang.y index d337aa7..9f48755 100644 --- a/lang.y +++ b/lang.y @@ -228,12 +228,14 @@ declaration: set_array_variable($3, count_expression_list($7), get_current_modifiers(), $2); populate_array_varialbe($3, $7); $$ = create_array_declaration_node($3, count_expression_list($7), $2); + free_expression_list($7); } | optional_modifiers type IDENTIFIER LBRACKET INT_LITERAL RBRACKET EQUALS array_init { set_array_variable($3, $5, get_current_modifiers(), $2); populate_array_varialbe($3, $8); $$ = create_array_declaration_node($3, $5, $2); + free_expression_list($8); } ;