Commit graph

471 commits

Author SHA1 Message Date
0579d1f2fc
env: correctly handle write errors such as /dev/full
The exit status of 125 for env in case of failure is documented in env(1).
2025-04-15 15:19:27 +02:00
ffaf2582a3
pwd: correctly handle write errors 2025-04-15 15:19:27 +02:00
cfc95994de
echo: correctly handle write errors such as /dev/full 2025-04-15 15:19:27 +02:00
871180f6d8
connec_pipe_cmd_execute: setup pipes between commands 2025-04-15 15:19:27 +02:00
81febcfcdd
debug: show command about to be executed in debug mode 2025-04-15 15:19:27 +02:00
204ed35ff4
fix(connec_cmd_execute): do not execute more than one command per process 2025-04-15 15:17:52 +02:00
596b21fc2f
debug, destroy all new types (also some code for testing it) 2025-04-15 15:17:51 +02:00
82da182773
fix: potential buffer overflow caused by missing terminating null byte in t_buffer
This only occurs in specific circumstances where the number of bytes to be added
to a t_buffer via the ft_buffer_push_buf function is exactly equal to the number
of free bytes in the current underlying buffer. This does not occur if the
number of bytes to add to the buffer is smaller than that, since we allocate new
space using ft_calloc.

In these circumstances, since no terminating null byte is added, other code may
read past the end of the buffer, causing a buffer overflow.
2025-04-15 15:17:51 +02:00
3ec90f7770
token_type: assign token_type to worddesc during wordsplit
Also added some tests for that part.
2025-04-15 15:17:51 +02:00
4d6a64bf6a
debug: worddesc show more information
This seems appropriate since worddescs are getting more and more complex:
markers, flags, tokentypes, ...
2025-04-15 15:17:51 +02:00
6576c47b56
exec: command to toggle execution
Execution starts out being enabled. But it can be toggled on and off by using
the .exec command.
2025-04-15 15:17:51 +02:00
ac411d36cc
debug: special command can turn debug mode on and off
Using the .debug command, debug mode can be toggled. It starts turned off.

When it is on, each command, before being executed, is debug-printed using the
tree-printing facilities.
2025-04-15 15:16:51 +02:00
18014cda98
token_type: add field with no logic for populating it 2025-04-15 15:15:30 +02:00
5716c4b3dc parsing: pass correct arguments to prevent use-after-free 2025-04-15 15:15:30 +02:00
ba9751e089
do postprocessing for each simple_cmd before execution 2025-04-15 15:13:17 +02:00
386d2bcb3a
exec: correct error and return value when cmd is a directory
minishell: /: Is a directory
$? = 126
2025-04-15 15:11:50 +02:00
d08c9a6727
tests: check simple noops 2025-04-15 15:11:50 +02:00
bb1390aac5
simple_cmd_execute: handle redirections
I am very uncertain on if this is actually correct, but I am unable to test it
very much atm
2025-04-15 15:11:50 +02:00
6f75f2d181
connec_cmd_execute: naive recursive pipe implementation (does not handle redirection) 2025-04-15 15:11:26 +02:00
df73b3d0c7
connec_cmd_execute: handle && and || 2025-04-15 15:11:26 +02:00
8feacccb15
group_cmd_execute: fork and passthrough to cmd_execute 2025-04-15 15:11:26 +02:00
425722801b
refactor(do_waitpid): put into own file for easier acces from other modules 2025-04-15 15:11:26 +02:00
f9f8a804a3
cmd_execute: execute simple commands 2025-04-15 15:11:11 +02:00
7af032539a
cmd_execute: initial scafholding 2025-04-15 15:11:11 +02:00
881c7f264c
signal: if a command is terminated by SIGQUIT, display that it core dumped 2025-04-15 15:11:11 +02:00
ceee5f99d4
signal: SIGQUIT prints ^\ to the terminal, and does not redisplay the prompt 2025-04-15 15:10:40 +02:00
10e8738336
signal: handle redisplaying the prompt correctly 2025-04-15 15:10:40 +02:00
3a309062d8
refactor(signal): remove unneded function
excve already resets signal handling to the default
2025-04-15 15:09:39 +02:00
cb8f64367d
minishell: exit with last command exit status 2025-04-15 15:09:39 +02:00
67b2347df9
signal: keep waiting for process to exit on SIGQUIT
the SA_RESTART flag causes syscalls such as waitpid to be resumed after a signal
is handled, which is what we want.
2025-04-15 15:08:06 +02:00
f0145d26f4
signal: do not exit shell on SIGINT, ignore SIGQUIT 2025-04-15 15:08:06 +02:00
be183b99ee
minishell: do wildcard expansion 2025-04-15 15:06:46 +02:00
f06f6e26e2
fix(expand_star): memory leak
You have to call closedir().

