Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing "*()" as an argument to a program in bash

Tags:

c

linux

bash

EDIT 2: even doing "ls *()" causes bash to not return and use 100% of one cpu. Does anyone know why bash is doing this?

I was learning C and learning about arguments and was wondering what characters do weird things when passed as arguments. I passed "*()" without quotes to a c program through bash like:

$ ./program *()

Bash could not be quit with ctrl c or ctrl z. When I looked at htop it was using 100% of one CPU and I had to SIGKILL it. Does anyone know what is going on here. I am just curious.

EDIT: even the simple program

#include <stdio.h>
int main(int argc, char *argv[]){ return 0; }

causes this behavior.

like image 897
Will Avatar asked Jan 02 '15 22:01

Will


1 Answers

This appears to be a known bug in bash, fixed in version 4.3.16. It occurs only if the extglob feature is enabled, for example if you have a command like

shopt -s extglob

in your .bashrc or some other init file.

I can reproduce it consistently with bash 4.3.11 on Linux Mint 17:

$ bash --norc
bash-4.3$ mkdir empty
bash-4.3$ cd empty
bash-4.3$ echo *()
bash: syntax error near unexpected token `('
bash-4.3$ shopt -s extglob
bash-4.3$ echo *()

The shell hangs after the last command. Note that I ran it in an empty directory; the problem also occurs in a non-empty directory.

The bash manual documents a form of wildcard that's enabled only when extglob is enabled:

`*(PATTERN-LIST)'
     Matches zero or more occurrences of the given patterns.

Given *(), that's zero or more occurrences of the empty string. Since there are infinitely many occurrences of the empty string in any string, I can see how that might cause an infinite loop if there's no special-case code to avoid it.

And it appears to have been fixed in a later version. I see the problem in 4.3.11 but not in 4.3.30. rici's comment suggests that patch 016 is likely to have fixed it. And the bug report corresponding to the patch includes this:

1) bash gets stuck

shopt -s extglob
echo !(*/) # never returns, cannot be interrupted

which I'd say confirms that that's the bug.

like image 108
Keith Thompson Avatar answered Sep 19 '22 07:09

Keith Thompson