From 0eee3ccaae5bb3d0016a0da8b8e5108767c02748 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 31 Mar 2017 22:41:38 +0200 Subject: [PATCH 2/3] glob: Fix 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 | 13 +++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7c15292..cca7542 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2017-03-31 Bruno Haible + glob: Fix memory leaks. + * lib/glob.c (glob): Free allocated memory before returning. + Reported by Coverity via Tim Rühsen. + +2017-03-31 Bruno Haible + md5, sha1, sha256, sha512: Add comments regarding correctness. * lib/md5.h (buflen): Add comments regarding range. * lib/sha1.h (buflen): Likewise. diff --git a/lib/glob.c b/lib/glob.c index e69a96e..1337817 100644 --- a/lib/glob.c +++ b/lib/glob.c @@ -789,10 +789,14 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), malloc_home_dir = 1; } memcpy (home_dir, p->pw_dir, home_dir_len); - - free (pwtmpbuf); } } + free (malloc_pwtmpbuf); + } + else + { + if (__glibc_unlikely (malloc_name)) + free (name); } } if (home_dir == NULL || home_dir[0] == '\0') @@ -847,6 +851,9 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), dirname = newp; dirlen += home_len - 1; malloc_dirname = !use_alloca; + + if (__glibc_unlikely (malloc_home_dir)) + free (home_dir); } dirname_modified = 1; } @@ -1048,6 +1055,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (newcount > SIZE_MAX / sizeof (char *) - 2) { nospace: + if (__glibc_unlikely (malloc_dirname)) + free (dirname); free (pglob->gl_pathv); pglob->gl_pathv = NULL; pglob->gl_pathc = 0; -- 2.7.4