[augeas-devel] augeas: master - Do not assume how glob allocates gl_pathv

David Lutterkort lutter at fedoraproject.org
Fri Aug 14 21:19:27 UTC 2009


Gitweb:        http://git.fedorahosted.org/git/augeas.git?p=augeas.git;a=commitdiff;h=ca5b2996127574acd2aed91dbd3d763ecc8ef613
Commit:        ca5b2996127574acd2aed91dbd3d763ecc8ef613
Parent:        7630b4db3962d1f16b60d692e975b66855aeb3f3
Author:        David Lutterkort <lutter at redhat.com>
AuthorDate:    Mon Aug 10 17:30:11 2009 -0700
Committer:     David Lutterkort <lutter at redhat.com>
CommitterDate: Wed Aug 12 16:44:46 2009 -0700

Do not assume how glob allocates gl_pathv

We assumed that the gl_pathv in a glob_t was allocated in a particular
manner.

  * src/transform.c (filter_generate): make a copy of gl_pathv
---
 src/transform.c |   57 ++++++++++++++++++++++++++++++++----------------------
 1 files changed, 34 insertions(+), 23 deletions(-)

diff --git a/src/transform.c b/src/transform.c
index f0b4775..d7ff886 100644
--- a/src/transform.c
+++ b/src/transform.c
@@ -122,44 +122,55 @@ static int filter_generate(struct tree *xfm, const char *root,
 
         if (r != 0 && r != GLOB_NOMATCH) {
             ret = -1;
-            goto done;
+            goto error;
         }
         gl_flags |= GLOB_APPEND;
     }
 
-    char **pathv = globbuf.gl_pathv;
-    int pathc = globbuf.gl_pathc;
-    globbuf.gl_pathv = NULL;
-    globbuf.gl_pathc = 0;
+    char **pathv = NULL;
+    int pathc = globbuf.gl_pathc, pathind = 0;
+
+    if (ALLOC_N(pathv, pathc) < 0)
+        goto error;
+
+    for (int i=0; i < pathc; i++) {
+        const char *path = globbuf.gl_pathv[i];
+        bool include = true;
+
+        list_for_each(e, xfm->children) {
+            if (! is_excl(e))
+                continue;
 
-    list_for_each(e, xfm->children) {
-        if (! is_excl(e))
-            continue;
-        for (int i=0; i < pathc;) {
-            const char *path = pathv[i];
             if (strchr(e->value, SEP) == NULL)
                 path = pathbase(path);
-            if (fnmatch(e->value, path, fnm_flags) == 0) {
-                free(pathv[i]);
-                pathc -= 1;
-                if (i < pathc) {
-                    pathv[i] = pathv[pathc];
-                }
-            } else {
-                i += 1;
+            if ((r = fnmatch(e->value, path, fnm_flags)) == 0) {
+                include = false;
             }
         }
+        if (include) {
+            pathv[pathind] = strdup(globbuf.gl_pathv[i]);
+            if (pathv[pathind] == NULL)
+                goto error;
+            pathind += 1;
+        }
     }
-    if (REALLOC_N(pathv, pathc) == -1) {
-        FREE(pathv);
-        pathc = 0;
-        ret = -1;
-    }
+    pathc = pathind;
+
+    if (REALLOC_N(pathv, pathc) == -1)
+        goto error;
+
     *matches = pathv;
     *nmatches = pathc;
  done:
     globfree(&globbuf);
     return ret;
+ error:
+    if (pathv != NULL)
+        for (int i=0; i < pathc; i++)
+            free(pathv[i]);
+    free(pathv);
+    ret = -1;
+    goto done;
 }
 
 static int filter_matches(struct tree *xfm, const char *path) {




More information about the augeas-devel mailing list