[libvirt] [PATCHv2 2/3] qemu_monitor_json: Build Json CPU Model Info
John Ferlan
jferlan at redhat.com
Fri Apr 27 18:41:01 UTC 2018
$SUBJ:
qemu_monitor_json: Introduce qemuMonitorJSONBuildCPUModelInfoToJSON
The corollary for qemuMonitorJSONBuildCPUModelInfoFromJSON is to build the JSON data from the qemuMonitorCPUModelInfoPtr.
On 04/19/2018 12:06 AM, Chris Venteicher wrote:
> Function qemuMonitorJSONBuildCPUModelInfoToJSON
> builds and returns JSON of form
> {"model": {"name": "IvyBridge", "props": {}}}
> from qemuMonitorCPUModelInfo.
>
> Function qemuMonitorJSONBuildCPUModelInfoToJSON
> returns virJsonValuePtr on success and NULL on failure.
> ---
> src/qemu/qemu_monitor_json.c | 48 ++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 48 insertions(+)
>
> diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
> index 4368aaaa0..44c1b2f15 100644
> --- a/src/qemu/qemu_monitor_json.c
> +++ b/src/qemu/qemu_monitor_json.c
> @@ -5337,6 +5337,54 @@ qemuMonitorJSONParseCPUModelProperty(const char *key,
> return 0;
> }
>
Again 2 blank lines (at end too).
> +/* model_json: {"model": {"name": "IvyBridge", "props": {}}}
> + */
> +static virJSONValuePtr
> +qemuMonitorJSONBuildCPUModelInfoToJSON(qemuMonitorCPUModelInfoPtr model)
> +{
> + virJSONValuePtr cpu_props = NULL;
> + virJSONValuePtr model_json = NULL;
> + size_t i;
> +
> + if (!(cpu_props = virJSONValueNewObject()))
> + goto cleanup;
> +
> + for (i = 0; i < model->nprops; i++) {
> + qemuMonitorCPUPropertyPtr prop = model->props + i;
or prop = model->props[i] - something I'm more used to seeing.
> +
> + switch (prop->type) {
> + case QEMU_MONITOR_CPU_PROPERTY_BOOLEAN:
> + if (virJSONValueObjectAppendBoolean(cpu_props, prop->name, prop->value.boolean) < 0)
Long line - consider creating line break after prop->name
Prefer to keep lines < 80 if possible unless it's just a
character or two.
> + goto cleanup;
> + break;
> +
> + case QEMU_MONITOR_CPU_PROPERTY_STRING:
> + if (virJSONValueObjectAppendString(cpu_props, prop->name, prop->value.string) < 0)
> + goto cleanup;
> + break;
> +
> + case QEMU_MONITOR_CPU_PROPERTY_NUMBER:
> + if (virJSONValueObjectAppendNumberLong(cpu_props, prop->name, prop->value.number) < 0)
> + goto cleanup;
> + break;
> +
> + case QEMU_MONITOR_CPU_PROPERTY_LAST:
> + default:
> + virReportEnumRangeError(qemuMonitorCPUPropertyPtr, prop->type);
> + goto cleanup;
> + }
> + }
> +
> + if (virJSONValueObjectCreate(&model_json, "s:name", model->name,
> + "a:props", &cpu_props, NULL) < 0) {
> + virJSONValueFree(cpu_props);
> + goto cleanup;
> + }
I just know the above will cause Coverity to get all upset, but I think
if you go with
ignore_value(virJSONValueObjectCreate(&model_json, "s:name", model->name,
"a:props", &cpu_props, NULL));
cleanup:
virJSONValueFree(cpu_props);
return model_json;
Then things will be fine. In the long run since @model_json is the returned
thing and it'd only be non NULL if virJSONValueObjectCreate succeeded and it
woudl consume @cpu_props properly, thus we don't care about the return value
because it's not changing what we do. But not having that @cpu_props Free
makes coverity believe it's leaked even though it's passed by reference.
John
> +
> + cleanup:
> + return model_json;
> +}
> +
> /* model_json: {"model": {"name": "IvyBridge", "props": {}}}
> */
> static qemuMonitorCPUModelInfoPtr
>
More information about the libvir-list
mailing list