[net.unix-wizards] C compiler bug

test@cit-vax@sri-unix.UUCP (07/15/83)

With the 4.1bsd C compiler, the following program

    extern blech(Z);            /* <<<<< illegal (?) */

    main(argc,argv)
    int argc;
    char *argv[];
    {
	printf("[%d]\n",argc);
    }

prints out argc as [2147479384] instead of [1]. (argv  is  also  screwed
up). It seems to me that it shouldn't even get past the compiler because
of the 'Z' in the extern. Removing the 'Z' causes [1] to be  printed  as
expected. Is there a defined meaning of "extern  blech(Z)"?  This  would
seem to be a bug.
				    * Eric Holstege
				    * Caltech, Pasadena, CA.
				    * (eric@cit-vax)
				    * (...!ucbvax!cithep!citcsv!eric)
				    * (...!research!citcsv!eric)

chris@umcp-cs.UUCP (07/18/83)

The code generated for

	extern foo(bar);
	main (argc) { printf ("%d\n", argc); }

is definitely wrong.  I took a quick look into the pcc code but didn't
find anything useful.  However, for anyone who wants to attempt to fix
pcc, what's wrong is almost certainly that the offsets for function
arguments are calculated during parsing of the argument list.  This
gets reset somewhere, probably after a left brace.  Since the extern
has a single parameter, but no left brace, the offset count is left too
large.  (I checked also "extern foo(bar,baz);" and that makes the
offset count two units too large, instead of one.)  The parser really
ought to reject the arguments in an "extern" declaration, and also
should probably give an error message if you write something like

	foo(bar); main () { ...

(for which it generates the same incorrect code as for the first
example).  Possibly one could check the argument offset after seeing a
function declaration with a right paren followed by a semicolon.  A
better (?) way to fix it would be to change the parser to reject that
syntax in the first place, but I suspect adding one semantic action
would be easier.  (Have you ever *looked* at the pcc parser?  Shudder.)

(Gee, this is an awfully long note for one without a bug fix.)

				- Chris
-- 
In-Real-Life:	Chris Torek, Univ of MD Comp Sci
UUCP:		{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:		chris@umcp-cs
ARPA:		chris.umcp-cs@UDel-Relay