]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
v2 [patch 4/6] api: Use a new counter in an inner loop
authorDhaval Giani <dhaval.giani@gmail.com>
Tue, 16 Nov 2010 13:29:49 +0000 (14:29 +0100)
committerDhaval Giani <dhaval.giani@gmail.com>
Tue, 16 Nov 2010 15:39:17 +0000 (16:39 +0100)
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 <sgrubb@redhat.com>
Signed-off-by: Dhaval Giani <dhaval.giani@gmail.com>
Acked-By: Jan Safranek <jsafrane@redhat.com>
src/api.c

index 91483ca6b41821df6acf4e3276a81fb07527fe86..9f231218c0fea4334299e38aa8507dd1bec670a1 100644 (file)
--- 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;