render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
6ae9ed
From b268c983e92680e9083c9e8d75e64e6215dff7b9 Mon Sep 17 00:00:00 2001
6ae9ed
Message-Id: <b268c983e92680e9083c9e8d75e64e6215dff7b9@dist-git>
6ae9ed
From: Peter Krempa <pkrempa@redhat.com>
6ae9ed
Date: Tue, 2 Aug 2016 13:41:46 +0200
6ae9ed
Subject: [PATCH] util: qemu: Don't generate any extra commas in
6ae9ed
 virQEMUBuildCommandLineJSON
6ae9ed
6ae9ed
The function would generate a leading comma. Let the callers properly
6ae9ed
add commas by formatting the commas at the end and trimming the trailing
6ae9ed
one.
6ae9ed
6ae9ed
(cherry picked from commit ca620e35ea75dc07f9415c384e240cc2adea4cb4)
6ae9ed
https://bugzilla.redhat.com/show_bug.cgi?id=1134878 [JSON backing]
6ae9ed
https://bugzilla.redhat.com/show_bug.cgi?id=1247521 [gluster multi-host]
6ae9ed
---
6ae9ed
 src/util/virqemu.c          | 22 ++++++++++++++--------
6ae9ed
 tests/qemucommandutiltest.c | 10 ++--------
6ae9ed
 2 files changed, 16 insertions(+), 16 deletions(-)
6ae9ed
6ae9ed
diff --git a/src/util/virqemu.c b/src/util/virqemu.c
6ae9ed
index 3cc59e7..8babe36 100644
6ae9ed
--- a/src/util/virqemu.c
6ae9ed
+++ b/src/util/virqemu.c
6ae9ed
@@ -66,10 +66,10 @@ virQEMUBuildCommandLineJSONArrayBitmap(const char *key,
6ae9ed
             end = virBitmapLastSetBit(bitmap) + 1;
6ae9ed
 
6ae9ed
         if (end - 1 > pos) {
6ae9ed
-            virBufferAsprintf(buf, ",%s=%zd-%zd", key, pos, end - 1);
6ae9ed
+            virBufferAsprintf(buf, "%s=%zd-%zd,", key, pos, end - 1);
6ae9ed
             pos = end;
6ae9ed
         } else {
6ae9ed
-            virBufferAsprintf(buf, ",%s=%zd", key, pos);
6ae9ed
+            virBufferAsprintf(buf, "%s=%zd,", key, pos);
6ae9ed
         }
6ae9ed
     }
6ae9ed
 
