[net.bugs.usg] Some "lex" cleanups and bug fixes

guy@sun.uucp (Guy Harris) (08/10/85)

Index:	usr.bin/lex/lmain.c usr.bin/lex/sub1.c 4.2BSD
	(The same bug exists in the S5 "lex" and probably exists
	in the V7 "lex" as well).

Description:
	1) A couple of bits of code here are really sloppy when it comes
	   to pointers vs. integers.

	2) Some other bits of code are sloppy when it comes to - surprise! -
	   dereferencing NULL pointers.
Repeat-By:
	Try "lex -Q anything.l" if your machine dumps core when dereferencing
	null pointers.
Fix:
	See following context diffs.  Line numbers and exact code
	will be different for S3/S5/V7.

diff -c /arch/4.2/usr/src/usr.bin/lex/lmain.c ./lmain.c
*** /arch/4.2/usr/src/usr.bin/lex/lmain.c	Thu Aug 11 20:51:24 1983
--- ./lmain.c	Tue Aug  6 11:43:35 1985
***************
*** 129,135
  dp =	dchar = myalloc(DEFCHAR,sizeof(*dchar));
  	sname = myalloc(STARTSIZE,sizeof(*sname));
  sp = 	schar = myalloc(STARTCHAR,sizeof(*schar));
! 	if(ccl == 0 || def == 0 || subs == 0 || dchar == 0 || sname == 0 || schar == 0)
  		error("Too little core to begin");
  	}
  free1core(){

--- 129,135 -----
  dp =	dchar = myalloc(DEFCHAR,sizeof(*dchar));
  	sname = myalloc(STARTSIZE,sizeof(*sname));
  sp = 	schar = myalloc(STARTCHAR,sizeof(*schar));
! 	if(ccl == 0 || pchar == 0 || def == 0 || subs == 0 || dchar == 0 || sname == 0 || schar == 0)
  		error("Too little core to begin");
  	}
  free1core(){
***************
*** 193,199
  # endif
  char *myalloc(a,b)
    int a,b; {
! 	register int i;
  	i = calloc(a, b);
  	if(i==0)
  		warning("OOPS - calloc returns a 0");

--- 193,199 -----
  # endif
  char *myalloc(a,b)
    int a,b; {
! 	register char *i;
  	i = calloc(a, b);
  # ifdef DEBUG
  	if(i==0)
***************
*** 195,200
    int a,b; {
  	register int i;
  	i = calloc(a, b);
  	if(i==0)
  		warning("OOPS - calloc returns a 0");
  	else if(i == -1){

--- 195,201 -----
    int a,b; {
  	register char *i;
  	i = calloc(a, b);
+ # ifdef DEBUG
  	if(i==0)
  		warning("OOPS - calloc returns a 0");
  # endif
***************
*** 197,205
  	i = calloc(a, b);
  	if(i==0)
  		warning("OOPS - calloc returns a 0");
- 	else if(i == -1){
- # ifdef DEBUG
- 		warning("calloc returns a -1");
  # endif
  		return(0);
  		}

--- 198,203 -----
  # ifdef DEBUG
  	if(i==0)
  		warning("OOPS - calloc returns a 0");
  # endif
  	return(i);
  	}
***************
*** 201,208
  # ifdef DEBUG
  		warning("calloc returns a -1");
  # endif
- 		return(0);
- 		}
  	return(i);
  	}
  # ifdef DEBUG

--- 199,204 -----
  	if(i==0)
  		warning("OOPS - calloc returns a 0");
  # endif
  	return(i);
  	}
  # ifdef DEBUG
***************
*** 208,214
  # ifdef DEBUG
  buserr(){
  	fflush(errorf);
! 	fflush(fout);
  	fflush(stdout);
  	fprintf(errorf,"Bus error\n");
  	if(report == 1)statistics();

--- 204,211 -----
  # ifdef DEBUG
  buserr(){
  	fflush(errorf);
! 	if(fout != NULL)
! 		fflush(fout);
  	fflush(stdout);
  	fprintf(errorf,"Bus error\n");
  	if(report == 1)statistics();
***************
*** 216,222
  	}
  segviol(){
  	fflush(errorf);
! 	fflush(fout);
  	fflush(stdout);
  	fprintf(errorf,"Segmentation violation\n");
  	if(report == 1)statistics();

--- 213,220 -----
  	}
  segviol(){
  	fflush(errorf);
! 	if(fout != NULL)
! 		fflush(fout);
  	fflush(stdout);
  	fprintf(errorf,"Segmentation violation\n");
  	if(report == 1)statistics();

diff -c /arch/4.2/usr/src/usr.bin/lex/sub1.c ./sub1.c
*** /arch/4.2/usr/src/usr.bin/lex/sub1.c	Thu Aug 11 20:51:25 1983
--- ./sub1.c	Tue Aug  6 11:35:17 1985
***************
*** 62,68
  	fprintf(errorf,s,p,d);
  	putc('\n',errorf);
  	fflush(errorf);
! 	fflush(fout);
  	fflush(stdout);
  	}
  index(a,s)

--- 62,69 -----
  	fprintf(errorf,s,p,d);
  	putc('\n',errorf);
  	fflush(errorf);
! 	if(fout != NULL)
! 		fflush(fout);
  	fflush(stdout);
  	}
  index(a,s)

guy@sun.uucp (Guy Harris) (08/10/85)

Here's the version of the previous posting about "lex" problems for the S5
"lex".  Line numbers may still differ...

1) A couple of bits of code here are really sloppy when it comes to pointers
vs. integers.

2) Some other bits of code are sloppy when it comes to - surprise! -
dereferencing NULL pointers.

