From: Dhaval Giani Date: Tue, 16 Nov 2010 13:29:49 +0000 (+0100) Subject: v2 [patch 4/6] api: Use a new counter in an inner loop X-Git-Tag: v0.37~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5f3696744381a5e788da16fa2d7858477461e0d3;p=thirdparty%2Flibcgroup.git v2 [patch 4/6] api: Use a new counter in an inner loop The same counter is reused in an inner loop in cg_prepare_cgroup. This is perfectly fine simply because we never exit the inner loop to the outer loop. The only way out of the inner loop leads to a return statement, during which there is no mention of the outer counter. However, this is ugly code, and hard to read and may lead to bugs if some decides to refactor the code. So clean it all up using a different counter. Thanks to Steve Grubb for raising this issue at http://article.gmane.org/gmane.comp.lib.libcg.devel/2485 Reported-by: Steve Grubb Signed-off-by: Dhaval Giani Acked-By: Jan Safranek --- diff --git a/src/api.c b/src/api.c index 91483ca6..9f231218 100644 --- a/src/api.c +++ b/src/api.c @@ -2208,6 +2208,7 @@ static int cg_prepare_cgroup(struct cgroup *cgroup, pid_t pid, /* Scan all the controllers */ for (i = 0; i < CG_CONTROLLER_MAX; i++) { + int j = 0; if (!controllers[i]) return 0; controller = controllers[i]; @@ -2216,16 +2217,16 @@ static int cg_prepare_cgroup(struct cgroup *cgroup, pid_t pid, * controllers. */ if (strcmp(controller, "*") == 0) { pthread_rwlock_rdlock(&cg_mount_table_lock); - for (i = 0; i < CG_CONTROLLER_MAX && - cg_mount_table[i].name[0] != '\0'; i++) { + for (j = 0; j < CG_CONTROLLER_MAX && + cg_mount_table[j].name[0] != '\0'; j++) { cgroup_dbg("Adding controller %s\n", - cg_mount_table[i].name); + cg_mount_table[j].name); cptr = cgroup_add_controller(cgroup, - cg_mount_table[i].name); + cg_mount_table[j].name); if (!cptr) { cgroup_dbg("Adding controller '%s'" " failed\n", - cg_mount_table[i].name); + cg_mount_table[j].name); pthread_rwlock_unlock(&cg_mount_table_lock); cgroup_free_controllers(cgroup); return ECGROUPNOTALLOWED;