diff --git a/src/parser/grammar_basic.c b/src/parser/grammar_basic.c index dcd6b31..df103fc 100644 --- a/src/parser/grammar_basic.c +++ b/src/parser/grammar_basic.c @@ -304,13 +304,12 @@ struct ast *parse_simple_command(struct lexer_context *ctx) // Get element type if (ast_is_word(element)) { + // Extract word struct ast_word *element_word = ast_get_word(element); - - // TODO test this fix for the memory leaks - char *word = strdup(element_word->word); + char *word = element_word->word; + element_word->word = NULL; // Prevents word to be freed ast_free(&element); command_elements = list_append(command_elements, word); - // end of fix } else if (ast_is_redir(element)) { @@ -344,20 +343,11 @@ struct ast *parse_element(struct lexer_context *ctx) { POP_TOKEN(); - char *word = strdup(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); + struct ast *result = ast_create_word(token->data); if (result == NULL) { perror("Internal error: could not create ast node (is your memory " "full ?)"); - free(word); return NULL; } diff --git a/src/utils/ast/ast_word.c b/src/utils/ast/ast_word.c index 6870c50..d83489c 100644 --- a/src/utils/ast/ast_word.c +++ b/src/utils/ast/ast_word.c @@ -2,7 +2,6 @@ #include "ast_word.h" #include -#include #include #include @@ -43,6 +42,8 @@ void ast_free_word(struct ast_word *ast_node) if (ast_node == NULL) return; - free(ast_node->word); + if (ast_node->word != NULL) + free(ast_node->word); + free(ast_node); }