From: Bruno Haible Date: Sat, 1 Apr 2017 13:15:18 +0000 (+0200) Subject: glob: Fix more memory leaks. X-Git-Tag: v1.0~6250 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=1540f3441555f756558f3a18e5f68914c0b72227;p=gnulib.git glob: Fix more memory leaks. * lib/glob.c (glob): Free allocated memory before returning. Reported by Coverity via Tim Rühsen. --- diff --git a/ChangeLog b/ChangeLog index 43dd861784..629d6ea293 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2017-04-01 Bruno Haible + + glob: Fix more memory leaks. + * lib/glob.c (glob): Free allocated memory before returning. + Reported by Coverity via Tim Rühsen. + 2017-04-22 Paul Eggert poll: improve fast check for out-of-range NFD diff --git a/lib/glob.c b/lib/glob.c index 93050389cd..d4fdc1737b 100644 --- a/lib/glob.c +++ b/lib/glob.c @@ -726,6 +726,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), pwtmpbuf = malloc (pwbuflen); if (pwtmpbuf == NULL) { + if (__glibc_unlikely (malloc_name)) + free (name); retval = GLOB_NOSPACE; goto out; } @@ -754,6 +756,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (newp == NULL) { free (malloc_pwtmpbuf); + if (__glibc_unlikely (malloc_name)) + free (name); retval = GLOB_NOSPACE; goto out; } @@ -801,10 +805,10 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), } if (home_dir == NULL || home_dir[0] == '\0') { + if (__glibc_unlikely (malloc_home_dir)) + free (home_dir); if (flags & GLOB_TILDE_CHECK) { - if (__glibc_unlikely (malloc_home_dir)) - free (home_dir); retval = GLOB_NOMATCH; goto out; } @@ -1084,9 +1088,14 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), } else { - pglob->gl_pathv[newcount] = strdup (dirname); - if (pglob->gl_pathv[newcount] == NULL) - goto nospace; + if (__glibc_unlikely (malloc_dirname)) + pglob->gl_pathv[newcount] = dirname; + else + { + pglob->gl_pathv[newcount] = strdup (dirname); + if (pglob->gl_pathv[newcount] == NULL) + goto nospace; + } } pglob->gl_pathv[++newcount] = NULL; ++pglob->gl_pathc;