fix: lexer_context is now freed on errors

This commit is contained in:
matteo 2026-01-31 10:59:45 +01:00
parent 64e3fb26c3
commit d95b0fd220
3 changed files with 16 additions and 16 deletions

View file

@ -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)

View file

@ -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
{

View file

@ -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;
}