longjmp out of signal handler (was Re: alloca() portability)

Mark Lanzo lanzo at wgate.UUCP
Sat Nov 17 08:59:27 AEST 1990


In a prior article rtm at island.uu.net (Richard Minner) wrote:
    
    I was compelled recently (by the devil no doubt) to do this thing.
    In short:
    	catch_sigsegv();
    	if (setjmp(sigsegv_jmp_buf) == 0)
    		<do naughty stuff that might cause SIGSEGV>
    	else
    		<make note of the SIGSEGV>
    	release_sigsegv();
    	...
    	void handler(sig) int sig;
    	{
    		<release the signal, check sig value...>
    		longjmp(sigsegv_jmp_buf, 1);
    	}
    
    Is this really all that unportable and/or unreliable?  I asked about
    this a while back when (I believe) Peter da Silva said it was evil,
    but no one answered.  If it's some kind of secret just say so and
    I'll understand.


I too ran into a situation where I had to do exactly what you have here.
Unfortunately, it seems to be the only way to handle some problems
(I despise using setjmp/longjmp for anything if it can be avoided).
In particular, it was mandatory that I longjmp out of the signal handler
since if I returned from the signal handler I got caught in an
infinite loop!  It seems that the system returns to exactly the same
instruction that caused the exception in the first place, thus causing 
the same error all over again.  {is this what it is supposed to do?}.

I did however use "_setjmp" and "_longjmp" rather than "setjmp" and
"longjmp", because the latter two functions screw around with the
process signal mask.  My equivalents to your "catch_sigsegv" and
"release_sigsegv" did all the setup & restoration of the signal mask
and exception vectors (they also trapped SIGBUS in addition to SIGSEGV).

Disclaimer:  this was under HP-UX 7.0 on HP9000 s300 series.
Your mileage may vary.  I don't know if "_setjmp" and "_longjmp"
are standard things or HP'isms.

-- 
Mark Lanzo                      | Wandel & Goltermann Technologies, Inc.
uunet!wgate!lanzo               | 1030 Swabia Court, Research Triangle Park
lanzo at wgate.wgate.com ??        | North Carolina 27709-3585
                                | Phone: (919) 941-5730  FAX: (919) 941-5751



More information about the Comp.lang.c mailing list