提交 ca620e35 编写于 作者: P Peter Krempa

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.
上级 b7eef33d
......@@ -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)
......
......@@ -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;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册