redirection parsing: detect malformed redirection

This commit is contained in:
Khaïs COLIN 2025-03-10 16:40:02 +01:00
parent 06dd3c3e83
commit 5df876bba3
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
4 changed files with 26 additions and 8 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 12:40:46 by khais #+# #+# */
/* Updated: 2025/02/21 15:38:54 by khais ### ########.fr */
/* Updated: 2025/03/10 16:56:52 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -51,6 +51,8 @@ char *ft_strerror(t_errno errno)
[FT_EINVAL] = "Invalid argument",
[FT_EBADID] = "Bad identifier",
[FT_EUNEXPECTED_PIPE] = "minishell: syntax error near unexpected token `|'",
[FT_EMALFORMED_REDIRECTION] = "minishell: malformed redirection (perhaps a \
missing filename)",
};
if (errno >= 0 && errno < FT_EMAXERRNO)

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 12:40:58 by khais #+# #+# */
/* Updated: 2025/02/21 15:37:56 by khais ### ########.fr */
/* Updated: 2025/03/10 16:38:28 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,6 +21,7 @@ typedef enum e_errno
FT_EINVAL,
FT_EBADID,
FT_EUNEXPECTED_PIPE,
FT_EMALFORMED_REDIRECTION,
FT_EMAXERRNO,
} t_errno;

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/07 12:30:04 by khais #+# #+# */
/* Updated: 2025/03/10 16:11:46 by khais ### ########.fr */
/* Updated: 2025/03/10 16:40:17 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,6 +15,7 @@
#include "redirection_list.h"
#include <stdlib.h>
#include "redirection_type.h"
#include "../../ft_errno.h"
#include <unistd.h>
/*
@ -53,6 +54,8 @@ struct s_simple_cmd *parse_redirections(struct s_simple_cmd *cmd)
{
wordlist_destroy_idx(&cmd->words, i);
marker = wordlist_pop_idx(&cmd->words, i);
if (marker == NULL)
return (ft_errno(FT_EMALFORMED_REDIRECTION), NULL);
redirection = redirection_create(type, marker);
if (redirection == NULL)
return (NULL);
@ -60,8 +63,7 @@ struct s_simple_cmd *parse_redirections(struct s_simple_cmd *cmd)
if (cmd->redirections == NULL)
return (redirection_destroy(redirection), NULL);
}
else
i++;
i++;
}
return (cmd);
}

View file

@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/10 16:13/18 by khais #+# #+# */
/* Updated: 2025/03/10 16:13:18 by khais ### ########.fr */
/* Created: 2025/03/10 16:38/00 by khais #+# #+# */
/* Updated: 2025/03/10 16:38:00 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,7 +16,9 @@
#include "../src/postprocess/redirections/redirection_parsing.h"
#include "../src/postprocess/redirections/redirection_parsing.h"
#include "../src/postprocess/redirections/redirection_list.h"
#include "../src/ft_errno.h"
#include "ft_printf.h"
#include "stdio.h"
#include "testutil.h"
static t_simple_cmd *parse_simple_cmd(char *input)
@ -84,13 +86,24 @@ static void test_redirection_parsing_output_in_middle(void)
do_leak_check();
}
static void test_redirection_parsing_output_no_filename_at_end(void)
{
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
ft_errno(FT_ESUCCESS);
t_simple_cmd *cmd = parse_simple_cmd("echo hello world >");
assert(parse_redirections(cmd) == NULL);
assert(FT_EMALFORMED_REDIRECTION == ft_errno_get());
simple_cmd_destroy(cmd);
do_leak_check();
}
int main(void) {
test_redirection_parsing_no_redirections();
test_redirection_parsing_output_at_start();
test_redirection_parsing_output_at_end();
test_redirection_parsing_output_in_middle();
// check operator alone without further tokens
test_redirection_parsing_output_no_filename_at_end();
// check null input
return (0);
}