]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: qemu: Don't generate any extra commas in virQEMUBuildCommandLineJSON
authorPeter Krempa <pkrempa@redhat.com>
Mon, 25 Jul 2016 12:59:19 +0000 (14:59 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 27 Jul 2016 07:40:12 +0000 (09:40 +0200)
The function would generate a leading comma. Let the callers properly
add commas by formatting the commas at the end and trimming the trailing
one.

src/util/virqemu.c
tests/qemucommandutiltest.c

index 3cc59e75ace8f04f2ce94fa32dbaa154c69d3f6c..8babe36d8de6b5f14d7cbc058ca8a90daf8294fe 100644 (file)
@@ -66,10 +66,10 @@ virQEMUBuildCommandLineJSONArrayBitmap(const char *key,
             end = virBitmapLastSetBit(bitmap) + 1;
 
         if (end - 1 > pos) {
-            virBufferAsprintf(buf, ",%s=%zd-%zd", key, pos, end - 1);
+            virBufferAsprintf(buf, "%s=%zd-%zd,", key, pos, end - 1);
             pos = end;
         } else {
-            virBufferAsprintf(buf, ",%s=%zd", key, pos);
+            virBufferAsprintf(buf, "%s=%zd,", key, pos);
         }
     }
 
@@ -125,19 +125,20 @@ virQEMUBuildCommandLineJSONRecurse(const char *key,
 
     switch ((virJSONType) value->type) {
     case VIR_JSON_TYPE_STRING:
-        virBufferAsprintf(buf, ",%s=", key);
+        virBufferAsprintf(buf, "%s=", key);
         virQEMUBuildBufferEscapeComma(buf, value->data.string);
+        virBufferAddLit(buf, ",");
         break;
 
     case VIR_JSON_TYPE_NUMBER:
-        virBufferAsprintf(buf, ",%s=%s", key, value->data.number);
+        virBufferAsprintf(buf, "%s=%s,", key, value->data.number);
         break;
 
     case VIR_JSON_TYPE_BOOLEAN:
         if (value->data.boolean)
-            virBufferAsprintf(buf, ",%s=yes", key);
+            virBufferAsprintf(buf, "%s=yes,", key);
         else
-            virBufferAsprintf(buf, ",%s=no", key);
+            virBufferAsprintf(buf, "%s=no,", key);
 
         break;
 
@@ -196,7 +197,12 @@ virQEMUBuildCommandLineJSON(const virJSONValue *value,
                             virBufferPtr buf,
                             virQEMUBuildCommandLineJSONArrayFormatFunc array)
 {
-    return virQEMUBuildCommandLineJSONRecurse(NULL, value, buf, array, false);
+    if (virQEMUBuildCommandLineJSONRecurse(NULL, value, buf, array, false) < 0)
+        return -1;
+
+    virBufferTrim(buf, ",", -1);
+
+    return 0;
 }
 
 
@@ -208,7 +214,7 @@ virQEMUBuildObjectCommandlineFromJSON(const char *type,
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     char *ret = NULL;
 
-    virBufferAsprintf(&buf, "%s,id=%s", type, alias);
+    virBufferAsprintf(&buf, "%s,id=%s,", type, alias);
 
     if (virQEMUBuildCommandLineJSON(props, &buf,
                                     virQEMUBuildCommandLineJSONArrayBitmap) < 0)
index a5e315347fa872c77c354d353fb3976066dbf392..0bf0351fc9ed620db401eeb15ae691bb7c5196f4 100644 (file)
@@ -37,7 +37,6 @@ testQemuCommandBuildFromJSON(const void *opaque)
 {
     const testQemuCommandBuildObjectFromJSONData *data = opaque;
     virJSONValuePtr val = NULL;
-    char *expect = NULL;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     char *result = NULL;
     int ret = -1;
@@ -47,10 +46,6 @@ testQemuCommandBuildFromJSON(const void *opaque)
         return -1;
     }
 
-    if (data->expectprops &&
-        virAsprintf(&expect, ",%s", data->expectprops) < 0)
-        return -1;
-
     if (virQEMUBuildCommandLineJSON(val, &buf,
                                     virQEMUBuildCommandLineJSONArrayBitmap) < 0) {
         fprintf(stderr,
@@ -61,10 +56,10 @@ testQemuCommandBuildFromJSON(const void *opaque)
 
     result = virBufferContentAndReset(&buf);
 
-    if (STRNEQ_NULLABLE(expect, result)) {
+    if (STRNEQ_NULLABLE(data->expectprops, result)) {
         fprintf(stderr, "\nFailed to create object string. "
                 "\nExpected:\n'%s'\nGot:\n'%s'",
-                NULLSTR(expect), NULLSTR(result));
+                NULLSTR(data->expectprops), NULLSTR(result));
         goto cleanup;
     }
 
@@ -72,7 +67,6 @@ testQemuCommandBuildFromJSON(const void *opaque)
  cleanup:
     virJSONValueFree(val);
     VIR_FREE(result);
-    VIR_FREE(expect);
     return ret;
 }