diff mbox series

[09/20] qobject: qobject_from_jsonv() is dangerous, hide it away

Message ID 20180712111221.20326-10-armbru@redhat.com
State New
Headers show
Series tests: Compile-time format string checking for libqtest.h | expand

Commit Message

Markus Armbruster July 12, 2018, 11:12 a.m. UTC
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 <armbru@redhat.com>
---
 include/qapi/qmp/qjson.h |  2 --
 qobject/qjson.c          | 13 ++++++++++++-
 2 files changed, 12 insertions(+), 3 deletions(-)

Comments

Philippe Mathieu-Daudé July 12, 2018, 2:09 p.m. UTC | #1
On 07/12/2018 08:12 AM, Markus Armbruster wrote:
> 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 <armbru@redhat.com>

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>

> ---
>  include/qapi/qmp/qjson.h |  2 --
>  qobject/qjson.c          | 13 ++++++++++++-
>  2 files changed, 12 insertions(+), 3 deletions(-)
> 
> diff --git a/include/qapi/qmp/qjson.h b/include/qapi/qmp/qjson.h
> index 0db121bef9..4a225cf7d1 100644
> --- a/include/qapi/qmp/qjson.h
> +++ b/include/qapi/qmp/qjson.h
> @@ -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)
>      GCC_FMT_ATTR(1, 0);
> diff --git a/qobject/qjson.c b/qobject/qjson.c
> index 2e450231ff..ab4040f235 100644
> --- a/qobject/qjson.c
> +++ b/qobject/qjson.c
> @@ -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 = {};
>  
>
diff mbox series

Patch

diff --git a/include/qapi/qmp/qjson.h b/include/qapi/qmp/qjson.h
index 0db121bef9..4a225cf7d1 100644
--- a/include/qapi/qmp/qjson.h
+++ b/include/qapi/qmp/qjson.h
@@ -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)
     GCC_FMT_ATTR(1, 0);
diff --git a/qobject/qjson.c b/qobject/qjson.c
index 2e450231ff..ab4040f235 100644
--- a/qobject/qjson.c
+++ b/qobject/qjson.c
@@ -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 = {};