]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
bus-unit-procs: add one more assertion for Coverity
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 19 Mar 2025 01:35:17 +0000 (10:35 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 19 Mar 2025 01:41:29 +0000 (10:41 +0900)
Hopefully fixes CID#1562447.

src/shared/bus-unit-procs.c

index ff4852150bee2c47fa0b9363d0f076d4c8637a17..19139403b8057fe7a94ae0dc0f93135ffae4ae35 100644 (file)
@@ -153,23 +153,22 @@ static int dump_processes(
 
         if (!hashmap_isempty(cg->pids)) {
                 const char *name;
-                size_t n = 0, i;
-                pid_t *pids;
                 void *pidp;
-                int width;
+                size_t n = 0;
 
                 /* Order processes by their PID */
-                pids = newa(pid_t, hashmap_size(cg->pids));
+                pid_t *pids = newa(pid_t, hashmap_size(cg->pids));
 
                 HASHMAP_FOREACH_KEY(name, pidp, cg->pids)
                         pids[n++] = PTR_TO_PID(pidp);
 
                 assert(n == hashmap_size(cg->pids));
+                assert(n > 0);
                 typesafe_qsort(pids, n, pid_compare_func);
 
-                width = DECIMAL_STR_WIDTH(pids[n-1]);
+                int width = DECIMAL_STR_WIDTH(pids[n-1]);
 
-                for (i = 0; i < n; i++) {
+                for (size_t i = 0; i < n; i++) {
                         _cleanup_free_ char *e = NULL;
                         const char *special;
                         bool more;