From d95b0fd220fa8f395b8945b82dadf4f95fa69dff Mon Sep 17 00:00:00 2001 From: matteo Date: Sat, 31 Jan 2026 10:59:45 +0100 Subject: [PATCH] fix: lexer_context is now freed on errors --- src/lexer/lexer_utils.c | 15 +++++++-------- src/lexer/lexer_utils.h | 2 +- src/main.c | 15 ++++++++------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/lexer/lexer_utils.c b/src/lexer/lexer_utils.c index babf9da..6c88a0c 100644 --- a/src/lexer/lexer_utils.c +++ b/src/lexer/lexer_utils.c @@ -257,16 +257,15 @@ struct token *new_token(char *begin, ssize_t size, struct token_info *info) return tok; } -void destroy_lexer_context(struct lexer_context **ctx) +void destroy_lexer_context(struct lexer_context *ctx) { - if (ctx == NULL || *ctx == NULL) + if (ctx == NULL) return; - if ((*ctx)->previous_token != NULL) - free((*ctx)->previous_token); - if ((*ctx)->current_token != NULL) - free((*ctx)->current_token); - free(*ctx); - *ctx = NULL; + if (ctx->previous_token != NULL) + free(ctx->previous_token); + if (ctx->current_token != NULL) + free(ctx->current_token); + free(ctx); } void free_token(struct token **tok) diff --git a/src/lexer/lexer_utils.h b/src/lexer/lexer_utils.h index 364f7cc..32b3793 100644 --- a/src/lexer/lexer_utils.h +++ b/src/lexer/lexer_utils.h @@ -16,7 +16,7 @@ struct lexer_context /* @brief: frees all fields of ctx and sets ctx to NULL. */ -void destroy_lexer_context(struct lexer_context **ctx); +void destroy_lexer_context(struct lexer_context *ctx); enum lexing_mode { diff --git a/src/main.c b/src/main.c index 6b0f59f..1f0fc93 100644 --- a/src/main.c +++ b/src/main.c @@ -21,9 +21,10 @@ /* @brief: frees the hash map. * @return: always ERR_INPUT_PROCESSING. */ -static int err_input(struct hash_map **vars) +static int err_input(struct hash_map **vars, struct lexer_context *ctx) { hash_map_free(vars); + destroy_lexer_context(ctx); return ERR_INPUT_PROCESSING; } @@ -64,7 +65,7 @@ static int main_loop(struct lexer_context *ctx, struct args_options *options, } if (command_ast == NULL) - return err_input(&vars); + return err_input(&vars, ctx); // === free @@ -90,7 +91,7 @@ int main(int argc, char **argv) if (return_code != 0) { print_usage(stderr, argv[0]); - return err_input(&vars); + return err_input(&vars, NULL); } // args_print(&options); @@ -105,7 +106,7 @@ int main(int argc, char **argv) { fprintf(stderr, "Error: Failed to configure IO Backend from arguments\n"); - return err_input(&vars); + return err_input(&vars, NULL); } // Init IO Backend (with the context struct) @@ -115,13 +116,13 @@ int main(int argc, char **argv) fprintf(stderr, "Error: IO Backend initialization failed with code %d\n", return_code); - return err_input(&vars); + return err_input(&vars, NULL); } // init lexer context - struct lexer_context ctx = { 0 }; + struct lexer_context *ctx = calloc(1, sizeof(struct lexer_context)); - return_code = main_loop(&ctx, &options, vars); + return_code = main_loop(ctx, &options, vars); return return_code; }