]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cpu-set-util: several cleanups for cpu_set_to_string() and cpu_set_to_range_string()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 20 Jun 2025 20:11:59 +0000 (05:11 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 23 Jun 2025 15:20:20 +0000 (00:20 +0900)
- Add missing assertions.
- Replace GREEDY_REALLOC() + sprintf() with strextendf_with_separator().

src/shared/cpu-set-util.c
src/shared/cpu-set-util.h

index 1532e0c9e36efe99e4e37b87fc206f68b7675ce5..dbef2204e5a18d91b35028ed94a2f78ac85f3b50 100644 (file)
 #include "parse-util.h"
 #include "string-util.h"
 
-char* cpu_set_to_string(const CPUSet *a) {
+char* cpu_set_to_string(const CPUSet *c) {
         _cleanup_free_ char *str = NULL;
-        size_t len = 0;
-        int i, r;
 
-        for (i = 0; (size_t) i < a->allocated * 8; i++) {
-                if (!CPU_ISSET_S(i, a->allocated, a->set))
+        assert(c);
+
+        for (size_t i = 0; i < c->allocated * 8; i++) {
+                if (!CPU_ISSET_S(i, c->allocated, c->set))
                         continue;
 
-                if (!GREEDY_REALLOC(str, len + 1 + DECIMAL_STR_MAX(int)))
+                if (strextendf_with_separator(&str, " ", "%zu", i) < 0)
                         return NULL;
-
-                r = sprintf(str + len, len > 0 ? " %d" : "%d", i);
-                assert_se(r > 0);
-                len += r;
         }
 
         return TAKE_PTR(str) ?: strdup("");
 }
 
-char* cpu_set_to_range_string(const CPUSet *set) {
-        unsigned range_start = 0, range_end;
+static int add_range(char **str, size_t start, size_t end) {
+        assert(str);
+        assert(start <= end);
+
+        if (start == end)
+                return strextendf_with_separator(str, " ", "%zu", start);
+
+        return strextendf_with_separator(str, " ", "%zu-%zu", start, end);
+}
+
+char* cpu_set_to_range_string(const CPUSet *c) {
         _cleanup_free_ char *str = NULL;
+        size_t start = 0, end;
         bool in_range = false;
-        size_t len = 0;
-        int r;
 
-        for (unsigned i = 0; i < set->allocated * 8; i++)
-                if (CPU_ISSET_S(i, set->allocated, set->set)) {
+        assert(c);
+
+        for (size_t i = 0; i < c->allocated * 8; i++) {
+                if (CPU_ISSET_S(i, c->allocated, c->set)) {
                         if (in_range)
-                                range_end++;
+                                end++;
                         else {
-                                range_start = range_end = i;
+                                start = end = i;
                                 in_range = true;
                         }
-                } else if (in_range) {
-                        in_range = false;
-
-                        if (!GREEDY_REALLOC(str, len + 2 + 2 * DECIMAL_STR_MAX(unsigned)))
-                                return NULL;
-
-                        if (range_end > range_start)
-                                r = sprintf(str + len, len > 0 ? " %u-%u" : "%u-%u", range_start, range_end);
-                        else
-                                r = sprintf(str + len, len > 0 ? " %u" : "%u", range_start);
-                        assert_se(r > 0);
-                        len += r;
+                        continue;
                 }
 
-        if (in_range) {
-                if (!GREEDY_REALLOC(str, len + 2 + 2 * DECIMAL_STR_MAX(int)))
+                if (in_range && add_range(&str, start, end) < 0)
                         return NULL;
 
-                if (range_end > range_start)
-                        r = sprintf(str + len, len > 0 ? " %u-%u" : "%u-%u", range_start, range_end);
-                else
-                        r = sprintf(str + len, len > 0 ? " %u" : "%u", range_start);
-                assert_se(r > 0);
+                in_range = false;
         }
 
+        if (in_range && add_range(&str, start, end) < 0)
+                return NULL;
+
         return TAKE_PTR(str) ?: strdup("");
 }
 
index e2ea839aa89cbbd0a19de1550b87c1592d922af8..5169fff14bdcd1ac2418ce581b3db6b50f97d48f 100644 (file)
@@ -24,9 +24,9 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(CPUSet*, cpu_set_free);
 int cpu_set_add_all(CPUSet *a, const CPUSet *b);
 int cpu_set_add(CPUSet *a, unsigned cpu);
 
-char* cpu_set_to_string(const CPUSet *a);
-char* cpu_set_to_range_string(const CPUSet *a);
-char* cpu_set_to_mask_string(const CPUSet *a);
+char* cpu_set_to_string(const CPUSet *c);
+char* cpu_set_to_range_string(const CPUSet *c);
+char* cpu_set_to_mask_string(const CPUSet *c);
 int cpu_set_realloc(CPUSet *cpu_set, unsigned ncpus);
 
 int parse_cpu_set_full(