fix: strdup a la con
This commit is contained in:
parent
f721e6db72
commit
b663655d53
2 changed files with 7 additions and 16 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
#include "ast_word.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue