]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
rtla/utils: Fix resource leak in set_comm_sched_attr()
authorWander Lairson Costa <wander@redhat.com>
Mon, 9 Mar 2026 19:46:30 +0000 (16:46 -0300)
committerTomas Glozar <tglozar@redhat.com>
Wed, 11 Mar 2026 14:29:50 +0000 (15:29 +0100)
The set_comm_sched_attr() function opens the /proc directory via
opendir() but fails to call closedir() on its successful exit path.
If the function iterates through all processes without error, it
returns 0 directly, leaking the DIR stream pointer.

Fix this by refactoring the function to use a single exit path. A
retval variable is introduced to track the success or failure status.
All exit points now jump to a unified out label that calls closedir()
before the function returns, ensuring the resource is always freed.

Fixes: dada03db9bb19 ("rtla: Remove procps-ng dependency")
Signed-off-by: Wander Lairson Costa <wander@redhat.com>
Link: https://lore.kernel.org/r/20260309195040.1019085-18-wander@redhat.com
Signed-off-by: Tomas Glozar <tglozar@redhat.com>
tools/tracing/rtla/src/utils.c

index c28ca48a5af004e7984fe5d36b78a6b97d8097a7..80b7eb7b6a7a2ff6f2f1649ce88bd5fe0f7c9bcd 100644 (file)
@@ -390,22 +390,23 @@ int set_comm_sched_attr(const char *comm_prefix, struct sched_attr *attr)
 
                if (strtoi(proc_entry->d_name, &pid)) {
                        err_msg("'%s' is not a valid pid", proc_entry->d_name);
-                       goto out_err;
+                       retval = 1;
+                       goto out;
                }
                /* procfs_is_workload_pid confirmed it is a pid */
                retval = __set_sched_attr(pid, attr);
                if (retval) {
                        err_msg("Error setting sched attributes for pid:%s\n", proc_entry->d_name);
-                       goto out_err;
+                       goto out;
                }
 
                debug_msg("Set sched attributes for pid:%s\n", proc_entry->d_name);
        }
-       return 0;
 
-out_err:
+       retval = 0;
+out:
        closedir(procfs);
-       return 1;
+       return retval;
 }
 
 #define INVALID_VAL    (~0L)