b9a53a
From 35894625624f0e8c7d3ca2c200861005c7ad4435 Mon Sep 17 00:00:00 2001
b9a53a
From: Michal Sekletar <msekleta@redhat.com>
b9a53a
Date: Mon, 3 Jun 2019 10:12:35 +0200
b9a53a
Subject: [PATCH] cpu-set-util: use %d-%d format in  cpu_set_to_range_string()
b9a53a
 only for actual ranges
b9a53a
b9a53a
(cherry picked from commit 71923237b18df35401626993d8a285cd998be78d)
b9a53a
b9a53a
Related: #1734787
b9a53a
---
b9a53a
 src/basic/cpu-set-util.c     |  4 ++--
b9a53a
 src/test/test-cpu-set-util.c | 16 +++++++++-------
b9a53a
 2 files changed, 11 insertions(+), 9 deletions(-)
b9a53a
b9a53a
diff --git a/src/basic/cpu-set-util.c b/src/basic/cpu-set-util.c
b9a53a
index 5024290557..103b9703b3 100644
b9a53a
--- a/src/basic/cpu-set-util.c
b9a53a
+++ b/src/basic/cpu-set-util.c
b9a53a
@@ -58,7 +58,7 @@ char *cpu_set_to_range_string(const CPUSet *set) {
b9a53a
                         if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(unsigned)))
b9a53a
                                 return NULL;
b9a53a
 
b9a53a
-                        if (range_end > range_start || len == 0)
b9a53a
+                        if (range_end > range_start)
b9a53a
                                 r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end);
b9a53a
                         else
b9a53a
                                 r = sprintf(str + len, len > 0 ? " %d" : "%d", range_start);
b9a53a
@@ -70,7 +70,7 @@ char *cpu_set_to_range_string(const CPUSet *set) {
b9a53a
                 if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(int)))
b9a53a
                         return NULL;
b9a53a
 
b9a53a
-                if (range_end > range_start || len == 0)
b9a53a
+                if (range_end > range_start)
b9a53a
                         r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end);
b9a53a
                 else
b9a53a
                         r = sprintf(str + len, len > 0 ? " %d" : "%d", range_start);
b9a53a
diff --git a/src/test/test-cpu-set-util.c b/src/test/test-cpu-set-util.c
b9a53a
index 995b981d25..9522582891 100644
b9a53a
--- a/src/test/test-cpu-set-util.c
b9a53a
+++ b/src/test/test-cpu-set-util.c
b9a53a
@@ -26,7 +26,7 @@ static void test_parse_cpu_set(void) {
b9a53a
         str = mfree(str);
b9a53a
         assert_se(str = cpu_set_to_range_string(&c);;
b9a53a
         log_info("cpu_set_to_range_string: %s", str);
b9a53a
-        assert_se(streq(str, "0-0"));
b9a53a
+        assert_se(streq(str, "0"));
b9a53a
         str = mfree(str);
b9a53a
         cpu_set_reset(&c);
b9a53a
 
b9a53a
@@ -95,17 +95,19 @@ static void test_parse_cpu_set(void) {
b9a53a
         cpu_set_reset(&c);
b9a53a
 
b9a53a
         /* Commas with spaces (and trailing comma, space) */
b9a53a
-        assert_se(parse_cpu_set_full("0, 1, 2, 3, 4, 5, 6, 7, ", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
b9a53a
+        assert_se(parse_cpu_set_full("0, 1, 2, 3, 4, 5, 6, 7, 63, ", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
b9a53a
         assert_se(c.allocated >= sizeof(__cpu_mask) / 8);
b9a53a
-        assert_se(CPU_COUNT_S(c.allocated, c.set) == 8);
b9a53a
+        assert_se(CPU_COUNT_S(c.allocated, c.set) == 9);
b9a53a
         for (cpu = 0; cpu < 8; cpu++)
b9a53a
                 assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
b9a53a
+
b9a53a
+        assert_se(CPU_ISSET_S(63, c.allocated, c.set));
b9a53a
         assert_se(str = cpu_set_to_string(&c);;
b9a53a
         log_info("cpu_set_to_string: %s", str);
b9a53a
         str = mfree(str);
b9a53a
         assert_se(str = cpu_set_to_range_string(&c);;
b9a53a
         log_info("cpu_set_to_range_string: %s", str);
b9a53a
-        assert_se(streq(str, "0-7"));
b9a53a
+        assert_se(streq(str, "0-7 63"));
b9a53a
         str = mfree(str);
b9a53a
         cpu_set_reset(&c);
b9a53a
 
b9a53a
@@ -161,11 +163,11 @@ static void test_parse_cpu_set(void) {
b9a53a
         cpu_set_reset(&c);
b9a53a
 
b9a53a
         /* Mix ranges and individual CPUs */
b9a53a
-        assert_se(parse_cpu_set_full("0,1 4-11", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
b9a53a
+        assert_se(parse_cpu_set_full("0,2 4-11", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
b9a53a
         assert_se(c.allocated >= sizeof(__cpu_mask) / 8);
b9a53a
         assert_se(CPU_COUNT_S(c.allocated, c.set) == 10);
b9a53a
         assert_se(CPU_ISSET_S(0, c.allocated, c.set));
b9a53a
-        assert_se(CPU_ISSET_S(1, c.allocated, c.set));
b9a53a
+        assert_se(CPU_ISSET_S(2, c.allocated, c.set));
b9a53a
         for (cpu = 4; cpu < 12; cpu++)
b9a53a
                 assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
b9a53a
         assert_se(str = cpu_set_to_string(&c);;
b9a53a
@@ -173,7 +175,7 @@ static void test_parse_cpu_set(void) {
b9a53a
         str = mfree(str);
b9a53a
         assert_se(str = cpu_set_to_range_string(&c);;
b9a53a
         log_info("cpu_set_to_range_string: %s", str);
b9a53a
-        assert_se(streq(str, "0-1 4-11"));
b9a53a
+        assert_se(streq(str, "0 2 4-11"));
b9a53a
         str = mfree(str);
b9a53a
         cpu_set_reset(&c);
b9a53a