diff options
author | delphij <delphij@FreeBSD.org> | 2010-07-23 19:36:11 +0000 |
---|---|---|
committer | delphij <delphij@FreeBSD.org> | 2010-07-23 19:36:11 +0000 |
commit | aa17bd87c6841106f418283b6ef59509d82dd6e5 (patch) | |
tree | 45af8c90a23de241b56a045dd3516dd2b7cd78ef /usr.bin | |
parent | 0c709bf1093eb3e3a2535ee09725b7dd5b635c2f (diff) | |
download | FreeBSD-src-aa17bd87c6841106f418283b6ef59509d82dd6e5.zip FreeBSD-src-aa17bd87c6841106f418283b6ef59509d82dd6e5.tar.gz |
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.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/grep/grep.c | 4 | ||||
-rw-r--r-- | usr.bin/grep/util.c | 23 |
2 files changed, 16 insertions, 11 deletions
diff --git a/usr.bin/grep/grep.c b/usr.bin/grep/grep.c index 9b8b52f..1703c70 100644 --- a/usr.bin/grep/grep.c +++ b/usr.bin/grep/grep.c @@ -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++) diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c index 03793ff..e5aa364 100644 --- a/usr.bin/grep/util.c +++ b/usr.bin/grep/util.c @@ -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); } |