Yet ANOTHER null pointer dereference problem (in "cat")

Guy Harris guy at sun.uucp
Thu Sep 26 14:45:28 AEST 1985


PLEASE, people: argv[argc] does NOT point to a string.  It is a null pointer.
Code which dereferences it is illegal.  If you want to write a program which
reads standard input if 1) no arguments are given or 2) an argument of "-"
is given, PLEASE check whether any arguments were given before checking if
an argument of "-" was given!  (One would think that it was obvious that one
should first check if something exists before checking if it has a
particular property, but there's a hell of a lot of code that seems not
to...)

*** cat.c.BAK	Mon Sep 23 13:06:36 1985
--- cat.c	Wed Sep 25 11:57:05 1985
***************
*** 85,91
  	for (argv = &argv[optind];
  	     optind < argc && !ferror(stdout); optind++, argv++) {
  		filenm = *argv;
! 		if (filenm[0]=='-' && filenm[1]=='\0' || stdinflg) {
  			filenm = "standard input";
  			fi = stdin;
  		} else {

--- 85,91 -----
  	for (argv = &argv[optind];
  	     optind < argc && !ferror(stdout); optind++, argv++) {
  		filenm = *argv;
! 		if (stdinflg || (filenm[0]=='-' && filenm[1]=='\0')) {
  			filenm = "standard input";
  			fi = stdin;
  		} else {

	Guy Harris



More information about the Net.bugs.usg mailing list