]> Savannah Git Hosting - gnulib.git/commitdiff
glob: Fix memory leaks.
authorBruno Haible <bruno@clisp.org>
Fri, 31 Mar 2017 20:41:38 +0000 (22:41 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 31 Mar 2017 20:41:38 +0000 (22:41 +0200)
* lib/glob.c (glob): Free allocated memory before returning.
Reported by Coverity via Tim Rühsen.

ChangeLog
lib/glob.c

index 7c15292c174bf21166f6d0d9a97585a24bec991c..cca7542d45a5531313b6cb66cfffeb3a6060d89b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2017-03-31  Bruno Haible  <bruno@clisp.org>
+
+       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  <bruno@clisp.org>
 
        md5, sha1, sha256, sha512: Add comments regarding correctness.
index e69a96e594e201fe80617bfe3e0401f3067f11bf..1337817c2213e2c05a7de9113ea832e3eb395bbf 100644 (file)
@@ -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;