Skip to content

Commit

Permalink
Merge pull request #532 from biralavor/531-fix-leak-at-heredoc-when-s…
Browse files Browse the repository at this point in the history
…igint

fixing leak and invalid read when SIG is activated in HereDoc
  • Loading branch information
biralavor authored Oct 10, 2024
2 parents dc69b21 + 41d2e30 commit 9f17c70
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ go: all
gdb: all
gdb --tui -ex 'b main' -ex 'set detach-on-fork off' -ex 'info inferiors' -ex 'run > /dev/null 2>&1' ./$(NAME)

val: all
val: re
valgrind --leak-check=full --track-origins=yes --trace-children-skip='*/bin/*,*/sbin/*,/usr/bin/*' --trace-children=yes --track-fds=yes --show-reachable=yes --suppressions=readline.sup ./$(NAME)

.PHONY: all clean fclean re bonus min val gdb
5 changes: 2 additions & 3 deletions headers/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: umeneses <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 12:53:52 by tmalheir #+# #+# */
/* Updated: 2024/10/09 22:02:55 by umeneses ### ########.fr */
/* Updated: 2024/10/10 09:28:11 by umeneses ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -126,8 +126,7 @@ t_token_list *find_dst_node(t_token_list *lst, int dst_idx);

void manage_heredoc(t_token_list *lst);
void path_file(t_token_list *lst);
int check_delimiter(char *delimiter, int fd, char *input,
int line);
int check_delimiter(char *delimiter, int fd);

void heredoc_fd_reset(int *heredoc_fd);
bool is_heredoc_running(bool update, bool caller);
Expand Down
36 changes: 18 additions & 18 deletions src/02_parser/20.manage_heredoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: umeneses <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/12 18:23:53 by umeneses #+# #+# */
/* Updated: 2024/10/09 22:47:33 by umeneses ### ########.fr */
/* Updated: 2024/10/10 09:30:18 by umeneses ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,36 +16,38 @@ void manage_heredoc(t_token_list *lst)
{
int heredoc_fd;
int flag;
char *heredoc_input;
char *delimiter;
t_token_list *tmp;
int line;
int original_stdin;

line = 0;

flag = 0;
heredoc_fd = -1;
heredoc_input = NULL;
delimiter = NULL;
tmp = lst;
original_stdin = dup(STDIN_FILENO);
while (tmp->next)
while (tmp && tmp->next)
{
if (tmp->type == REDIR_HDOC)
{
delimiter = redir_quote_detector(ft_strdup(tmp->next->lexeme), &flag);
path_file(tmp);
if (delimiter == NULL)
{
ft_putendl_fd(" syntax error near unexpected token `newline'", STDERR_FILENO);
exit_status_holder(2, true);
break ;
}
path_file(lst);
heredoc_fd_reset(&heredoc_fd);
heredoc_fd = open(tmp->next->lexeme, O_CREAT | O_RDWR | O_TRUNC, 0644);
if (heredoc_fd_error_runner(heredoc_fd))
break ;
check_delimiter(delimiter, heredoc_fd);
free(delimiter);
if (g_sigmonitor == SIGINT)
{
dup2(heredoc_fd, STDIN_FILENO);
break ;
}
check_delimiter(delimiter, heredoc_fd, heredoc_input, line);
free(delimiter);
}
tmp = tmp->next;
}
Expand Down Expand Up @@ -79,16 +81,14 @@ void path_file(t_token_list *lst)
free(pathname);
}

int check_delimiter(char *delimiter, int fd, char *input, int line)
int check_delimiter(char *delimiter, int fd)
{
int idx;
int idx;
int line;
char *input;

if (delimiter == NULL)
{
free(input);
ft_putendl_fd(" syntax error near unexpected token `newline'", STDERR_FILENO);
return (exit_status_holder(2, true));
}
line = 0;
input = NULL;
while (true)
{
is_heredoc_running(true, true);
Expand Down

0 comments on commit 9f17c70

Please sign in to comment.