Compiler bug

Steve Summit scs at adam.pika.mit.edu
Sun Dec 18 16:13:49 AEST 1988


In article <4279 at freja.diku.dk> njk at freja.diku.dk (Niels Jurgen Kruse) writes:
>Consider this little one liner:
>-------- foo.c --------
>int foo = 1 && 0;
>------ end foo.c ------
>On a Bsd 4.3 VAX 785, cc complains :
>"foo.c", line 1: illegal initialization
[several other failures described]
>So 3 out of 4 get it wrong. This bug seems to bee contagious.

In article <733 at auspex.UUCP> guy at auspex.UUCP (Guy Harris) writes:
>3 out of 4 are based on PCC...
>The compiler problem may be due to PCC's incredible sensitivity to tiny
>perturbations in the order in which various operations are done on the
>expression trees...

Or, more likely, it has to do with the unfortunate description of
constant expressions in the original K&R (first edition, section
15, page 211):

	...the expression can involve... the binary operators

		+  -  *  /  %  &  |  ^  <<  >>  ==  !=  <  >  <=  >=

	or... the unary operators

		-  ~

	or... the ternary operator

		? :

Ritchie's original pdp11 C compiler also reports "illegal
initialization" for an initialization involving &&.

Harbison and Steele note (second edition, p. 188) that "the
original description of C did not mention that the operator ! is
allowed in constant expressions, but this obviously was an
oversight or a typographical error."  Their list of operators
allowed in constant expressions includes && and ||, without
comment.

K&R, second edition (section A7.19, p. 209) contains no explicit
list of operators, implying that all (other than obvious
exceptions such as unary &) are now allowed.

Before rejecting the historical restriction on && and ||
out-of-hand, recall that && and || are somewhat different from
the other binary operators: they affect control flow, in that
evaluation of the right-hand-side is short-circuited if its value
will not affect the result.  This fact does not justify their
being disallowed in constant expressions, but it makes it easier
to understand why an implementation might (perhaps inadvertently)
do so.  Since "control flow" is meaningless in constant
expressions, the handling of && and || in constant expressions
could be different (read "nonexistent"), as opposed to normal
expressions.  (Of course, the interpretive handling of constant
expressions is already pretty different from the code generation
performed for normal expressions.)

Another illustration of boolean foibles in constant expressions
is something like

	#define a 0
	#if a == 0 || 4 / a < 2

which causes some (many?) cpp's to abort with a divide by 0
fault.

                                            Steve Summit
                                            scs at adam.pika.mit.edu



More information about the Comp.lang.c mailing list