From: Jan Safranek Date: Wed, 24 Aug 2011 09:41:19 +0000 (+0200) Subject: libcgroup: Rework walk_tree iterator to release all resources on error. X-Git-Tag: v0.38~51 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fef75d9cbc6fe5aa474670cfcf03ca69713f09ec;p=thirdparty%2Flibcgroup.git libcgroup: Rework walk_tree iterator to release all resources on error. 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 Acked-by: Dhaval Giani --- diff --git a/src/api.c b/src/api.c index 2ed9e223..e0ed696d 100644 --- 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; }