I also removed the errno checking to make some space, I don't think it is needed
when looking at the errors returned by these functions.
2025-04-15 15:05:52 +02:00
32e3976774
field splitting: implement field splitting 2025-04-15 15:05:52 +02:00
0cfa2677c5
fix(echo): echo - was treated as echo -n
Small logic error on my part
2025-04-15 15:03:13 +02:00
d40560bb37
unset: implement unset 2025-04-15 15:03:13 +02:00
1ef8b7a0ae
env: implement builtin env 2025-04-15 15:03:13 +02:00
0de583cf45
echo: implement builtin echo 2025-04-15 15:03:13 +02:00
715c2aced8
exit: handle invalid and large arguments 2025-04-15 15:03:13 +02:00
f1c132337b
exit: work for simple arguments
This does not address non-integer arguments, or extremly high/low arguments.
2025-04-15 15:03:13 +02:00
24ba87abba
exec: retain exit status of commands, including if they were signaled 2025-04-15 15:01:19 +02:00
5b7367925f
simple_cmd refactor: put subprocess functions in own file
This clears some space for new functions later
2025-04-15 14:41:44 +02:00
9e79a0829a
fix: do not read ahead in STDIN
The Open Group Base Specifications Issue 8 IEEE Std 1003.1-2024 sh — shell, the
standard command language interpreter says:

> When the shell is using standard input and it invokes a command that also uses
> standard input, the shell shall ensure that the standard input file pointer
> points directly after the command it has read when the command begins
> execution. It shall not read ahead in such a manner that any characters
> intended to be read by the invoked command are consumed by the shell (whether
> interpreted by the shell or not) or that characters that are not read by the
> invoked command are not seen by the shell.

We used the default BUFFER_SIZE for get_next_line of 1024, which caused us to
read ahead farther than was allowed by the Open Group Base Specification.

Setting BUFFER_SIZE=1 ensures that we don't read too far ahead, since
get_next_line will always immediatly stop once a newline is found.

