[comp.bugs.sys5] SVR2 cpp bug

gwyn@brl-smoke.ARPA (Doug Gwyn ) (11/25/86)

Here is a fix for Problem ID 722 in the UNIX System V Known Problem List.

The problem can be seen by running the following file through /lib/cpp -C:

/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
... [precisely enough text so that the following
  asterisk is exactly the BUFSIZth character] ...
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
data1;
/* comment */
data2;

Buggy cpp produces:

# 1 ""
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
...
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx**//*/
data1;
/* comment */
data2;

which accidentally comments out "data1".  After
applying my fix, cpp produces:

# 1 ""
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
...
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*//**/
data1;
/* comment */
data2;

which is much better.

FIX:  Somewhere around line 600 of cpp.c:

/*  @(#)cpp.c	1.11	*/
char	release[] = "@(#)C rel 6.0";
...
char *
cotoken( p )
    register char *p;
{
...
	case '/':
	    for ( ;; )
	    {
		if ( *p++ == '*' )  /* comment */
...
		    for ( ;; )
...
			if ( p[-1] == '*' )
			    for ( ;; )
			    {
...
				    /* split long comment */
				    else if ( ( p - inp ) >= BUFSIZ )
				    {
					/* DAG -- removed for bug fix: */
/*					*p++ = '*';	*/
					*p++ = '/';
					inp = p;
					p = refill( p );
					/* DAG -- changed 2 to 3 for bug fix: */
					outp = inp = p -= 3;
					*p++ = '/';
					*p++ = '*';
					/* DAG -- added for bug fix: */
					*p++ = '*';
				    }

WARNING:  There is a similar kludge about 50 lines later in the source
file; do NOT make this change to it.