Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 23 Jul 2010 19:36:11 +0000 (UTC)
From:      Xin LI <delphij@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r210430 - head/usr.bin/grep
Message-ID:  <201007231936.o6NJaBgX095796@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: delphij
Date: Fri Jul 23 19:36:11 2010
New Revision: 210430
URL: http://svn.freebsd.org/changeset/base/210430

Log:
  Fix crashes when using grep -R:
  
   - Explicitly pre-zero memory for fts_open parameters.
   - Don't test against directory patterns when we are testing direct
     leaf of current directory.
  
  While I'm there plug a few of memory leaks.

Modified:
  head/usr.bin/grep/grep.c
  head/usr.bin/grep/util.c

Modified: head/usr.bin/grep/grep.c
==============================================================================
--- head/usr.bin/grep/grep.c	Fri Jul 23 18:58:27 2010	(r210429)
+++ head/usr.bin/grep/grep.c	Fri Jul 23 19:36:11 2010	(r210430)
@@ -351,8 +351,8 @@ main(int argc, char *argv[])
 		}
 		eargv[++eargc] = NULL;
 
-		aargv = (char **)grep_malloc(sizeof(char *) *
-		    (eargc + argc + 1));
+		aargv = (char **)grep_calloc(eargc + argc + 1,
+		    sizeof(char *));
 		aargv[0] = argv[0];
 
 		for(i = 1; i < eargc; i++)

Modified: head/usr.bin/grep/util.c
==============================================================================
--- head/usr.bin/grep/util.c	Fri Jul 23 18:58:27 2010	(r210429)
+++ head/usr.bin/grep/util.c	Fri Jul 23 19:36:11 2010	(r210430)
@@ -60,7 +60,7 @@ grep_tree(char **argv)
 {
 	FTS *fts;
 	FTSENT *p;
-	char *d, *dir;
+	char *d, *dir = NULL;
 	unsigned int i;
 	int c, fts_flags;
 	bool ok;
@@ -82,7 +82,7 @@ grep_tree(char **argv)
 	fts_flags |= FTS_NOSTAT | FTS_NOCHDIR;
 
 	if (!(fts = fts_open(argv, fts_flags, NULL)))
-		err(2, NULL);
+		err(2, "fts_open");
 	while ((p = fts_read(fts)) != NULL) {
 		switch (p->fts_info) {
 		case FTS_DNR:
@@ -103,11 +103,12 @@ grep_tree(char **argv)
 			/* Check for file exclusion/inclusion */
 			ok = true;
 			if (exclflag) {
-				d = strrchr(p->fts_path, '/');
-				dir = grep_malloc(sizeof(char) *
-				    (d - p->fts_path + 2));
-				strlcpy(dir, p->fts_path,
-				    (d - p->fts_path + 1));
+				if ((d = strrchr(p->fts_path, '/')) != NULL) {
+					dir = grep_malloc(sizeof(char) *
+					    (d - p->fts_path + 2));
+					strlcpy(dir, p->fts_path,
+					    (d - p->fts_path + 1));
+				}
 				for (i = 0; i < epatterns; ++i) {
 					switch(epattern[i].type) {
 					case FILE_PAT:
@@ -116,13 +117,14 @@ grep_tree(char **argv)
 							ok = epattern[i].mode != EXCL_PAT;
 						break;
 					case DIR_PAT:
-						if (strstr(dir,
+						if (dir != NULL && strstr(dir,
 						    epattern[i].pat) != NULL)
 							ok = epattern[i].mode != EXCL_PAT;
 						break;
 					}
 				}
-			free(dir);
+				free(dir);
+				dir = NULL;
 			}
 
 			if (ok)
@@ -131,6 +133,7 @@ grep_tree(char **argv)
 		}
 	}
 
+	fts_close(fts);
 	return (c);
 }
 
@@ -196,6 +199,7 @@ procfile(const char *fn)
 		/* Return if we need to skip a binary file */
 		if (f->binary && binbehave == BINFILE_SKIP) {
 			grep_close(f);
+			free(ln.file);
 			free(f);
 			return (0);
 		}
@@ -230,6 +234,7 @@ procfile(const char *fn)
 	    binbehave == BINFILE_BIN && f->binary && !qflag)
 		printf(getstr(9), fn);
 
+	free(ln.file);
 	free(f);
 	return (c);
 }



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