From 5049f1971ec7c2893e0e5b8a7f1eb7d2909f8bd3 Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Wed, 5 Feb 2003 17:47:50 +0000 Subject: Update. 2003-02-05 Jim Meyering Fix a heap-corrupting bug. * io/ftw.c: Include . (PATH_MAX) [!defined PATH_MAX]: Define to 1024. (process_entry): Allocate enough space to hold the resulting file name. Don't presume that 2*dirbufsize is enough. (ftw_startup): Always use PATH_MAX to compute buffer size, now that it is guaranteed to be defined. --- io/ftw.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'io/ftw.c') diff --git a/io/ftw.c b/io/ftw.c index 6b117765cb..45001d5b54 100644 --- a/io/ftw.c +++ b/io/ftw.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -84,6 +85,14 @@ # define NFTW_FUNC_T __nftw_func_t #endif +/* We define PATH_MAX if the system does not provide a definition. + This does not artificially limit any operation. PATH_MAX is simply + used as a guesstimate for the expected maximal path length. + Buffers will be enlarged if necessary. */ +#ifndef PATH_MAX +# define PATH_MAX 1024 +#endif + struct dir_data { DIR *stream; @@ -282,18 +291,20 @@ process_entry (struct ftw_data *data, struct dir_data *dir, const char *name, struct STAT st; int result = 0; int flag = 0; + size_t new_buflen; if (name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) /* Don't process the "." and ".." entries. */ return 0; - if (data->dirbufsize < data->ftw.base + namlen + 2) + new_buflen = data->ftw.base + namlen + 2; + if (data->dirbufsize < new_buflen) { /* Enlarge the buffer. */ char *newp; - data->dirbufsize *= 2; + data->dirbufsize = 2 * new_buflen; newp = (char *) realloc (data->dirbuf, data->dirbufsize); if (newp == NULL) return -1; @@ -518,11 +529,8 @@ ftw_startup (const char *dir, int is_nftw, void *func, int descriptors, * sizeof (struct dir_data *)); memset (data.dirstreams, '\0', data.maxdir * sizeof (struct dir_data *)); -#ifdef PATH_MAX + /* PATH_MAX is always defined when we get here. */ data.dirbufsize = MAX (2 * strlen (dir), PATH_MAX); -#else - data.dirbufsize = 2 * strlen (dir); -#endif data.dirbuf = (char *) malloc (data.dirbufsize); if (data.dirbuf == NULL) return -1; -- cgit 1.4.1