bash seems confused about it's state after unclosed single quotes in nested command substitution

2022-04-15 Thread Martin Schulte
Hello bash-bughunters,

please consider the following interactive lines:

$ echo $BASH_VERSION 
5.1.4(1)-release
$ uname -a
Linux t1 5.10.0-13-amd64 #1 SMP Debian 5.10.106-1 (2022-03-17) x86_64 GNU/Linux
$ echo $BASH_VERSION
5.1.4(1)-release
$ sleep $(expr 60 - $(date +%s')) ; date
bash: command substitution: line 417: unexpected EOF while looking for matching 
`''
bash: command substitution: line 418: syntax error: unexpected end of file
bash: unexpected EOF while looking for matching `)'
$ 

At this point it looks as if bash has encountered the problem and thus not 
executed the command line - fine.

Things get strange when you enter a command, e.g. echo:

$ echo
>

To get out of this you can enter the missing single quote followed by two 
closing braces:

> '))
expr: non-integer argument
sleep: missing operand
Try 'sleep --help' for more information
$

Now you can go on as expected, the last command in history is $'echo\n\'))' and 
the 'sleep ...' is not in the history at all.

I would either have expected to get PS2 and no error messages after entering 
the line starting with sleep or being able to just enter the next command.

Best regards

Martin



Re: bash seems confused about it's state after unclosed single quotes in nested command substitution

2022-04-15 Thread Andreas Schwab
On Apr 15 2022, Martin Schulte wrote:

> I would either have expected to get PS2 and no error messages after
> entering the line starting with sleep

That's what I get when trying this in 5.2-beta.

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."



executes statement after "exit"

2022-04-15 Thread Frank Heckenbach
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -g -O2 -fstack-protector-strong -Wformat 
-Werror=format-security -Wall 
uname output: Linux mars 5.10.0-12-amd64 #1 SMP Debian 5.10.103-1 (2022-03-07) 
x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 5.1
Patch Level: 16
Release Status: release

Description:

bash executes statement after "exit".

Happens with or without "set -e".

Doesn't happen with newline instead of ";".

Similar to https://bugs.debian.org/819327, but perhaps not the same
(as that one also happens with newline).

#!/bin/bash
: $((08 + 0)); exit
echo "Should not get here."

But I guess that's also prescribed by POSIX, right?



Re: executes statement after "exit"

2022-04-15 Thread Chet Ramey

On 4/15/22 12:57 PM, Frank Heckenbach wrote:


Bash Version: 5.1
Patch Level: 16
Release Status: release

Description:

bash executes statement after "exit".

Happens with or without "set -e".

Doesn't happen with newline instead of ";".

Similar to https://bugs.debian.org/819327, but perhaps not the same
(as that one also happens with newline).

#!/bin/bash
: $((08 + 0)); exit
echo "Should not get here."


It never executes `exit'.

The explanation in 
https://lists.gnu.org/archive/html/bug-bash/2022-04/msg00010.html applies here.


The arithmetic syntax error (invalid octal constant) results in a word
expansion error (the $((...)) ), which causes the shell to abort execution
of the current command (the command list) and jump back to the top level
to continue execution with the next command (echo).


But I guess that's also prescribed by POSIX, right?


POSIX requires the shell to exit on a word expansion error, which bash does
in posix mode.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/



Re: executes statement after "exit"

2022-04-15 Thread Frank Heckenbach
> > #!/bin/bash
> > : $((08 + 0)); exit
> > echo "Should not get here."
> 
> It never executes `exit'.
> 
> The explanation in 
> https://lists.gnu.org/archive/html/bug-bash/2022-04/msg00010.html applies 
> here.
> 
> The arithmetic syntax error (invalid octal constant) results in a word
> expansion error (the $((...)) ), which causes the shell to abort execution
> of the current command (the command list)

Current command means command list? Is this actually documented
somewhere?

E.g., in "3.5.6 Process Substitution" the manual says:
"The process list is run asynchronously, and its input or output
appears as a filename. This filename is passed as an argument to the
current command as the result of the expansion."

So if "current command" is the command list, in

  sleep 3; : <(echo foo >&2)

shouldn't it start the "echo" before the "sleep" (in order to pass
its stdout as a filename to the command list)? It doesn't seem to do
so. So here, "current command" apparently means a simple command
(or in other cases, a compound command), but not a command list.

> and jump back to the top level
> to continue execution with the next command (echo).

Let't see. This is a command list, so according to your explanation,
due to the expansion error, neither the "exit" nor the "echo" is
run:

: $((08 + 0)); exit; echo "Should not get here."

Alright. Now, in "3.2.4 Lists of Commands" it says:

"A sequence of one or more newlines may appear in a list instead of a semicolon 
to delimit commands."

