Subject: Re: bin/19354: Semantics of /bin/sh "command" builtin violates POSIX
To: Christopher Richards <richards+netbsd@CS.Princeton.EDU>
From: David Laight <>
List: netbsd-bugs
Date: 12/19/2002 09:15:12
On Wed, Dec 18, 2002 at 07:24:11PM -0500, Christopher Richards wrote:
> On 19 Dec 2002 00:12:37 GMT, David Laight wrote:
> > +		/* stop shell blowing its stack */
> > +		if (++funcnest > 1000)
> > +			error("too many nested function calls");
> I don't like this -- it deviates from other /bin/sh implementations
> and may cause previously-working scripts to fail.

I put it in because I (mis)typed:
    $ f() { f; }
    $ f
and got a core dump from the shell.
With a default (2Mb) stack on x86 I can get just over 8000
nested calls.  The limit of 1000 isn't very silly, nothing
portable can expect that depth of function call.

Clearly you can blow the stack with other deep definitions,
but they are a little more unlikely.
    $ x="c=\$(( \$c + 1 )); echo \$c; eval \$x"
    $ eval $x
core dumps after 5030 iterations (in the bowels of vfprintf).

Basically it seems reasonable that it shouldn't be possibly to
get the shell to core dump.  Catching SIGSEGV might be tricky
especially when you are faulting on the stack...

Anyone know how to find out (reasonable portably) how much stack
space an application has left?


David Laight: