From 2ebbeb426d0b301c83987aa1f8a56a20c16fb923 Mon Sep 17 00:00:00 2001 From: mateusmoutinho Date: Tue, 18 Mar 2025 14:17:08 -0300 Subject: [PATCH 1/2] fixed white space --- src/core/parser.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/core/parser.c b/src/core/parser.c index 94c6544..b3dbe6e 100644 --- a/src/core/parser.c +++ b/src/core/parser.c @@ -22,17 +22,20 @@ ParsedCommand *parse_command(char *input) { char *input_copy = strdup(input); if (!input_copy) return NULL; + char *first_addres_of_input_copy = input_copy; input_copy = trim_whitespace(input_copy); if (strlen(input_copy) == 0) { PARSER_DEBUG("Empty command after trimming"); - free(input_copy); + free(first_addres_of_input_copy); + first_addres_of_input_copy =NULL; return NULL; } ParsedCommand *cmd = calloc(1, sizeof(ParsedCommand)); if (!cmd) { PARSER_DEBUG("Failed to allocate ParsedCommand"); - free(input_copy); + free(first_addres_of_input_copy); + first_addres_of_input_copy =NULL; return NULL; } @@ -40,14 +43,13 @@ ParsedCommand *parse_command(char *input) { cmd->args = calloc(MAX_ARGS, sizeof(char *)); if (!cmd->args) { PARSER_DEBUG("Failed to allocate args array"); - free(input_copy); + free(first_addres_of_input_copy); + first_addres_of_input_copy = NULL; free(cmd); return NULL; } - int arg_count = 0; char *token, *saveptr = NULL; - // Tokenize and process input token = strtok_r(input_copy, " \t", &saveptr); while (token != NULL && arg_count < MAX_ARGS - 1) { @@ -81,13 +83,17 @@ ParsedCommand *parse_command(char *input) { // Get next token token = strtok_r(NULL, " \t", &saveptr); } - // Ensure NULL termination cmd->args[arg_count] = NULL; PARSER_DEBUG("Command parsed with %d arguments", arg_count); - - free(input_copy); + + if(first_addres_of_input_copy){ + free(first_addres_of_input_copy); + first_addres_of_input_copy = NULL; + } + + return cmd; } From 26b69e81118d2cc358a9a2a07908a301ca51c646 Mon Sep 17 00:00:00 2001 From: Chandra Irugalbandara <52673571+chandralegend@users.noreply.github.com> Date: Thu, 20 Mar 2025 19:40:02 +0530 Subject: [PATCH 2/2] Update parser.c --- src/core/parser.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/core/parser.c b/src/core/parser.c index b3dbe6e..cad7938 100644 --- a/src/core/parser.c +++ b/src/core/parser.c @@ -22,20 +22,20 @@ ParsedCommand *parse_command(char *input) { char *input_copy = strdup(input); if (!input_copy) return NULL; - char *first_addres_of_input_copy = input_copy; + char *original_input_copy = input_copy; input_copy = trim_whitespace(input_copy); if (strlen(input_copy) == 0) { PARSER_DEBUG("Empty command after trimming"); - free(first_addres_of_input_copy); - first_addres_of_input_copy =NULL; + free(original_input_copy); + original_input_copy = NULL; return NULL; } ParsedCommand *cmd = calloc(1, sizeof(ParsedCommand)); if (!cmd) { PARSER_DEBUG("Failed to allocate ParsedCommand"); - free(first_addres_of_input_copy); - first_addres_of_input_copy =NULL; + free(original_input_copy); + original_input_copy = NULL; return NULL; } @@ -43,8 +43,8 @@ ParsedCommand *parse_command(char *input) { cmd->args = calloc(MAX_ARGS, sizeof(char *)); if (!cmd->args) { PARSER_DEBUG("Failed to allocate args array"); - free(first_addres_of_input_copy); - first_addres_of_input_copy = NULL; + free(original_input_copy); + original_input_copy = NULL; free(cmd); return NULL; } @@ -88,9 +88,9 @@ ParsedCommand *parse_command(char *input) { PARSER_DEBUG("Command parsed with %d arguments", arg_count); - if(first_addres_of_input_copy){ - free(first_addres_of_input_copy); - first_addres_of_input_copy = NULL; + if(original_input_copy){ + free(original_input_copy); + original_input_copy = NULL; } @@ -109,4 +109,4 @@ void free_parsed_command(ParsedCommand *cmd) { free(cmd->input_file); free(cmd->output_file); free(cmd); -} \ No newline at end of file +}