[net.bugs.4bsd] "xstr" doesn't check for "calloc" or "fclose" failing

guy@sun.uucp (Guy Harris) (09/26/85)

Index:	ucb/xstr.c 4.2BSD

Description:
	"xstr" assumes that "calloc" will never fail to allocate
	storage.  It also assumes that "fclose" will never fail,
	although it checks most other I/O operations.

	"calloc" can fail, even on a 4.2BSD system, if you run out
	of swap space - which happens all too frequently on my
	machine.  "fclose" can fail, since it flushes any buffered
	I/O out to the file, and can get an I/O error doing so.
Repeat-By:
	Inspect the code.
Fix:
	The code to discard unused function return values has also
	been cleaned up.

*** /arch/4.2/usr/src/ucb/xstr.c	Sat Oct 23 21:48:33 1982
--- ./xstr.c	Wed Sep 25 12:32:48 1985
***************
*** 11,17
   * November, 1978
   */
  
! #define	ignore(a)	Ignore((char *) a)
  
  char	*calloc();
  off_t	tellpt;

--- 11,17 -----
   * November, 1978
   */
  
! #define	ignore(a)	((void) a)
  
  char	*calloc();
  off_t	tellpt;
***************
*** 269,275
  		if (i >= 0)
  			return (hp->hpt + i);
  	}
! 	hp = (struct hash *) calloc(1, sizeof (*hp));
  	hp->hpt = mesgpt;
  	hp->hstr = savestr(str);
  	mesgpt += strlen(hp->hstr) + 1;

--- 269,278 -----
  		if (i >= 0)
  			return (hp->hpt + i);
  	}
! 	if ((hp = (struct hash *) calloc(1, sizeof (*hp))) == NULL) {
! 		perror("xstr");
! 		exit(8);
! 	}
  	hp->hpt = mesgpt;
  	hp->hstr = savestr(str);
  	mesgpt += strlen(hp->hstr) + 1;
***************
*** 307,313
  					perror(strings), exit(4);
  			}
  		}
! 	ignore(fclose(mesgwrit));
  }
  
  found(new, off, str)

--- 310,317 -----
  					perror(strings), exit(4);
  			}
  		}
! 	if (fclose(mesgwrit) == EOF)
! 		perror(strings), exit(4);
  }
  
  found(new, off, str)
***************
*** 315,322
  	off_t off;
  	char *str;
  {
- 	register char *cp;
- 
  	if (vflg == 0)
  		return;
  	if (!new)

--- 319,324 -----
  	off_t off;
  	char *str;
  {
  	if (vflg == 0)
  		return;
  	if (!new)
***************
*** 381,387
  savestr(cp)
  	register char *cp;
  {
! 	register char *dp = (char *) calloc(1, strlen(cp) + 1);
  
  	return (strcpy(dp, cp));
  }

--- 383,389 -----
  savestr(cp)
  	register char *cp;
  {
! 	register char *dp;
  
  	if ((dp = (char *) calloc(1, strlen(cp) + 1)) == NULL) {
  		perror("xstr");
***************
*** 383,388
  {
  	register char *dp = (char *) calloc(1, strlen(cp) + 1);
  
  	return (strcpy(dp, cp));
  }
  

--- 385,394 -----
  {
  	register char *dp;
  
+ 	if ((dp = (char *) calloc(1, strlen(cp) + 1)) == NULL) {
+ 		perror("xstr");
+ 		exit(8);
+ 	}
  	return (strcpy(dp, cp));
  }
  
***************
*** 386,405
  	return (strcpy(dp, cp));
  }
  
- Ignore(a)
- 	char *a;
- {
- 
- 	a = a;
- }
- 
- ignorf(a)
- 	int (*a)();
- {
- 
- 	a = a;
- }
- 
  lastchr(cp)
  	register char *cp;
  {

--- 392,397 -----
  	return (strcpy(dp, cp));
  }
  
  lastchr(cp)
  	register char *cp;
  {
***************
*** 422,428
  onintr()
  {
  
! 	ignorf(signal(SIGINT, SIG_IGN));
  	if (strings[0] == '/')
  		ignore(unlink(strings));
  	ignore(unlink("x.c"));

--- 414,420 -----
  onintr()
  {
  
! 	ignore(signal(SIGINT, SIG_IGN));
  	if (strings[0] == '/')
  		ignore(unlink(strings));
  	ignore(unlink("x.c"));

	Guy Harris