SIGINT, longjmp

Hal Murray hmurray at
Wed Jan 27 02:05:52 UTC 2016

esr at said:
> I don't know of any functions that are specifically unsafe around setjmp()/
> longjmp().

The interesting case is longjmp-ing from a signal handler.

> The right way to think about setjmp()/longjmp() is as a save/restore of the
> processor's register state, including  the stack and frame pointers.  It
> doesn't have the concurrency issues that threads do because it doesn't alter
> static memory or the heap.

It doesn't alter the heap, but it doesn't restore it either.

Anything that calls malloc is an opportunity for a storage leak.  Jmp-ing 
from a signal handler yanks you out of the middle of a routine without any 
opportunity for cleanup.

I suspect getaddrinfo masks or intercepts SIGINT, but I haven't found 
anything like that in the man pages.  The symptom is that ^C doesn't work 
right away but does work after several seconds, a reasonable time for a DNS 
lookup if you need to retransmit or have a bloated link.

A signal handler is roughly equivalent to a short-lived thread.  The Linux 
man page has a list of routines that are guaranteed to work crrectly from a 
handler.  It doesn't say anything about longjmp.  The man page for longjmp 
doesn't say anything about signals other than siglongjmp restores the mask.

These are my opinions.  I hate spam.

More information about the devel mailing list