From 443e6b765df8ee30cc3d6a283f079f1e5255c6dc Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 1 Apr 2017 15:15:18 +0200 Subject: [PATCH] glob: Fix more memory leaks. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * lib/glob.c (glob): Free allocated memory before returning. Reported by Coverity via Tim Rühsen. --- ChangeLog | 6 ++++++ lib/glob.c | 19 ++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2e01161..1f4d3c0 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-03-31 Bruno Haible glob: Fix invalid free() call. diff --git a/lib/glob.c b/lib/glob.c index 9305038..d4fdc17 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; -- 2.7.4