svn commit: r225274 - user/gabor/grep/trunk

Gabor Kovesdan gabor at FreeBSD.org
Tue Aug 30 23:55:46 UTC 2011


Author: gabor
Date: Tue Aug 30 23:55:45 2011
New Revision: 225274
URL: http://svn.freebsd.org/changeset/base/225274

Log:
  - Avoid segfault wheen pattern file is not a regular file

Modified:
  user/gabor/grep/trunk/grep.c

Modified: user/gabor/grep/trunk/grep.c
==============================================================================
--- user/gabor/grep/trunk/grep.c	Tue Aug 30 23:16:54 2011	(r225273)
+++ user/gabor/grep/trunk/grep.c	Tue Aug 30 23:55:45 2011	(r225274)
@@ -289,10 +289,13 @@ add_dpattern(const char *pat, int mode)
 static void
 read_patterns(const char *fn)
 {
+	struct stat st;
 	FILE *f;
 	char *line;
 	size_t len;
 
+	if ((stat(fn, &st) == -1) || !(S_ISREG(st.st_mode)))
+		return;
 	if ((f = fopen(fn, "r")) == NULL)
 		err(2, "%s", fn);
 	while ((line = fgetln(f, &len)) != NULL)
@@ -637,6 +640,10 @@ main(int argc, char *argv[])
 	aargc -= optind;
 	aargv += optind;
 
+	/* Empty pattern file matches nothing */
+	if (!needpattern && (patterns == 0))
+		exit(1);
+
 	/* Fail if we don't have any pattern */
 	if (aargc == 0 && needpattern)
 		usage();


More information about the svn-src-user mailing list