diff options
author | Paolo Bonzini <bonzini@gnu.org> | 2010-02-22 14:36:58 +0100 |
---|---|---|
committer | Paolo Bonzini <bonzini@gnu.org> | 2010-03-05 15:05:35 +0100 |
commit | 401d81946e98f70ed135f57522f58eeeff54cdb2 (patch) | |
tree | ea93ca18b3bcda6ca1b3e5b86b4c7b6acd4af2d6 /lib | |
parent | 29182990773f031a7b39f93a17a05b64fa28e38d (diff) | |
download | grep-401d81946e98f70ed135f57522f58eeeff54cdb2.tar.gz |
fix for bug 21276
* lib/savedir.c (isdir1): Use realloc instead of calloc. Remove
dead code.
(savedir): Do not leak name_space if allocation of new_name_space fails.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/savedir.c | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/lib/savedir.c b/lib/savedir.c index 55c7385b..45bf9c32 100644 --- a/lib/savedir.c +++ b/lib/savedir.c @@ -35,30 +35,35 @@ #include <string.h> #include <fnmatch.h> #include "savedir.h" +#include "xalloc.h" -extern int isdir (const char *path); +static char *path; +static size_t pathlen; -char *path; -size_t pathlen; +extern int isdir (const char *path); static int isdir1 (const char *dir, const char *file) { - int status; - int slash; size_t dirlen = strlen (dir); size_t filelen = strlen (file); + + while (dirlen && path[dirlen - 1] == '/') + dirlen--; + if ((dirlen + filelen + 2) > pathlen) { - path = calloc (dirlen + 1 + filelen + 1, sizeof (*path)); - pathlen = dirlen + filelen + 2; + pathlen *= 2; + if ((dirlen + filelen + 2) > pathlen) + pathlen = dirlen + filelen + 2; + + path = xrealloc (path, pathlen); } - strcpy (path, dir); - slash = (path[dirlen] != '/'); + + memcpy (path, dir, dirlen); path[dirlen] = '/'; - strcpy (path + dirlen + slash , file); - status = isdir (path); - return status; + strcpy (path + dirlen + 1, file); + return isdir (path); } /* Return a freshly allocated string containing the filenames @@ -133,7 +138,7 @@ savedir (const char *dir, off_t name_size, struct exclude *included_patterns, if (new_name_space == NULL) { closedir (dirp); - return NULL; + goto fail; } namep += new_name_space - name_space; name_space = new_name_space; @@ -145,8 +150,9 @@ savedir (const char *dir, off_t name_size, struct exclude *included_patterns, *namep = '\0'; if (CLOSEDIR (dirp)) { + fail: free (name_space); - return NULL; + name_space = NULL; } if (path) { |