SIGFPE C quiz answer

Dave Brower daveb at rtech.UUCP
Mon Apr 21 06:23:51 AEST 1986


Last week, I posted a C quiz, and promised to post the answer.  The
question is, why does the code get a SIGFPE.   Here's the crucial
piece:

> void dynomite( flag, i )
> int flag, i;
> {
> 	float f;
>
> 	if( flag ) i = zerf( &f );
> 	else show( flag, i, f );	/* <-- SIGFPE happens here */
> }

The good news is that almost everyone who mailed me an answer got it
correct.  Congratulations to all of the language lawyers out there!

What is going on is more evil with C's float/double parameter
convention.  The float variable "f" must be converted to a double when
being pushed as an argument to show(), even though show might not use
it.  Since "f" is not initialized, it may contain an illegal floating
point number.  When the conversion is attempted, the system may very
well say, "acckkk pffft!, I don't grok that value!!!," and send a
signal.

I have seen this provoked on VAXen and on machines with National's 32xxx
series floating fpoint co-processor.  Doug Gwyn suggests it can happen
easily on PDP-11s as well.  You should consider it a bug everywhere.

The moral is,

     **************************************************************
     "Initialize float variables before passing them as arguments."
     **************************************************************

-dB
-- 
Red:   "Whadda ya got that big nose for, granny?"
Wolf:  "Just to HAVE, see!"
{amdahl, sun, mtxinu, cbosgd}!rtech!daveb



More information about the Comp.unix mailing list