Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 31 Aug 2011 23:04:24 +0000 (UTC)
From:      Gabor Kovesdan <gabor@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-user@freebsd.org
Subject:   svn commit: r225298 - user/gabor/grep/trunk
Message-ID:  <201108312304.p7VN4Osm020957@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: gabor
Date: Wed Aug 31 23:04:24 2011
New Revision: 225298
URL: http://svn.freebsd.org/changeset/base/225298

Log:
  - Use fstat() after opening the file, which is safer
  - Only disallow directories for pattern file
  
  Suggested by:	jh

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

Modified: user/gabor/grep/trunk/grep.c
==============================================================================
--- user/gabor/grep/trunk/grep.c	Wed Aug 31 16:54:05 2011	(r225297)
+++ user/gabor/grep/trunk/grep.c	Wed Aug 31 23:04:24 2011	(r225298)
@@ -38,6 +38,7 @@ __FBSDID("$FreeBSD$");
 #include <ctype.h>
 #include <err.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <getopt.h>
 #include <limits.h>
 #include <libgen.h>
@@ -293,13 +294,18 @@ read_patterns(const char *fn)
 	FILE *f;
 	char *line;
 	size_t len;
+	int fd;
 
-	if ((stat(fn, &st) == -1) || !(S_ISREG(st.st_mode)))
+	if ((fd = open(fn, O_RDONLY)) == -1)
 		return;
-	if ((f = fopen(fn, "r")) == NULL)
+	if ((fstat(fd, &st) == -1) || (S_ISDIR(st.st_mode))) {
+		close(fd);
+		return;
+	}
+	if ((f = fdopen(fd, "r")) == NULL)
 		err(2, "%s", fn);
-	while ((line = fgetln(f, &len)) != NULL)
-		add_pattern(line, *line == '\n' ? 0 : len);
+        while ((line = fgetln(f, &len)) != NULL)
+		add_pattern(line, line[0] == '\n' ? 0 : len);
 	if (ferror(f))
 		err(2, "%s", fn);
 	fclose(f);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201108312304.p7VN4Osm020957>