6ae9ed
@@ -125,19 +125,20 @@ virQEMUBuildCommandLineJSONRecurse(const char *key,
6ae9ed
 
6ae9ed
     switch ((virJSONType) value->type) {
6ae9ed
     case VIR_JSON_TYPE_STRING:
6ae9ed
-        virBufferAsprintf(buf, ",%s=", key);
6ae9ed
+        virBufferAsprintf(buf, "%s=", key);
6ae9ed
         virQEMUBuildBufferEscapeComma(buf, value->data.string);
6ae9ed
+        virBufferAddLit(buf, ",");
6ae9ed
         break;
6ae9ed
 
6ae9ed
     case VIR_JSON_TYPE_NUMBER:
6ae9ed
-        virBufferAsprintf(buf, ",%s=%s", key, value->data.number);
6ae9ed
+        virBufferAsprintf(buf, "%s=%s,", key, value->data.number);
6ae9ed
         break;
6ae9ed
 
6ae9ed
     case VIR_JSON_TYPE_BOOLEAN:
6ae9ed
         if (value->data.boolean)
6ae9ed
-            virBufferAsprintf(buf, ",%s=yes", key);
6ae9ed
+            virBufferAsprintf(buf, "%s=yes,", key);
6ae9ed
         else
6ae9ed
-            virBufferAsprintf(buf, ",%s=no", key);
6ae9ed
+            virBufferAsprintf(buf, "%s=no,", key);
6ae9ed
 
6ae9ed
         break;
6ae9ed
 
6ae9ed
@@ -196,7 +197,12 @@ virQEMUBuildCommandLineJSON(const virJSONValue *value,
6ae9ed
                             virBufferPtr buf,
6ae9ed
                             virQEMUBuildCommandLineJSONArrayFormatFunc array)
6ae9ed
 {
6ae9ed
-    return virQEMUBuildCommandLineJSONRecurse(NULL, value, buf, array, false);
6ae9ed
+    if (virQEMUBuildCommandLineJSONRecurse(NULL, value, buf, array, false) < 0)
6ae9ed
+        return -1;
6ae9ed
+
6ae9ed
+    virBufferTrim(buf, ",", -1);
6ae9ed
+
6ae9ed
+    return 0;
6ae9ed
 }
6ae9ed
 
6ae9ed
 
6ae9ed
@@ -208,7 +214,7 @@ virQEMUBuildObjectCommandlineFromJSON(const char *type,
6ae9ed
     virBuffer buf = VIR_BUFFER_INITIALIZER;
6ae9ed
     char *ret = NULL;
6ae9ed
 
6ae9ed
-    virBufferAsprintf(&buf, "%s,id=%s", type, alias);
6ae9ed
+    virBufferAsprintf(&buf, "%s,id=%s,", type, alias);
6ae9ed
 
6ae9ed
     if (virQEMUBuildCommandLineJSON(props, &buf,
6ae9ed
                                     virQEMUBuildCommandLineJSONArrayBitmap) < 0)
6ae9ed
diff --git a/tests/qemucommandutiltest.c b/tests/qemucommandutiltest.c
6ae9ed
index a5e3153..0bf0351 100644
6ae9ed
--- a/tests/qemucommandutiltest.c
6ae9ed
+++ b/tests/qemucommandutiltest.c
6ae9ed
@@ -37,7 +37,6 @@ testQemuCommandBuildFromJSON(const void *opaque)
6ae9ed
 {
6ae9ed
     const testQemuCommandBuildObjectFromJSONData *data = opaque;
6ae9ed
     virJSONValuePtr val = NULL;
6ae9ed
-    char *expect = NULL;
6ae9ed
     virBuffer buf = VIR_BUFFER_INITIALIZER;
6ae9ed
     char *result = NULL;
6ae9ed
     int ret = -1;
6ae9ed
@@ -47,10 +46,6 @@ testQemuCommandBuildFromJSON(const void *opaque)
6ae9ed
         return -1;
6ae9ed
     }
6ae9ed
 
6ae9ed
-    if (data->expectprops &&
6ae9ed
-        virAsprintf(&expect, ",%s", data->expectprops) < 0)
6ae9ed
-        return -1;
6ae9ed
-
6ae9ed
     if (virQEMUBuildCommandLineJSON(val, &buf,
6ae9ed
                                     virQEMUBuildCommandLineJSONArrayBitmap) < 0) {
6ae9ed
         fprintf(stderr,
6ae9ed
@@ -61,10 +56,10 @@ testQemuCommandBuildFromJSON(const void *opaque)
6ae9ed
 
6ae9ed
     result = virBufferContentAndReset(&buf;;
6ae9ed
 
6ae9ed
-    if (STRNEQ_NULLABLE(expect, result)) {
6ae9ed
+    if (STRNEQ_NULLABLE(data->expectprops, result)) {
6ae9ed
         fprintf(stderr, "\nFailed to create object string. "
6ae9ed
                 "\nExpected:\n'%s'\nGot:\n'%s'",
6ae9ed
-                NULLSTR(expect), NULLSTR(result));
6ae9ed
+                NULLSTR(data->expectprops), NULLSTR(result));
6ae9ed
         goto cleanup;
6ae9ed
     }
6ae9ed
 
6ae9ed
@@ -72,7 +67,6 @@ testQemuCommandBuildFromJSON(const void *opaque)
6ae9ed
  cleanup:
6ae9ed
     virJSONValueFree(val);
6ae9ed
     VIR_FREE(result);
6ae9ed
-    VIR_FREE(expect);
6ae9ed
     return ret;
6ae9ed
 }
6ae9ed
 
6ae9ed
-- 
6ae9ed
2.9.2
6ae9ed