From aa2b11633584be6cf47e85f768e8e20ba669bec4 Mon Sep 17 00:00:00 2001 From: savalet Date: Fri, 9 May 2025 16:24:07 +0200 Subject: [PATCH] Fix leaks Fix leaks Fix leaks --- src/ast/ast_utils.c | 29 +++++++++++++++++++++-------- src/exec.c | 5 +++-- src/main.c | 6 +++++- src/shell.c | 2 +- src/utils.h | 1 + src/utils/free.c | 20 ++++++++++++++++---- 6 files changed, 47 insertions(+), 16 deletions(-) diff --git a/src/ast/ast_utils.c b/src/ast/ast_utils.c index 35d5f5a..83561c5 100644 --- a/src/ast/ast_utils.c +++ b/src/ast/ast_utils.c @@ -39,12 +39,12 @@ ast_t *create_node(ast_ctx_t *ctx) ast_t *new_ast; if (ctx->ast == NULL) - return NULL; + return nullptr; if (ctx->sz + 1 == ctx->cap) { new_ast = u_realloc(ctx->ast, sizeof *ctx->ast * ctx->sz, sizeof *ctx->ast * (ctx->cap << 1)); if (new_ast == NULL) - return NULL; + return nullptr; ctx->ast = new_ast; ctx->cap <<= 1; } @@ -53,13 +53,26 @@ ast_t *create_node(ast_ctx_t *ctx) return ctx->ast + ctx->sz - 1; } +static +void free_ast_nodes(ast_t *ast) +{ + if (ast == nullptr) + return; + if (ast->type == N_BIN) { + free_ast_nodes(ast->binary.left); + free_ast_nodes(ast->binary.right); + } + if (ast->type == N_CMD) + free(ast->vector.tokens); + if (ast->type == N_LST) { + for (size_t i = 0; i < ast->list.sz; i++) + free_ast_nodes(ast->list.nodes[i]); + free(ast->list.nodes); + } +} + void free_ast(ast_ctx_t *ctx) { - for (size_t i = 0; i < ctx->sz; i++) { - if (ctx->first_node[i].type == N_LST) - free((void *)ctx->first_node[i].list.nodes); - if (ctx->first_node[i].type == N_CMD) - free(ctx->first_node[i].vector.tokens); - } + free_ast_nodes(ctx->ast); free(ctx->first_node); } diff --git a/src/exec.c b/src/exec.c index 8a69a82..49ad91e 100644 --- a/src/exec.c +++ b/src/exec.c @@ -24,6 +24,7 @@ #include "repl.h" #include "u_mem.h" #include "u_str.h" +#include "utils.h" const builtins_funcs_t BUILTINS[] = { { "builtins", &builtins_builtins }, @@ -140,7 +141,7 @@ int launch_bin(char *full_bin_path, char **args, ef_t *ef) if (execve(full_bin_path, args, ef->env->env) < 0) { status = command_error(full_bin_path, args, errno); free_env(ef->env); - exit(((free((void *)args), free(ef->buffer)), status)); + exit(((free_args(args), free(ef->buffer)), status)); } } if (!(ef->flags & F_PIPE) || ef->p_i == ef->p_sz - 1) @@ -217,7 +218,7 @@ int execute(ef_t *ef) if (!args) return RETURN_FAILURE; exec_the_args(ef, args); - free((void *)args); + free_args(args); init_shell_repl(ef->exec_ctx); return ef->exec_ctx->history->last_exit_code != 0 ? RETURN_FAILURE : RETURN_SUCCESS; diff --git a/src/main.c b/src/main.c index 8d6ad83..7117864 100644 --- a/src/main.c +++ b/src/main.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "common.h" #include "debug.h" @@ -49,9 +50,12 @@ bool parse_args(opt_t *opt, int ac, char **av) int main(int ac, char **av, char **env) { opt_t opt = { 0, .script_file = nullptr, .cmd = nullptr }; + int result; U_DEBUG_MSG("Debug mode altivated.\n"); if (!parse_args(&opt, ac, av)) return RETURN_FAILURE; - return shell(&opt, env); + result = shell(&opt, env); + free(opt.cmd); + return result; } diff --git a/src/shell.c b/src/shell.c index 88541d3..d486d99 100644 --- a/src/shell.c +++ b/src/shell.c @@ -92,7 +92,7 @@ int shell_loop(int is_a_tty, exec_ctx_t *exec_ctx) while (true) { write_prompt(is_a_tty, exec_ctx); if (!change_shell_command(&buff, exec_ctx)) - return exec_ctx->history->last_exit_code; + return free(buff.str), exec_ctx->history->last_exit_code; } return free(buff.str), exec_ctx->history->last_exit_code; } diff --git a/src/utils.h b/src/utils.h index 703c7cb..0197ed3 100644 --- a/src/utils.h +++ b/src/utils.h @@ -17,4 +17,5 @@ char *cat_in_str(his_variable_t *his_variable, char *str, char *cpy); int len_array(char **array); char *insert_str(const char *base, const char *insert, size_t pos); void free_everything(exec_ctx_t *exec_ctx); +void free_args(char **args); #endif /* UTILS_H */ diff --git a/src/utils/free.c b/src/utils/free.c index d448768..e5136ff 100644 --- a/src/utils/free.c +++ b/src/utils/free.c @@ -9,9 +9,21 @@ #include "exec.h" -void free_everything(exec_ctx_t *exec_ctx) +void free_args(char **args) { - free_env(exec_ctx->env); - free_alias(exec_ctx->alias); - free(exec_ctx->history_command); + for (size_t i = 0; args[i] != nullptr; i++) + free(args[i]); + free((void *)args); +} + +void free_everything(exec_ctx_t *ec) +{ + free_env(ec->env); + free_alias(ec->alias); + for (int i = 0; i < ec->history_command->sz; i++) { + free(ec->history_command[i].command); + free(ec->history_command[i].arg); + } + free(ec->history_command); + free(ec->local->local_var); }