This is for me the simplest way to solve this issue.
2025-04-15 14:41:44 +02:00
0842fdfe1d
fix: double free when getting command path for empty command
```
minishell$ ""
```
leads to the following error:
```
=================================================================
==1158770==ERROR: AddressSanitizer: attempting double-free on 0x5060000020c0 in thread T0:
    #0 0x5649fcb5b8a8 in free.part.0 asan_malloc_linux.cpp.o
    #1 0x5649fcbac549 in deal_with_filled_path /home/khais/src/42/common_env/minishell/src/subst/simple_filename_exp.c:119:3
    #2 0x5649fcbac38b in filepath_from_env /home/khais/src/42/common_env/minishell/src/subst/simple_filename_exp.c:165:10
    #3 0x5649fcbac220 in get_cmdpath /home/khais/src/42/common_env/minishell/src/subst/simple_filename_exp.c:186:10
    #4 0x5649fcba8664 in simple_cmd_execute /home/khais/src/42/common_env/minishell/src/executing/simple_cmd/simple_cmd_execute.c:71:8
    #5 0x5649fcba6e4c in execute_command /home/khais/src/42/common_env/minishell/src/minishell.c:49:2
    #6 0x5649fcba6ce5 in main /home/khais/src/42/common_env/minishell/src/minishell.c:92:3
    #7 0x7fb4f2dcd27d in __libc_start_call_main (/nix/store/nqb2ns2d1lahnd5ncwmn6k84qfd7vx2k-glibc-2.40-36/lib/libc.so.6+0x2a27d) (BuildId: 704cab5816f130c494208e8cc24d87a386ef085b)
    #8 0x7fb4f2dcd338 in __libc_start_main@GLIBC_2.2.5 (/nix/store/nqb2ns2d1lahnd5ncwmn6k84qfd7vx2k-glibc-2.40-36/lib/libc.so.6+0x2a338) (BuildId: 704cab5816f130c494208e8cc24d87a386ef085b)
    #9 0x5649fca6f394 in _start (/home/khais/src/42/common_env/minishell/minishell+0x2b394)

0x5060000020c0 is located 0 bytes inside of 58-byte region [0x5060000020c0,0x5060000020fa)
freed by thread T0 here:
    #0 0x5649fcb5b8a8 in free.part.0 asan_malloc_linux.cpp.o
    #1 0x5649fcbac669 in select_path /home/khais/src/42/common_env/minishell/src/subst/simple_filename_exp.c:81:3
    #2 0x5649fcbac524 in deal_with_filled_path /home/khais/src/42/common_env/minishell/src/subst/simple_filename_exp.c:116:9
    #3 0x5649fcbac38b in filepath_from_env /home/khais/src/42/common_env/minishell/src/subst/simple_filename_exp.c:165:10
    #4 0x5649fcbac220 in get_cmdpath /home/khais/src/42/common_env/minishell/src/subst/simple_filename_exp.c:186:10
    #5 0x5649fcba8664 in simple_cmd_execute /home/khais/src/42/common_env/minishell/src/executing/simple_cmd/simple_cmd_execute.c:71:8
    #6 0x5649fcba6e4c in execute_command /home/khais/src/42/common_env/minishell/src/minishell.c:49:2
    #7 0x5649fcba6ce5 in main /home/khais/src/42/common_env/minishell/src/minishell.c:92:3
    #8 0x7fb4f2dcd27d in __libc_start_call_main (/nix/store/nqb2ns2d1lahnd5ncwmn6k84qfd7vx2k-glibc-2.40-36/lib/libc.so.6+0x2a27d) (BuildId: 704cab5816f130c494208e8cc24d87a386ef085b)

previously allocated by thread T0 here:
    #0 0x5649fcb5c897 in malloc (/home/khais/src/42/common_env/minishell/minishell+0x118897)
    #1 0x5649fcbac6be in alloc_path /home/khais/src/42/common_env/minishell/src/subst/simple_filename_exp_utils.c:34:9
    #2 0x5649fcbac4e9 in deal_with_filled_path /home/khais/src/42/common_env/minishell/src/subst/simple_filename_exp.c:113:15
    #3 0x5649fcbac38b in filepath_from_env /home/khais/src/42/common_env/minishell/src/subst/simple_filename_exp.c:165:10
    #4 0x5649fcbac220 in get_cmdpath /home/khais/src/42/common_env/minishell/src/subst/simple_filename_exp.c:186:10
    #5 0x5649fcba8664 in simple_cmd_execute /home/khais/src/42/common_env/minishell/src/executing/simple_cmd/simple_cmd_execute.c:71:8
    #6 0x5649fcba6e4c in execute_command /home/khais/src/42/common_env/minishell/src/minishell.c:49:2
    #7 0x5649fcba6ce5 in main /home/khais/src/42/common_env/minishell/src/minishell.c:92:3
    #8 0x7fb4f2dcd27d in __libc_start_call_main (/nix/store/nqb2ns2d1lahnd5ncwmn6k84qfd7vx2k-glibc-2.40-36/lib/libc.so.6+0x2a27d) (BuildId: 704cab5816f130c494208e8cc24d87a386ef085b)

SUMMARY: AddressSanitizer: double-free asan_malloc_linux.cpp.o in free.part.0
==1158770==ABORTING
```

Looks like select_path frees filepath sometimes, no idea why. Removing these
lines didn't break any tests.

I'm pushing this to get a second opinion
2025-04-15 14:41:44 +02:00
cea8043ec9
simple_cmd executing: show error in case execve fails
We don't have to check the return status, since execve only returns on failure.

Fixes #52
2025-04-15 14:41:43 +02:00
4de1d0024a parsing: refactor out the last two functions needed to pass the norm 2025-04-15 14:41:43 +02:00
5e84e9a111 parsing: remove (at least some) dead code 2025-04-15 14:41:43 +02:00
218be08049 parsing: refactor group_cmd_parse into own file 2025-04-15 14:41:43 +02:00
b7871be426 parsing: refactor minishell_optional_pipeline_parse into own file 2025-04-15 14:41:43 +02:00
811ce3ef8e parsing: refactor minishell_simple_cmd_parse into own file 2025-04-15 14:41:43 +02:00