From 35894625624f0e8c7d3ca2c200861005c7ad4435 Mon Sep 17 00:00:00 2001
From: Michal Sekletar <msekleta@redhat.com>
Date: Mon, 3 Jun 2019 10:12:35 +0200
Subject: [PATCH] cpu-set-util: use %d-%d format in  cpu_set_to_range_string()
 only for actual ranges

(cherry picked from commit 71923237b18df35401626993d8a285cd998be78d)

Related: #1734787
---
 src/basic/cpu-set-util.c     |  4 ++--
 src/test/test-cpu-set-util.c | 16 +++++++++-------
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/src/basic/cpu-set-util.c b/src/basic/cpu-set-util.c
index 5024290557..103b9703b3 100644
--- a/src/basic/cpu-set-util.c
+++ b/src/basic/cpu-set-util.c
@@ -58,7 +58,7 @@ char *cpu_set_to_range_string(const CPUSet *set) {
                         if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(unsigned)))
                                 return NULL;
 
-                        if (range_end > range_start || len == 0)
+                        if (range_end > range_start)
                                 r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end);
                         else
                                 r = sprintf(str + len, len > 0 ? " %d" : "%d", range_start);
@@ -70,7 +70,7 @@ char *cpu_set_to_range_string(const CPUSet *set) {
                 if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(int)))
                         return NULL;
 
-                if (range_end > range_start || len == 0)
+                if (range_end > range_start)
                         r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end);
                 else
                         r = sprintf(str + len, len > 0 ? " %d" : "%d", range_start);
diff --git a/src/test/test-cpu-set-util.c b/src/test/test-cpu-set-util.c
index 995b981d25..9522582891 100644
--- a/src/test/test-cpu-set-util.c
+++ b/src/test/test-cpu-set-util.c
@@ -26,7 +26,7 @@ static void test_parse_cpu_set(void) {
         str = mfree(str);
         assert_se(str = cpu_set_to_range_string(&c));
         log_info("cpu_set_to_range_string: %s", str);
-        assert_se(streq(str, "0-0"));
+        assert_se(streq(str, "0"));
         str = mfree(str);
         cpu_set_reset(&c);
 
@@ -95,17 +95,19 @@ static void test_parse_cpu_set(void) {
         cpu_set_reset(&c);
 
         /* Commas with spaces (and trailing comma, space) */
-        assert_se(parse_cpu_set_full("0, 1, 2, 3, 4, 5, 6, 7, ", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
+        assert_se(parse_cpu_set_full("0, 1, 2, 3, 4, 5, 6, 7, 63, ", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
         assert_se(c.allocated >= sizeof(__cpu_mask) / 8);
-        assert_se(CPU_COUNT_S(c.allocated, c.set) == 8);
+        assert_se(CPU_COUNT_S(c.allocated, c.set) == 9);
         for (cpu = 0; cpu < 8; cpu++)
                 assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
+
+        assert_se(CPU_ISSET_S(63, c.allocated, c.set));
         assert_se(str = cpu_set_to_string(&c));
         log_info("cpu_set_to_string: %s", str);
         str = mfree(str);
         assert_se(str = cpu_set_to_range_string(&c));
         log_info("cpu_set_to_range_string: %s", str);
-        assert_se(streq(str, "0-7"));
+        assert_se(streq(str, "0-7 63"));
         str = mfree(str);
         cpu_set_reset(&c);
 
@@ -161,11 +163,11 @@ static void test_parse_cpu_set(void) {
         cpu_set_reset(&c);
 
         /* Mix ranges and individual CPUs */
-        assert_se(parse_cpu_set_full("0,1 4-11", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
+        assert_se(parse_cpu_set_full("0,2 4-11", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
         assert_se(c.allocated >= sizeof(__cpu_mask) / 8);
         assert_se(CPU_COUNT_S(c.allocated, c.set) == 10);
         assert_se(CPU_ISSET_S(0, c.allocated, c.set));
-        assert_se(CPU_ISSET_S(1, c.allocated, c.set));
+        assert_se(CPU_ISSET_S(2, c.allocated, c.set));
         for (cpu = 4; cpu < 12; cpu++)
                 assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
         assert_se(str = cpu_set_to_string(&c));
@@ -173,7 +175,7 @@ static void test_parse_cpu_set(void) {
         str = mfree(str);
         assert_se(str = cpu_set_to_range_string(&c));
         log_info("cpu_set_to_range_string: %s", str);
-        assert_se(streq(str, "0-1 4-11"));
+        assert_se(streq(str, "0 2 4-11"));
         str = mfree(str);
         cpu_set_reset(&c);
 
