Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: unbounded recursive call in a shell script crashes zsh

On Apr 16, 11:12pm, Daniel Shahaf wrote:
} I realized that the code could compare how far it is from main() to
} RLIMIT_STACK and bail out gracefully if it has, say, less than X KB or
} Y% of stack remaining.

Well ...

Another approach would be to add a C function that does nothing more
than declare a large array to force a stack allocation, and assign
something to the last byte; then in doshfunc(), trap the appropriate
signals (SIGSEGV and SIGSTKFLT ?) while calling that function.  If a
signal is raised, bail out as if MAX_FUNCTION_DEPTH has been reached.

I suspect this would destroy some efficiency gains Sebastian has been
so diligently wringing out of the code the last few months, but it'd
probably be fairly bulletpoof.  doshfunc() could remember the depth it
last safely reached and skip calling the stack-probe until that was
next exceeded, so you'd only pay the penalty once.

But is all this really worth it?

I note in passing that MAX_FUNCTION_DEPTH can be switched off at compile
time, leaving the shell entirely at the mercy of ths script writer in
this regard.

Messages sorted by: Reverse Date, Date, Thread, Author