Re: Segmentation Fault in bash --posix

2023-01-20 Thread Greg Wooledge
On Fri, Jan 20, 2023 at 11:50:52PM +0100, Martin Schulte wrote:
> Hello Nicolas!
> 
> > bash-5.1$ echo () { echo test }
> > > echo test
> > > }
> > bash-5.1$ echo

> Most probably you want to insert a ; before the first closing }:
> 
> echo() { echo test ; }
> 
> But even now bash will crash.

It's not clear what the original intent was, but...

unicorn:~$ echo() { command echo test; }
unicorn:~$ echo hello world
test

Perhaps that's what the OP was trying to do.



Re: Segmentation Fault in bash --posix

2023-01-20 Thread Martin Schulte
Hello Nicolas!

> bash-5.1$ echo () { echo test }
> > echo test
> > }
> bash-5.1$ echo

You have defined a function echo that
- calls itself with the first argument 'test' and the second argument '}' (!!!)
- calls itself with the first and only argument test

Try

type -a echo

and

Echo() {
  echo test }
  Echo test
}
Echo

to get an insight was is happening.

So, it's no surprise that bash finally crashes.

Most probably you want to insert a ; before the first closing }:

echo() { echo test ; }

But even now bash will crash.

Best regards

Martin



Re: Segmentation Fault in bash --posix

2023-01-20 Thread David
On Sat, 21 Jan 2023 at 09:24, N R  wrote:

> I ran into a segmentation fault running bash --posix. Here are the
> steps to reproduce :
> bash-5.1$ echo () { echo test }
> > echo test
> > }
> bash-5.1$ echo
> Even though I'm not sure what is causing this seg fault, I'm sure it is
> not the normal/expected behaviour.

That is the normal expected behaviour.

What do you expect this should do:
$ foo() { foo ; } ; foo

This code instructs the function foo to call itself again
and again without limit forever.

So it tries to do that until the process entirely fills its call
stack with return addresses and runs out of memory
resources at which point the operating system will kill
the process.

That's not bash' fault. That's the programmers fault.



Re: Segmentation Fault in bash --posix

2023-01-20 Thread Chet Ramey

On 1/20/23 2:35 PM, N R wrote:


Bash Version: 5.2
Patch Level: 15
Release Status: release

Description:
 I ran into a segmentation fault running bash --posix. Here are the
steps to reproduce :
 bash-5.1$ echo () { echo test }
 > echo test
 > }
 bash-5.1$ echo
 Even though I'm not sure what is causing this seg fault, I'm sure it is
not the normal/expected behaviour.


You created a recursive function, executed it, and ran yourself out of
stack space. Infinite recursion will eventually result in a seg fault.

If you don't want this, look at the FUNCNEST variable.

--
``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/