]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
libcgroup: Rework walk_tree iterator to release all resources on error.
authorJan Safranek <jsafrane@redhat.com>
Wed, 24 Aug 2011 09:41:19 +0000 (11:41 +0200)
committerJan Safranek <jsafrane@redhat.com>
Wed, 14 Sep 2011 11:24:29 +0000 (13:24 +0200)
cgroup_walk_tree_begin() can end up with error and in some code paths
'*handle' is not set and in some it is.

The function should free all resources on error + return *handle = NULL

Signed-off-by: Jan Safranek <jsafrane@redhat.com>
Acked-by: Dhaval Giani <dhaval.giani@gmail.com>
src/api.c

index 2ed9e2233f11d246ade7cd0a63057aa304c9dc16..e0ed696d7875d512e8d7c1b64103c3d4507e1213 100644 (file)
--- a/src/api.c
+++ b/src/api.c
@@ -3078,8 +3078,6 @@ int cgroup_walk_tree_begin(const char *controller, const char *base_path,
 
        cgroup_dbg("path is %s\n", base_path);
 
-       cgroup_dbg("path is %s\n", base_path);
-
        if (!cg_build_path(base_path, full_path, controller))
                return ECGOTHER;
 
@@ -3087,6 +3085,7 @@ int cgroup_walk_tree_begin(const char *controller, const char *base_path,
 
        if (!entry) {
                last_errno = errno;
+               *handle = NULL;
                return ECGOTHER;
        }
 
@@ -3101,20 +3100,28 @@ int cgroup_walk_tree_begin(const char *controller, const char *base_path,
        if (entry->fts == NULL) {
                free(entry);
                last_errno = errno;
+               *handle = NULL;
                return ECGOTHER;
        }
        ent = fts_read(entry->fts);
        if (!ent) {
                cgroup_dbg("fts_read failed\n");
+               fts_close(entry->fts);
                free(entry);
+               *handle = NULL;
                return ECGINVAL;
        }
        if (!*base_level && depth)
                *base_level = ent->fts_level + depth;
 
        ret = cg_walk_node(entry->fts, ent, *base_level, info, entry->flags);
-
-       *handle = entry;
+       if (ret != 0) {
+               fts_close(entry->fts);
+               free(entry);
+               *handle = NULL;
+       } else {
+               *handle = entry;
+       }
        return ret;
 }