]> Savannah Git Hosting - gnulib.git/commitdiff
glob: Fix more memory leaks.
authorBruno Haible <bruno@clisp.org>
Sat, 1 Apr 2017 13:15:18 +0000 (15:15 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 23 Apr 2017 10:55:44 +0000 (12:55 +0200)
* lib/glob.c (glob): Free allocated memory before returning.
Reported by Coverity via Tim Rühsen.

ChangeLog
lib/glob.c

index 43dd8617849ae83f8bbb6b8a6ba8ac002d82b101..629d6ea293a14a8401f2003ae9993c45c3328a34 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2017-04-01  Bruno Haible  <bruno@clisp.org>
+
+       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  <eggert@cs.ucla.edu>
 
        poll: improve fast check for out-of-range NFD
index 93050389cd3eda61647b23c2bbf65b5af03b464a..d4fdc1737b84da476d924bb4063601305c6d058d 100644 (file)
@@ -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;