From: Marc-André Lureau Date: Thu, 19 Jul 2018 18:40:59 +0000 (+0200) Subject: qga: process_event() simplification and leak fix X-Git-Tag: v2.12.1~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bf1cb819e983494f2606f9d7ff0726c210a6c757;p=thirdparty%2Fqemu.git qga: process_event() simplification and leak fix json_parser_parse_err() may return something else than a QDict, in which case we loose the object. Let's keep track of the original object to avoid leaks. When an error occurs, "qdict" contains the response, but we still check the "execute" key there. Untangle a bit this code, by having a clear error path. CC: Michael Roth Signed-off-by: Marc-André Lureau Reviewed-by: Markus Armbruster Cc: qemu-stable@nongnu.org Signed-off-by: Michael Roth (cherry picked from commit ae7da1e5f658ea21d96e565514de20ff2cf24fa1) * drop context dep on d43b16945a * drop functional dep on cb3e7f08ae Signed-off-by: Michael Roth --- diff --git a/qga/main.c b/qga/main.c index 74765e32b92..9241eb3229d 100644 --- a/qga/main.c +++ b/qga/main.c @@ -600,42 +600,42 @@ static void process_command(GAState *s, QDict *req) static void process_event(JSONMessageParser *parser, GQueue *tokens) { GAState *s = container_of(parser, GAState, parser); - QDict *qdict; + QObject *obj; + QDict *req, *rsp; Error *err = NULL; int ret; g_assert(s && parser); g_debug("process_event: called"); - qdict = qobject_to(QDict, json_parser_parse_err(tokens, NULL, &err)); - if (err || !qdict) { - QDECREF(qdict); - if (!err) { - g_warning("failed to parse event: unknown error"); - error_setg(&err, QERR_JSON_PARSING); - } else { - g_warning("failed to parse event: %s", error_get_pretty(err)); - } - qdict = qmp_error_response(err); + obj = json_parser_parse_err(tokens, NULL, &err); + if (err) { + goto err; } - - /* handle host->guest commands */ - if (qdict_haskey(qdict, "execute")) { - process_command(s, qdict); - } else { - if (!qdict_haskey(qdict, "error")) { - QDECREF(qdict); - g_warning("unrecognized payload format"); - error_setg(&err, QERR_UNSUPPORTED); - qdict = qmp_error_response(err); - } - ret = send_response(s, QOBJECT(qdict)); - if (ret < 0) { - g_warning("error sending error response: %s", strerror(-ret)); - } + req = qobject_to(QDict, obj); + if (!req) { + error_setg(&err, QERR_JSON_PARSING); + goto err; + } + if (!qdict_haskey(req, "execute")) { + g_warning("unrecognized payload format"); + error_setg(&err, QERR_UNSUPPORTED); + goto err; } - QDECREF(qdict); + process_command(s, req); + qobject_decref(obj); + return; + +err: + g_warning("failed to parse event: %s", error_get_pretty(err)); + rsp = qmp_error_response(err); + ret = send_response(s, QOBJECT(rsp)); + if (ret < 0) { + g_warning("error sending error response: %s", strerror(-ret)); + } + QDECREF(rsp); + qobject_decref(obj); } /* false return signals GAChannel to close the current client connection */