[net.bugs.v7] bug in /usr/src/cmd/cpp/cpy.y

swatt (08/19/82)

Please escuse me if this is the second time you've seen this, but our
decvax link was causing problems and I could never find out if this
got through.

	- Alan

>>From: ittvax!swatt (Alan S. Watt)
>>Newsgroups: net.bugs.4bsd,net.bugs.v7
>>Title: Bug in /usr/src/cmd/cpp/cpy.y 
>>Article-I.D.: ittvax.416
>>Posted: Sun Aug 15 14:30:07 1982
>>Received: Sun Aug 15 14:30:07 1982


Sun Aug 15 14:12:09 EDT 1982:   Bug in /usr/src/cmd/cpp/cpy.y

I just got bitten by a bug in the C pre-processor.  It took me
quite a while to realize I was dealing with a cpp bug, and not
one of my own.  The symptom is that negative numbers in "#if"
statements are treated as 0.  Try the following:

	/lib/cpp <<!EOF
	#if -1 == 0
		Oh boy, are you in trouble
	#else
		Yours works OK.
	#endif
	!EOF

Our cpp does not carry a version number, but the README file
in /usr/src/cmd/cpp is dated Aug 25, 1978.  I think this was
included on all the V7 distributions;  our system is 4.1bsd.

It's hard to believe someone hasn't noticed this before, but
I usually pay close attention to C compiler bugs reports, and
I don't recall seeing anything.

	- Alan S. Watt
	[decvax!]ittvax!swatt

The fix is quite simple and involves 1 line in the yacc source:

======================================================================
	echo -n "/usr/src/cmd/cpp/cpy.y: "
	sum /usr/src/cmd/cpp/cpy.y >/tmp/upd.$$.sum
	if cmp - /tmp/upd.$$.sum <<!EOF.9122
	53580     2
	!EOF.9122
	then
		echo "OK -- applying edit commands"
		cp /usr/src/cmd/cpp/cpy.y /tmp/upd.$$.tmp ; chmod +w /tmp/upd.$$.tmp
		ed - /tmp/upd.$$.tmp <<\!EOF.9122
	66c
			={$$ = -$2;}
	.
	0a
	/* Aug 15 14:05:14 1982: (ittvax!swatt):
	 * bug in term: '-' term
	 * production fixed.
	 */
	.
	w
	q
	!EOF.9122
		rm -f cpy.y
		cp /tmp/upd.$$.tmp cpy.y ; chmod a-w cpy.y
	else
		echo "Old source file not same version;" \
			"use diff listings by hand" <<!EOF.9122
	*** /usr/src/cmd/cpp/cpy.y	Sat May  3 20:54:20 1980
	--- cpy.y	Sun Aug 15 14:06:11 1982
	***************
	*** 1,3
	  %term number stop DEFINED
	  %term EQ NE LE GE LS RS
	  %term ANDAND OROR

	--- 1,7 -----
	+ /* Aug 15 14:05:14 1982: (ittvax!swatt):
	+  * bug in term: '-' term
	+  * production fixed.
	+  */
	  %term number stop DEFINED
	  %term EQ NE LE GE LS RS
	  %term ANDAND OROR
	***************
	*** 63,69
			={$$ = $1;}
	  term:
		  '-' term %prec UMINUS
	! 		={$$ = -$1;}
		| '!' term
			={$$ = !$2;}
		| '~' term

	--- 67,73 -----
			={$$ = $1;}
	  term:
		  '-' term %prec UMINUS
	! 		={$$ = -$2;}
		| '!' term
			={$$ = !$2;}
		| '~' term
	!EOF.9122
	fi
	rm -f /tmp/upd.$$.tmp /tmp/upd.$$.sum
======================================================================