Skip to content

Commit

Permalink
qobject: qobject_from_jsonv() is dangerous, hide it away
Browse files Browse the repository at this point in the history
qobject_from_jsonv() takes ownership of %p arguments.  On failure, we
can't generally know whether we failed before or after %p, so
ownership becomes indeterminate.  To avoid leaks, callers passing %p
must terminate on error, e.g. by passing &error_abort.  Trap for the
unwary; document and give the function internal linkage.

Signed-off-by: Markus Armbruster <[email protected]>
Reviewed-by: Philippe Mathieu-Daudé <[email protected]>
Reviewed-by: Eric Blake <[email protected]>
Message-Id: <[email protected]>
  • Loading branch information
Markus Armbruster committed Aug 16, 2018
1 parent eac78bd commit 2d36e84
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 0 additions & 2 deletions include/qapi/qmp/qjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#define QJSON_H

QObject *qobject_from_json(const char *string, Error **errp);
QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp)
GCC_FMT_ATTR(1, 0);

QObject *qobject_from_vjsonf_nofail(const char *string, va_list ap)
GCC_FMT_ATTR(1, 0);
Expand Down
13 changes: 12 additions & 1 deletion qobject/qjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,18 @@ static void parse_json(JSONMessageParser *parser, GQueue *tokens)
s->result = json_parser_parse_err(tokens, s->ap, &s->err);
}

QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp)
/*
* Parse @string as JSON value.
* If @ap is non-null, interpolate %-escapes.
* Takes ownership of %p arguments.
* On success, return the JSON value.
* On failure, store an error through @errp and return NULL.
* Ownership of %p arguments becomes indeterminate then. To avoid
* leaks, callers passing %p must terminate on error, e.g. by passing
* &error_abort.
*/
static QObject *qobject_from_jsonv(const char *string, va_list *ap,
Error **errp)
{
JSONParsingState state = {};

Expand Down

0 comments on commit 2d36e84

Please sign in to comment.