From b268c983e92680e9083c9e8d75e64e6215dff7b9 Mon Sep 17 00:00:00 2001 Message-Id: From: Peter Krempa Date: Tue, 2 Aug 2016 13:41:46 +0200 Subject: [PATCH] util: qemu: Don't generate any extra commas in virQEMUBuildCommandLineJSON 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. (cherry picked from commit ca620e35ea75dc07f9415c384e240cc2adea4cb4) https://bugzilla.redhat.com/show_bug.cgi?id=1134878 [JSON backing] https://bugzilla.redhat.com/show_bug.cgi?id=1247521 [gluster multi-host] --- src/util/virqemu.c | 22 ++++++++++++++-------- tests/qemucommandutiltest.c | 10 ++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/util/virqemu.c b/src/util/virqemu.c index 3cc59e7..8babe36 100644 --- a/src/util/virqemu.c +++ b/src/util/virqemu.c @@ -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) diff --git a/tests/qemucommandutiltest.c b/tests/qemucommandutiltest.c index a5e3153..0bf0351 100644 --- a/tests/qemucommandutiltest.c +++ b/tests/qemucommandutiltest.c @@ -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; } -- 2.9.2