3) They also use signal numbers instead of including <signal.h>.

diff -c /arch/s5r2compat/src/lex/main.c ./main.c
*** /arch/s5r2compat/src/lex/main.c	Wed Jan 30 19:56:15 1985
--- ./main.c	Tue Aug  6 11:45:52 1985
***************
*** 12,19
    char **argv; {
  	register int i;
  # ifdef DEBUG
! 	signal(10,buserr);
! 	signal(11,segviol);
  # endif
  	while (argc > 1 && argv[1][0] == '-' ){
  		i = 0;

--- 12,20 -----
    char **argv; {
  	register int i;
  # ifdef DEBUG
! #include <signal.h>
! 	signal(SIGBUS,buserr);
! 	signal(SIGSEGV,segviol);
  # endif
  	while (argc > 1 && argv[1][0] == '-' ){
  		i = 0;
***************
*** 127,133
  	dp = dchar = myalloc(DEFCHAR,sizeof(*dchar));
  	sname = (char **)myalloc(STARTSIZE,sizeof(*sname));
  sp = 	schar = myalloc(STARTCHAR,sizeof(*schar));
! 	if(ccl == 0 || def == 0 || subs == 0 || dchar == 0 || sname == 0 || schar == 0)
  		error("Too little core to begin");
  	}
  free1core(){

--- 128,134 -----
  	dp = dchar = myalloc(DEFCHAR,sizeof(*dchar));
  	sname = (char **)myalloc(STARTSIZE,sizeof(*sname));
  sp = 	schar = myalloc(STARTCHAR,sizeof(*schar));
! 	if(ccl == 0 || pchar == 0 || def == 0 || subs == 0 || dchar == 0 || sname == 0 || schar == 0)
  		error("Too little core to begin");
  	}
  free1core(){
***************
*** 191,198
  # endif
  char *myalloc(a,b)
    int a,b; {
! 	register int i;
! 	i = (int)calloc(a, b);
  	if(i==0)
  		warning("OOPS - calloc returns a 0");
  	else if(i == -1){

--- 192,200 -----
  # endif
  char *myalloc(a,b)
    int a,b; {
! 	register char *i;
! 	i = calloc(a, b);
! # ifdef DEBUG
  	if(i==0)
  		warning("OOPS - calloc returns a 0");
  # endif
***************
*** 195,203
  	i = (int)calloc(a, b);
  	if(i==0)
  		warning("OOPS - calloc returns a 0");
- 	else if(i == -1){
- # ifdef DEBUG
- 		warning("calloc returns a -1");
  # endif
  		return(0);
  		}

--- 197,202 -----
  # ifdef DEBUG
  	if(i==0)
  		warning("OOPS - calloc returns a 0");
  # endif
  	return(i);
  	}
***************
*** 199,207
  # ifdef DEBUG
  		warning("calloc returns a -1");
  # endif
! 		return(0);
! 		}
! 	return((char *)i);
  	}
  # ifdef DEBUG
  buserr(){

--- 198,204 -----
  	if(i==0)
  		warning("OOPS - calloc returns a 0");
  # endif
! 	return(i);
  	}
  # ifdef DEBUG
  buserr(){
***************
*** 206,212
  # ifdef DEBUG
  buserr(){
  	fflush(errorf);
! 	fflush(fout);
  	fflush(stdout);
  	fprintf(errorf,"Bus error\n");
  	if(report == 1)statistics();

--- 203,210 -----
  # ifdef DEBUG
  buserr(){
  	fflush(errorf);
! 	if(fout != NULL)
! 		fflush(fout);
  	fflush(stdout);
  	fprintf(errorf,"Bus error\n");
  	if(report == 1)statistics();
***************
*** 214,220
  	}
  segviol(){
  	fflush(errorf);
! 	fflush(fout);
  	fflush(stdout);
  	fprintf(errorf,"Segmentation violation\n");
  	if(report == 1)statistics();

--- 212,219 -----
  	}
  segviol(){
  	fflush(errorf);
! 	if(fout != NULL)
! 		fflush(fout);
  	fflush(stdout);
  	fprintf(errorf,"Segmentation violation\n");
  	if(report == 1)statistics();
diff -c /arch/s5r2compat/src/lex/parser.y ./parser.y
*** /arch/s5r2compat/src/lex/parser.y	Wed Jan 30 19:56:16 1985
--- ./parser.y	Tue Aug  6 11:12:37 1985
***************
*** 220,226
  yylex(){
  	register char *p;
  	register int c, i;
! 	char  *t, *xp;
  	int n, j, k, x;
  	static int sectbegin;
  	static char token[TOKENSIZE];

--- 220,226 -----
  yylex(){
  	register char *p;
  	register int c, i;
! 	register char *t, *xp;
  	int n, j, k, x;
  	static int sectbegin;
  	static char token[TOKENSIZE];
***************
*** 245,252
  						sectbegin = TRUE;
  						i = treesize*(sizeof(*name)+sizeof(*left)+
  							sizeof(*right)+sizeof(*nullstr)+sizeof(*parent))+ALITTLEEXTRA;
! 						c = (int)myalloc(i,1);
! 						if(c == 0)
  							error("Too little core for parse tree");
  						p = (char *)c;
  						cfree((char *)p,i,1);

--- 245,252 -----
  						sectbegin = TRUE;
  						i = treesize*(sizeof(*name)+sizeof(*left)+
  							sizeof(*right)+sizeof(*nullstr)+sizeof(*parent))+ALITTLEEXTRA;
! 						xp = (char *)myalloc(i,1);
! 						if(xp == 0)
  							error("Too little core for parse tree");
  						cfree((char *)xp,i,1);
  						name = (int *)myalloc(treesize,sizeof(*name));
***************
*** 248,255
  						c = (int)myalloc(i,1);
  						if(c == 0)
  							error("Too little core for parse tree");
! 						p = (char *)c;
! 						cfree((char *)p,i,1);
  						name = (int *)myalloc(treesize,sizeof(*name));
  						left = (int *)myalloc(treesize,sizeof(*left));
  						right = (int *)myalloc(treesize,sizeof(*right));

--- 248,254 -----
  						xp = (char *)myalloc(i,1);
  						if(xp == 0)
  							error("Too little core for parse tree");
! 						cfree((char *)xp,i,1);
  						name = (int *)myalloc(treesize,sizeof(*name));
  						left = (int *)myalloc(treesize,sizeof(*left));
  						right = (int *)myalloc(treesize,sizeof(*right));
diff -c /arch/s5r2compat/src/lex/sub1.c ./sub1.c
*** /arch/s5r2compat/src/lex/sub1.c	Wed Jan 30 19:56:17 1985
--- ./sub1.c	Tue Aug  6 11:23:46 1985
***************
*** 60,66
  	fprintf(errorf,s,p,d);
  	putc('\n',errorf);
  	fflush(errorf);
! 	fflush(fout);
  	fflush(stdout);
  	}
  index(a,s)

--- 60,67 -----
  	fprintf(errorf,s,p,d);
  	putc('\n',errorf);
  	fflush(errorf);
! 	if(fout != NULL)
! 		fflush(fout);
  	fflush(stdout);
  	}
  index(a,s)