From owner-svn-src-head@FreeBSD.ORG Fri Jul 23 19:36:12 2010 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 162A0106566C; Fri, 23 Jul 2010 19:36:12 +0000 (UTC) (envelope-from delphij@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id EF73E8FC0A; Fri, 23 Jul 2010 19:36:11 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o6NJaBUw095799; Fri, 23 Jul 2010 19:36:11 GMT (envelope-from delphij@svn.freebsd.org) Received: (from delphij@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id o6NJaBgX095796; Fri, 23 Jul 2010 19:36:11 GMT (envelope-from delphij@svn.freebsd.org) Message-Id: <201007231936.o6NJaBgX095796@svn.freebsd.org> From: Xin LI Date: Fri, 23 Jul 2010 19:36:11 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r210430 - head/usr.bin/grep X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 23 Jul 2010 19:36:12 -0000 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); }