Commit graph

439 commits

Author SHA1 Message Date
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
e033909819 makefile: generate depfiles for the correct object files
The depfiles were completely ineffective.

Now this is fixed.

The -MT argument sets the path for the dependenency root. By default it is set
only to a filename, and thus does not include the full path of the file.
2025-04-15 14:41:43 +02:00
9a58198303 parsing: refactor minishell_redirect_parse to own file 2025-04-15 14:41:43 +02:00
d4197fec38 parsing: create specialised function to create a t_cmd 2025-04-15 14:41:43 +02:00
fde0bf4dc9 parsing: refactor: simple norm linecount gains 2025-04-15 14:41:43 +02:00
055dabc546 parsing: add newline at end of error message 2025-04-15 14:41:43 +02:00
dbba36dc44 parsing: split minishell_group_or_simple_parse 2025-04-15 14:41:43 +02:00
86e658fcca parsing: rename minishell_group_or_smp_parse -> minishell_group_or_simple_parse 2025-04-15 14:41:43 +02:00
283458b24f parsing: put redirect specific functions into own file 2025-04-15 14:41:43 +02:00
54f6c2a609 parsing: remove debugs 2025-04-15 14:41:43 +02:00
0c489f25bb parsing: fix some easy leaks 2025-04-15 14:41:43 +02:00
d40b6c3586
debug, destroy all new types (also some code for testing it) 2025-04-15 14:41:43 +02:00
53693e171e parsing: do not use a builder struct
since only one field was being used, just pass that field around
2025-04-14 14:53:31 +02:00
7425195350 norm: fix all easy issues 2025-04-14 14:44:42 +02:00
1c9653a5a5 parsing: parse redirections 2025-04-14 14:44:42 +02:00
1866da6ea6
fix: make it compile 2025-04-11 16:57:31 +02:00
Jérôme Guélen
4baad88a44
parse-cmd: Only redirections left to parse. 2025-04-10 18:57:04 +02:00
Jérôme Guélen
926774846f
parse-cmd: Almost done but stopped by the gong 2025-04-08 19:56:46 +02:00
Jérôme Guélen
d0cea8828d
parse-cmd: Mainly adding destructors and a token type. 2025-04-08 16:29:57 +02:00
Jérôme Guélen
72714855f7
parse-cmd: fixes and partial norm. 2025-04-08 13:08:43 +02:00
Jérôme Guélen
10a3c9c411
parse-cmd: Various fixes and a rotation of the parsing tree 2025-04-08 12:14:38 +02:00
Jerome
90d213bf98 parse-cmd: Slow start. Ideas starting to coalesce. 2025-04-06 21:52:33 +02:00
Jerome
9acee63d22 parse-cmd: grammar.md 2025-04-06 15:49:52 +02:00
Jerome
1a22a39336 parse-cmd: fix typo and grammar.md appearance 2025-04-06 15:49:52 +02:00
Jérôme Guélen
fea514ea63 parse-cmd: Just a forgotten check to be reviewed later. 2025-04-06 15:49:52 +02:00
Jérôme Guélen
5f95751b36 parse-cmd: A very basic skeleton of functions to come 2025-04-06 15:49:52 +02:00
Jérôme Guélen
f490cc22f5 Parsing: Added a document to describe the underlying grammar 2025-04-06 15:49:52 +02:00
Jérôme Guélen
4d9fabc794 Parse-cmd: Added a builder structure (unfinished) 2025-04-06 15:49:52 +02:00
Jérôme Guélen
10d7b65b08 Parse-cmd: fix and add comment 2025-04-06 15:49:52 +02:00
0a80b9fbe3
simple_cmd executing refactor: put builtin related utilities into own file 2025-04-01 16:38:41 +02:00
ba4192bf8d
export: set correct exit status 2025-04-01 15:58:56 +02:00
3cdf7c3f76
export: handle invalid identifiers 2025-04-01 15:54:46 +02:00
299e016a27
export: handle simple cases 2025-04-01 14:39:02 +02:00