1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-23 11:18:54 +00:00

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.
This commit is contained in:
Xin LI 2010-07-23 19:36:11 +00:00
parent 69a8f9e3d1
commit 0c41ffb3d4
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=210430
2 changed files with 16 additions and 11 deletions

View File

@ -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++)

View File

@ -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);
}