[augeas-devel] [PATCH 8/8] Plug a few memory leaks

David Lutterkort lutter at redhat.com
Wed Feb 25 21:58:38 UTC 2009


---
 src/augeas.c  |    8 ++++++--
 src/augtool.c |   12 ++++++++----
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/src/augeas.c b/src/augeas.c
index 2ba574c..cd3da0f 100644
--- a/src/augeas.c
+++ b/src/augeas.c
@@ -72,8 +72,10 @@ static struct pathx *parse_user_pathx(const struct augeas *aug,
         return result;
 
     if (pathx_parse(aug->origin, AUGEAS_META_PATHX "/error", &err)
-        != PATHX_NOERROR)
+        != PATHX_NOERROR) {
+        free_pathx(result);
         return NULL;
+    }
     tree_set(err, pathx_error(result, NULL, &pos));
     free_pathx(err);
 
@@ -89,6 +91,7 @@ static struct pathx *parse_user_pathx(const struct augeas *aug,
         }
     }
     free_pathx(err);
+    free_pathx(result);
     return NULL;
 }
 
@@ -511,7 +514,7 @@ int aug_match(const struct augeas *aug, const char *pathin, char ***matches) {
     }
 
     if (matches == NULL)
-        return cnt;
+        goto done;
 
     if (ALLOC_N(*matches, cnt) < 0)
         goto error;
@@ -526,6 +529,7 @@ int aug_match(const struct augeas *aug, const char *pathin, char ***matches) {
         }
         i += 1;
     }
+ done:
     free_pathx(p);
     return cnt;
 
diff --git a/src/augtool.c b/src/augtool.c
index 2e48a9b..2e7ba6a 100644
--- a/src/augtool.c
+++ b/src/augtool.c
@@ -131,15 +131,17 @@ static int cmd_match(char *args[]) {
     const char *pattern = cleanpath(args[0]);
     char **matches;
     int filter = (args[1] != NULL) && (strlen(args[1]) > 0);
+    int result = 0;
 
     cnt = aug_match(aug, pattern, &matches);
     if (cnt < 0) {
         printf("  (error matching %s)\n", pattern);
-        return -1;
+        result = -1;
+        goto done;
     }
     if (cnt == 0) {
         printf("  (no matches)\n");
-        return 0;
+        goto done;
     }
 
     for (int i=0; i < cnt; i++) {
@@ -153,10 +155,12 @@ static int cmd_match(char *args[]) {
         } else {
             printf("%s = %s\n", matches[i], val);
         }
-        free((void *) matches[i]);
     }
+ done:
+    for (int i=0; i < cnt; i++)
+        free(matches[i]);
     free(matches);
-    return 0;
+    return result;
 }
 
 static int cmd_rm(char *args[]) {
-- 
1.6.0.6




More information about the augeas-devel mailing list