[libvirt] [PATCH] qemu_monitor_json: Properly check "return" type
Peter Krempa
pkrempa at redhat.com
Thu Mar 29 18:02:54 UTC 2018
On Thu, Mar 29, 2018 at 16:35:59 +0200, Jiri Denemark wrote:
> My commit 2e0d6cdec41 claimed qemuMonitorJSONCheckError guarantees
> "return" object exists in the JSON reply. But it only makes sure the key
> is there, while the type of the value is not checked. A lot of callers
> do not care since they only want to see whether their QMP command failed
> or not, but any caller which needs to read some data from the reply
> wants to make sure the correct data type was returned.
>
> This patch adds a new API called qemuMonitorJSONCheckReply which calls
> qemuMonitorJSONCheckError and checks "return" contains a value of the
> specified type.
>
> Signed-off-by: Jiri Denemark <jdenemar at redhat.com>
> ---
> src/qemu/qemu_monitor_json.c | 261 ++++++++++++++++++-------------------------
> 1 file changed, 108 insertions(+), 153 deletions(-)
>
> diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
> index d80c4f18d1..b251fc9964 100644
> --- a/src/qemu/qemu_monitor_json.c
> +++ b/src/qemu/qemu_monitor_json.c
> @@ -413,6 +413,36 @@ qemuMonitorJSONCheckError(virJSONValuePtr cmd,
> }
>
>
> +static int
> +qemuMonitorJSONCheckReply(virJSONValuePtr cmd,
> + virJSONValuePtr reply,
> + virJSONType type)
> +{
> + virJSONValuePtr data;
> +
> + if (qemuMonitorJSONCheckError(cmd, reply) < 0)
> + return -1;
> +
> + data = virJSONValueObjectGet(reply, "return");
> + if (data->type != type) {
Please use virJSONValueGetType instead of direct access.
> + char *cmdstr = virJSONValueToString(cmd, false);
> + char *retstr = virJSONValueToString(data, false);
> +
> + VIR_DEBUG("Unexpected return type %d (expecting %d) for command %s: %s",
> + data->type, type, cmdstr, retstr);
Same here.
> + virReportError(VIR_ERR_INTERNAL_ERROR,
> + _("unexpected type returned by QEMU command '%s'"),
> + qemuMonitorJSONCommandName(cmd));
> +
ACK
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20180329/14d176fa/attachment-0001.sig>
More information about the libvir-list
mailing list