Skip to content

Commit 02cefeb

Browse files
author
Ahmed Alsahfy
committed
Fixed heredoc fd-leaks when SIGINT'd
1 parent 227906f commit 02cefeb

File tree

7 files changed

+33
-11
lines changed

7 files changed

+33
-11
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ NAME := minishell
44

55
# Necessities
66
CC := cc
7-
CFLAGS = -Wall -Wextra -Werror -Wpedantic -g3 -fsanitize=address,undefined
7+
CFLAGS = -Wall -Wextra -Werror -Wpedantic -g3 #-fsanitize=address,undefined
88
OS := $(shell uname)
99

1010
Dar = Darwin

includes/interpreter.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* interpreter.h :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: ebinjama <ebinjama@student.42abudhabi.ae> +#+ +:+ +#+ */
6+
/* By: aalshafy <aalshafy@student.42abudhabi.a +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/06/18 09:53:03 by ebinjama #+# #+# */
9-
/* Updated: 2024/06/26 14:27:09 by ebinjama ### ########.fr */
9+
/* Updated: 2024/06/26 15:41:12 by aalshafy ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -135,6 +135,7 @@ void perform_wait_and_fetch_wstatus(t_shcontext *mshcontext);
135135
int determine_exit_code(t_shcontext *mshcontext);
136136
int store_heredoc_input(t_astnode *lredir, int *pipedes,
137137
t_shcontext *mshcontext);
138+
void close_heredoc_recursively(t_astnode *node);
138139
void perform_word_checks_and_close_pipes_if_needed(t_astnode *word,
139140
t_shcontext *mshcontext, char **envp, t_node *envl);
140141
void ignore_signals_and_close_pipes_if_needed_then_set_pid(t_astnode *word,

srcs/interpreter/interpreter.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: aalshafy <[email protected] +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/04/12 02:40:13 by ebinjama #+# #+# */
9-
/* Updated: 2024/06/25 20:56:37 by aalshafy ### ########.fr */
9+
/* Updated: 2024/06/26 15:40:33 by aalshafy ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -20,6 +20,8 @@ static void visit(t_astnode *node, t_node *envl,
2020
static void find_rightmost_word(t_astnode *root, t_astnode **to_set);
2121
void restore_iodes(t_shcontext *mshcontext, bool clear);
2222

23+
24+
2325
int interpret(t_astnode *root, t_node *envl)
2426
{
2527
t_shcontext mshcontext;
@@ -32,7 +34,11 @@ int interpret(t_astnode *root, t_node *envl)
3234
find_rightmost_word(root, &mshcontext.rightmost_word);
3335
visit_prematurely(root, &mshcontext);
3436
if (mshcontext.terminate)
37+
{
38+
close_heredoc_recursively(root);
39+
restore_iodes(&mshcontext, true);
3540
return (*(int *)envl->content = 130);
41+
}
3642
visit(root, envl, &mshcontext);
3743
perform_wait_and_fetch_wstatus(&mshcontext);
3844
restore_iodes(&mshcontext, true);

srcs/interpreter/processor_norme_dump/interpret_dump.c

+15-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* interpret_dump.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: ebinjama <ebinjama@student.42abudhabi.ae> +#+ +:+ +#+ */
6+
/* By: aalshafy <aalshafy@student.42abudhabi.a +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/06/18 15:12:42 by ebinjama #+# #+# */
9-
/* Updated: 2024/06/21 19:24:29 by ebinjama ### ########.fr */
9+
/* Updated: 2024/06/26 15:40:37 by aalshafy ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -64,3 +64,16 @@ int determine_exit_code(t_shcontext *mshcontext)
6464
return (EXIT_FAILURE);
6565
return (EXIT_FAILURE);
6666
}
67+
68+
void close_heredoc_recursively(t_astnode *node)
69+
{
70+
if (!node)
71+
return ;
72+
close_heredoc_recursively(node->left);
73+
close_heredoc_recursively(node->right);
74+
if (node->type == TK_LREDIR && node->data.redirection.mode == O_APPEND)
75+
{
76+
close(node->data.redirection.fd[READ_END]);
77+
close(node->data.redirection.fd[WRITE_END]);
78+
}
79+
}

srcs/msh.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: aalshafy <[email protected] +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/06/26 13:35:21 by aalshafy #+# #+# */
9-
/* Updated: 2024/06/26 13:43:13 by aalshafy ### ########.fr */
9+
/* Updated: 2024/06/26 15:23:46 by aalshafy ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

srcs/parsing/parser.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: aalshafy <[email protected] +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/06/19 16:59:25 by aalshafy #+# #+# */
9-
/* Updated: 2024/06/23 09:56:26 by aalshafy ### ########.fr */
9+
/* Updated: 2024/06/26 15:11:31 by aalshafy ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -63,7 +63,7 @@ int parse_pipe(t_token **token_list, t_astnode **node)
6363
if (!(*token_list)->prev || ((*node) && ((*node)->type == TK_AND
6464
|| (*node)->type == TK_OR))
6565
|| (*token_list)->next->token_type == TK_PIPE)
66-
return (show_syntax_error((*token_list)->value), 2);
66+
return (show_syntax_error((*token_list)->value), 6);
6767
new_node = (t_astnode *)malloc(sizeof(t_astnode));
6868
if (new_node == NULL)
6969
return (1);
@@ -87,7 +87,7 @@ int parse_redir(t_token **token_list, t_astnode **node)
8787
t_astnode *new_node;
8888

8989
if (!(*token_list)->next || (*token_list)->next->token_type != TK_WORD)
90-
return (show_syntax_error("newline"), 2);
90+
return (show_syntax_error("newline"), 6);
9191
new_node = (t_astnode *)malloc(sizeof(t_astnode));
9292
if (new_node == NULL)
9393
return (1);

srcs/show_errors.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: aalshafy <[email protected] +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/06/25 17:22:25 by aalshafy #+# #+# */
9-
/* Updated: 2024/06/25 18:33:56 by aalshafy ### ########.fr */
9+
/* Updated: 2024/06/26 15:12:14 by aalshafy ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -52,6 +52,8 @@ void show_error(int exit_status)
5252
return ;
5353
else if (exit_status == 5)
5454
return ;
55+
else if (exit_status == 6)
56+
return ;
5557
if (msg)
5658
write(2, msg, ft_strlen(msg));
5759
}

0 commit comments

Comments
 (0)