mirror of
https://github.com/Savapitech/42sh.git
synced 2026-03-18 21:50:35 +01:00
Fix leaks
Fix leaks Fix leaks
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <getopt.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user