fix: strdup a la con

This commit is contained in:
Gu://em_ 2026-01-30 21:27:23 +01:00
parent f721e6db72
commit b663655d53
2 changed files with 7 additions and 16 deletions

View file

@ -304,13 +304,12 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
// Get element type // Get element type
if (ast_is_word(element)) if (ast_is_word(element))
{ {
// Extract word
struct ast_word *element_word = ast_get_word(element); struct ast_word *element_word = ast_get_word(element);
char *word = element_word->word;
// TODO test this fix for the memory leaks element_word->word = NULL; // Prevents word to be freed
char *word = strdup(element_word->word);
ast_free(&element); ast_free(&element);
command_elements = list_append(command_elements, word); command_elements = list_append(command_elements, word);
// end of fix
} }
else if (ast_is_redir(element)) else if (ast_is_redir(element))
{ {
@ -344,20 +343,11 @@ struct ast *parse_element(struct lexer_context *ctx)
{ {
POP_TOKEN(); POP_TOKEN();
char *word = strdup(token->data); struct ast *result = ast_create_word(token->data);
if (word == NULL)
{
perror("Internal error: could not copy token data (is your memory "
"full ?)");
return NULL;
}
struct ast *result = ast_create_word(word);
if (result == NULL) if (result == NULL)
{ {
perror("Internal error: could not create ast node (is your memory " perror("Internal error: could not create ast node (is your memory "
"full ?)"); "full ?)");
free(word);
return NULL; return NULL;
} }

View file

@ -2,7 +2,6 @@
#include "ast_word.h" #include "ast_word.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -43,6 +42,8 @@ void ast_free_word(struct ast_word *ast_node)
if (ast_node == NULL) if (ast_node == NULL)
return; return;
if (ast_node->word != NULL)
free(ast_node->word); free(ast_node->word);
free(ast_node); free(ast_node);
} }