So let's do this for the latter semicolon, resulting in:

: $((08 + 0)); exit
echo "Should not get here."

According to the above, this should still be one command list, so
the "echo" shouldn't run either, but it does.

> POSIX requires the shell to exit on a word expansion error, which bash does
> in posix mode.

This actually seems the saner behaviour rather than continuing at some
arbitrary point -- which I guess is well-defined in some sense, but
really unobvious to the programmer and very fragile since it changes
when a block is moved to a function, put inside an "if"-statement or
just has the formatting (";" vs. newline) changed ...



Crash with 5.2 beta in compgen

2022-04-15 Thread Sam James
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: x86_64-pc-linux-gnu-gcc
Compilation CFLAGS: -O2 -pipe -march=native -ggdb3
uname output: Linux 2021-10-24 5.15.34-gentoo-dist-hardened #1 SMP Sat Apr 16 
02:20:51 BST 2022 x86_64 AMD Ryzen 9 3950X 16-Core Processor AuthenticAMD 
GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 5.2
Patch Level: 0
Release Status: beta

Description:
Bash crashes when running compgen -c -X a.

Repeat-By:
   # bash -c "compgen -c -X a"
   Segmentation fault (core dumped)



Originally noticed when running 'eselect compiler-shadow update' in Gentoo 
Linux, but Ionen Wolkens (ionen@g.o) came up with a far smaller reproducer:

# bash -c "compgen -c -X a"
Segmentation fault (core dumped)

Backtrace:
```
Starting program: /bin/bash -c compgen\ -c\ -X\ a
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
__strchr_avx2 () at ../sysdeps/x86_64/multiarch/strchr-avx2.S:65
65  vmovdqu (%rdi), %ymm8
(gdb) bt
#0  __strchr_avx2 () at ../sysdeps/x86_64/multiarch/strchr-avx2.S:65
#1  0x555c618f in quote_word_break_chars (text=0x5565f300 
"/usr/local/sbin/.keep") at bashline.c:4143
#2  bash_quote_filename (s=s@entry=0x5565f2c0 "/usr/local/sbin/.keep", 
rtype=rtype@entry=1, qcp=qcp@entry=0x7fffdbbf "") at bashline.c:4346
#3  0x555c73f5 in executable_completion (searching_path=1, 
filename=0x5565f2c0 "/usr/local/sbin/.keep") at bashline.c:1951
#4  command_word_completion_function (hint_text=0x55613bff "", state=83) at 
bashline.c:2378
#5  0x77f77257 in rl_completion_matches (text=text@entry=0x55613bff 
"", entry_function=0x555c6d70 )
at 
/usr/src/debug/sys-libs/readline-8.2_beta/readline-8.2-beta/complete.c:2219
#6  0x555d00ef in gen_action_completions 
(text=text@entry=0x55613bff "", cs=, cs=) at 
pcomplete.c:856
#7  0x555d02f1 in gen_compspec_completions (cs=cs@entry=0x55655860, 
cmd=cmd@entry=0x556130b0 "compgen", word=word@entry=0x55613bff "", 
start=start@entry=0,
end=end@entry=0, foundp=foundp@entry=0x0) at pcomplete.c:1333
#8  0x555ebab1 in compgen_builtin (list=) at 
./complete.def:721
#9  0x55579f7b in execute_builtin (builtin=builtin@entry=0x555eb7f0 
, words=words@entry=0x55655510, flags=flags@entry=64, 
subshell=subshell@entry=0)
at execute_cmd.c:4958
#10 0x5557fec1 in execute_builtin_or_function (flags=64, 
fds_to_close=0x556551a0, redirects=0x0, var=0x0, builtin=0x555eb7f0 
, words=0x55655510)
at execute_cmd.c:5472
#11 execute_simple_command (fds_to_close=0x556551a0, async=, 
pipe_out=, pipe_in=, 
simple_command=0x55655080) at execute_cmd.c:4724
#12 execute_command_internal (command=0x55655050, 
asynchronous=asynchronous@entry=0, pipe_in=pipe_in@entry=-1, 
pipe_out=pipe_out@entry=-1, fds_to_close=fds_to_close@entry=0x556551a0)
at execute_cmd.c:866
#13 0x555d8c99 in parse_and_execute (string=, 
from_file=from_file@entry=0x555fd09f "-c", flags=flags@entry=20) at 
evalstring.c:519
#14 0x55565650 in run_one_command (command=0x7fffe4fe "compgen -c 
-X a") at shell.c:1473
#15 0x55563f0d in main (argc=3, argv=0x7fffe268, 
env=0x7fffe288) at shell.c:763
```

Let me know if I need to grab more information.


signature.asc
Description: Message signed with OpenPGP