[comp.bugs.4bsd] touch with no files dereferences a null pointer +Fix

gww@beatnix.UUCP (Gary Winiger) (03/13/88)

Subject: touch with no files dereferences a null pointer +Fix
Index:	usr.bin/touch.c 4.3BSD +Fix

Description:
	If touch is called with no files, it will dereference a null pointer.
	On systems which don't permit null pointer dereferences, this will
	cause a core dump.
Repeat-By:
	touch
	On a system which doesn't permit null pointer dereferences.
Fix:
	Test to see if a parameter was passed.
	The attached code solves this problem at Elxsi.

Gary..
{ucbvax!sun,uunet,lll-lcc!lll-tis,amdahl!altos86,bridge2}!elxsi!gww
--------- cut --------- snip --------- :.,$w diff -------------
*** /tmp/,RCSt1001870	Tue Sep  8 17:41:04 1987
--- touch.c	Tue Sep  8 17:15:19 1987
***************
*** 1,11 ****
  /*
   * $Log:	touch.c,v $
   * Revision 1.1  86/12/30  10:24:34  gww
   * Initial revision
   * 
   */
  #ifndef lint
! static char *ERcsId = "$Header: touch.c,v 1.1 86/12/30 10:24:34 gww Exp $ ENIX BSD";
  static	char sccsid[] = "@(#)touch.c	4.3 (Berkeley) 8/11/83";
  #endif
  
--- 1,15 ----
  /*
   * $Log:	touch.c,v $
+  * Revision 1.2  87/09/08  17:14:30  gww
+  * If no file(s) is (are) specified, give a usage message and don't dereference 
+  * a null pointer.
+  * 
   * Revision 1.1  86/12/30  10:24:34  gww
   * Initial revision
   * 
   */
  #ifndef lint
! static char *ERcsId = "$Header: touch.c,v 1.2 87/09/08 17:14:30 gww Exp $ ENIX BSD";
  static	char sccsid[] = "@(#)touch.c	4.3 (Berkeley) 8/11/83";
  #endif
  
***************
*** 33,39 ****
  
  	dontcreate = 0;
  	force = 0;
! 	for (argv++; **argv == '-'; argv++) {
  		for (argp = &(*argv)[1]; *argp; argp++) {
  			switch (*argp) {
  			case 'c':
--- 37,43 ----
  
  	dontcreate = 0;
  	force = 0;
! 	for (argc--, argv++; argc >= 1 && **argv == '-'; argc--, argv++) {
  		for (argp = &(*argv)[1]; *argp; argp++) {
  			switch (*argp) {
  			case 'c':
***************
*** 48,53 ****
--- 52,61 ----
  				exit(1);
  			}
  		}
+ 	}
+ 	if (argc < 1) {
+ 		fprintf(stderr, "usage: touch [-c] [-f] file ...\n");
+ 		exit(1);
  	}
  	for (/*void*/; *argv; argv++) {
  		touch(*argv);