Commit graph

296 commits

Author SHA1 Message Date
74f013e5c6
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-02 19:27:30 +02:00
e84b88394a
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-02 19:27:30 +02:00
1df6a6ad9a
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-02 19:27:30 +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
b92aa33c4e
inttests: fix test runner sometimes reporting wrong todo count 2025-04-01 14:28:47 +02:00
bac3512898
minishell: do variable substitution in postprocessing 2025-04-01 13:50:14 +02:00
7153df9305
cd: print error if cd is called with too many arguments 2025-03-31 20:06:34 +02:00
a66b71e5ce
cd: ensure relative paths work as expected 2025-03-31 19:59:32 +02:00
f1b2e1e22d
cd: handle empty argument 2025-03-31 17:07:46 +02:00
8f3c15f096
cd: when going to $HOME, show error message if it is unset 2025-03-31 16:58:21 +02:00
e046d151a2
inttests: add ability to manipulate environment for a single test 2025-03-31 16:57:40 +02:00
d66ca10a0c
cd: with no arg, go to $HOME 2025-03-31 16:40:22 +02:00
e93e6efb93
builtins: cd: implement cd to absolute paths 2025-03-31 16:34:48 +02:00
5ce4a2b85f
builtin: implement pwd 2025-03-31 14:54:40 +02:00
1c4733b1cc
here_doc: print error if failed to create temp file 2025-03-28 19:10:43 +01:00
66ea1328aa
simple_cmd executing: print error message on command not found 2025-03-28 16:58:13 +01:00
a4c482e8f9
here_doc: perform variable expansion 2025-03-28 16:45:28 +01:00
Jérôme Guélen
5ff990ef50 Parsing-refactor: Determining structures. 2025-03-28 15:03:15 +01:00
befe219436
simple_cmd executing: works with arguments and env
Does not handle redirections at all, no special facilities are in place to allow
nofork or builtin execution.
2025-03-28 14:50:37 +01:00
dd293c0183
leaks: ignore readline add_history leaks 2025-03-28 14:50:37 +01:00
bd01c79838
fix: get_cmdpath returns NULL if executable is absent in first directory searched 2025-03-28 14:50:37 +01:00
a5a93be3d0
fix: get_cmdpath will sometimes leak memory 2025-03-28 14:50:37 +01:00
a7b78272ab
minishell: initialize app struct with env 2025-03-28 14:50:37 +01:00
db1834a620
inttests: run each test in a clean directory 2025-03-28 14:50:37 +01:00
d8bc528c5a
minishell: only parse simple commands 2025-03-28 14:50:37 +01:00
f07a80c762
postprocessing: do quote removal 2025-03-28 14:50:37 +01:00
22b0b4746f
fix: worddesc_copy did not copy the marker 2025-03-28 14:50:37 +01:00
8b67ccc813
inttests: add a way to mark a test as TODO 2025-03-28 14:50:37 +01:00
0fbfee7d78
cmdgroup parsing: parse cmdlist surrounded by parentheses 2025-03-28 14:50:37 +01:00
185a069044
cmdgroup parsing refactor: use builder pattern
This will make it easier to add the next logic stuff to it
2025-03-28 14:50:37 +01:00
73a64b2ec3
fix potential memory leak in cmdlist_from_wordlist
This leak would happen if the alocation of the builder fails. In that case, we
don't free our copy of the wordlist
2025-03-28 14:50:37 +01:00
bed8c9d507
inttests: exit with number of failed test as exit code 2025-03-28 14:50:37 +01:00
dff5de66da
fix redirection parsing sometimes skipping over redirections
This happens when multiple redirections are following each others, such as in

	 echo hi > out < in

which would be parsed as

 ╰─ t_cmdgroup
     ├─ t_cmdlist
     │   ├─ num_cmds = 1
     │   ╰─ cmd[0]
     │       ├─ t_cmdlist_item
     │       │   ╰─ t_pipeline
     │       │       ├─ num_cmd = 1
     │       │       ╰─ cmd[0]
     │       │           ╰─ t_simple_cmd
     │       │               ├─ words = [echo][hi][<][in]
     │       │               ╰─ t_redir_list
     │       │                   ╰─ redirection[0]
     │       │                       ╰─ t_redirection
     │       │                           ├─ t_redir_type = REDIR_OUTPUT
     │       │                           ╰─ marker = [out]
     │       ╰─ t_operator = END
     ╰─ (no redirections)

when the correct parsing is
 ╰─ t_cmdgroup
     ├─ t_cmdlist
     │   ├─ num_cmds = 1
     │   ╰─ cmd[0]
     │       ├─ t_cmdlist_item
     │       │   ╰─ t_pipeline
     │       │       ├─ num_cmd = 1
     │       │       ╰─ cmd[0]
     │       │           ╰─ t_simple_cmd
     │       │               ├─ words = [echo][hi]
     │       │               ╰─ t_redir_list
     │       │                   ├─ redirection[0]
     │       │                   │   ╰─ t_redirection
     │       │                   │       ├─ t_redir_type = REDIR_OUTPUT
     │       │                   │       ╰─ marker = [out]
     │       │                   ╰─ redirection[1]
     │       │                       ╰─ t_redirection
     │       │                           ├─ t_redir_type = REDIR_INPUT
     │       │                           ╰─ marker = [in]
     │       ╰─ t_operator = END
     ╰─ (no redirections)
2025-03-28 14:50:37 +01:00
9fb4e13420
implement tree debug for redirections 2025-03-28 14:50:37 +01:00
88ed66e138
refactor cmdlist.num_cmds -> cmdlist.num_cmd to be more consistent 2025-03-28 14:50:37 +01:00
448458b37f
redirection parsing: parse redirections for cmdgroup 2025-03-28 14:50:37 +01:00
c258fa6077
fix(simple_cmd): did not free redirection list correctly 2025-03-28 14:50:37 +01:00
0584757446
refactor: put parsing of command and debugging out of main loop 2025-03-28 14:50:37 +01:00
36c1b72eff
tests: implement a better integration test framework than shellspec
(for our usecase at least)
2025-03-28 14:50:37 +01:00
1f03cbbedb
tree debug: implement rest of tree debug
This is a big commit, sorry!
2025-03-28 14:50:37 +01:00
76b2b8ec7f
treedrawing: fix bug where rare circumstances could lead to incorrect indent
The problem took me a while to find, but the issue here was that push_buf does
not add a terminating null byte, and I was only asking push_buf to copy the
bytes that contained characters, not the terminating null byte as well.

This, combined with the fact that to remove bytes from the end of the buffer, I
just placed a null byte a the end, caused some combinations of indentation and
dedentations to overwrite the null byte, and not place a null byte after the new
indent, which would still contain data from a previous indentation.
2025-03-28 14:50:37 +01:00
1486c3b124
treeprint refactor: use simpler types 2025-03-28 14:50:37 +01:00
3edff91107
cmdgroup parsing: parse a single cmdlist 2025-03-28 14:50:37 +01:00
5379ad34f2
cmdlist debug: implement debug func (STUB)
I need to actually parse some cmdlists for this to have any meaning further
2025-03-28 14:50:37 +01:00
40c5164eef
tree debug: create library for easy indent/dedent 2025-03-28 14:50:37 +01:00
47a61699ab
cmdlist debug: tree debug printing (STUB) 2025-03-28 14:50:37 +01:00
131ba36d93
cmdlist refactor: use cmdlist instead of command_list as a shorthand
I hope this doesn't break too much code ^^
2025-03-28 14:50:37 +01:00