Re: signal(SIGFPE,SIG_IGN) causes endless loop

Tim Wright (timw@sequent.com)
Tue, 23 Apr 1996 15:55:03 PDT


In message <Pine.LNX.3.91.960420120232.21177D-100000@linux.cs.Helsinki.FI>,Linu
s Torvalds writes:
>
>
> On Sat, 20 Apr 1996, Bryn Paul Arnold Jones wrote:
> >
> > On 19 Apr 1996, Thomas Koenig wrote:
> >
> > > The following program loops endlessly, with repeated SIGFPE's, as
> > > repeated by strace.
> > >
> > > This is an old, old bug, but maybe fixable before 2.0?
> > >
> > > #include <stdio.h>
> > > #include <signal.h>
> > >
> > > int main()
> > > {
> > > int a,b,c;
> > >
> > > signal(SIGFPE,SIG_IGN);
> > > a = 1;
> > > b = 0;
> > > c = a/b;
> > > return 0;
> > > }
> > >
> > Hmm, gdb sais it's restarting on line 11 (c=a/b), after the signal.
> > Perhaps it should ignore the signal, and carry on on the next line ....
> > (you know what I mean).
>
> Can't be done, at least not without doing something overly complex (like
> disassembling the faulting instruction and jumping over it "by hand" in
> the kernel).
>
> The thing that sends SIGFPE might check if SIGFPE is ignored/blocked, and
> just kill the process forcibly if so (same for SIGSEGV etc, I guess).
> That process _deserves_ to die ("My name is Linus Torvalds, prepare to
> die").
>

Indeed.
This is *not* a bug. You are (non-portably) relying on the behaviour of the
processor. If it restarts the instruction, you will continually trap, just
the same as if you 'signal(SIGSEGV, SIG_IGN)' and then make an illegal memory
reference.

It is possible for the kernel to detect this behaviour as you will have
noticed Solaris does, but it still isn't "ignoring" the signal. In this
case, it kills the process, which is just what would happen if you didn't
set the signal handler in the first place !

Ignoring a divide by zero error doesn't seem to be a particularly good idea,
anyway. Seems most people check their args before integer divide :-)

t

--
Tim Wright, Worldwide Technical Services,         |  Email: timw@sequent.com
Sequent Computer Systems Inc., 15450,             |
SW Koll Parkway, Beaverton, Oregon 97006          |  Phone: +1-503-578-3822
"Nobody ever said I was charming, they said "Rimmer, you're a git!"" RD VI