compiler craziness

utzoo!decvax!harpo!eagle!mhuxt!mhuxj!cbosgd!djb utzoo!decvax!harpo!eagle!mhuxt!mhuxj!cbosgd!djb
Fri Nov 12 14:41:49 AEST 1982

Here's a fun puzzle, and a great way to spend a couple of hours trying
to figure just what's going on in the compiler.  (This happened to me on
our 4.1BSD system using the stock compiler, so if your system is different,
you may not be able to play.)


I typed in a new subroutine and compiled it.  I got a "syntax error"
message, then "Fatal error in /lib/ccom"!  A quick look at the file
revealed I'd inadvertently used a : instead of a ; to end a line.
Fascinated by the fact that the compiler tossed its cookies instead of
catching the problem, I started playing with the routine to see if I
could isolate the source of the difficulty.

After some trimming, I reduced the offending code block to:

			{ n=5; }

Never mind the missing declarations.  Just type this fellow in and
try and compile.  For fun, see if you can figure out what's going on.
The following modifications eliminated the fatal error condition:
	1) Removing the braces around the n=5; statement
	2) Changing the for-loop to something else
	3) Throwing away either the { n=5; } line or the for-loop
	4) Messing with the statements inside the for-loop declaration.

I'm convinced the problem is caused by the compiler's insistance on
interpreting the : as if it were part of the ternary operator "?:", but
as to what's happening after that, I'm buffaloed.  If anybody can figure 
out what's going on, please let me know, plus help answer the following 
	Is this a hole in the compiler's checking?  
	Is it some unpreventable side-effect of the ?: operator?
	What bizarre chain of events leads to the compiler dumping core?  
	Which flavors of the compiler barf on this and which figure it out?

						David Bryant

More information about the Comp.lang.c mailing list