diff mbox

[v9,33/37] qapi: Split visit_end_struct() into pieces

Message ID 1453219845-30939-34-git-send-email-eblake@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Eric Blake Jan. 19, 2016, 4:10 p.m. UTC
As mentioned in previous patches, we want to call visit_end_struct()
functions unconditionally, so that visitors can release resources
tied up since the matching visit_start_struct() without also having
to worry about error priority if more than one error occurs.

Even though error_propagate() can be safely used to ignore a second
error during cleanup caused by a first error, it is simpler if the
cleanup cannot set an error, and we instead split the task of
checking that an input visitor has no unvisited input as a new
function visit_check_struct(), called only if all prior steps are
successful.

Generated code has diffs resembling:

|@@ -59,10 +59,12 @@ void visit_type_ACPIOSTInfo(Visitor *v,
|         goto out_obj;
|     }
|     visit_type_ACPIOSTInfo_fields(v, obj, &err);
|+    if (err) {
|+        goto out_obj;
|+    }
|+    visit_check_struct(v, &err);
| out_obj:
|-    error_propagate(errp, err);
|-    err = NULL;
|-    visit_end_struct(v, &err);
|+    visit_end_struct(v);
| out:

Signed-off-by: Eric Blake <eblake@redhat.com>

---
v9: rebase to earlier changes, drop Marc-Andre's R-b
v8: rebase to 'name' motion
v7: rebase to earlier changes
v6: new patch, revised version of RFC based on discussion of v5 7/46
---
 hmp.c                          |  7 ++++---
 hw/ppc/spapr_drc.c             |  3 ++-
 hw/virtio/virtio-balloon.c     | 12 ++++++------
 include/qapi/visitor-impl.h    |  4 +++-
 include/qapi/visitor.h         | 13 ++++++++++---
 qapi/opts-visitor.c            | 17 +++++++++++++++--
 qapi/qapi-dealloc-visitor.c    |  2 +-
 qapi/qapi-visit-core.c         | 11 +++++++++--
 qapi/qmp-input-visitor.c       | 34 +++++++++++++++++++---------------
 qapi/qmp-output-visitor.c      |  2 +-
 qom/object.c                   |  5 ++---
 scripts/qapi-event.py          |  3 ++-
 scripts/qapi-visit.py          |  9 ++++-----
 tests/test-qmp-input-visitor.c |  3 ++-
 vl.c                           | 11 +++++------
 15 files changed, 85 insertions(+), 51 deletions(-)

Comments

Markus Armbruster Jan. 27, 2016, 5:20 p.m. UTC | #1
Eric Blake <eblake@redhat.com> writes:

> As mentioned in previous patches, we want to call visit_end_struct()
> functions unconditionally, so that visitors can release resources
> tied up since the matching visit_start_struct() without also having
> to worry about error priority if more than one error occurs.
>
> Even though error_propagate() can be safely used to ignore a second
> error during cleanup caused by a first error, it is simpler if the
> cleanup cannot set an error, and we instead split the task of
> checking that an input visitor has no unvisited input as a new
> function visit_check_struct(), called only if all prior steps are
> successful.
>
> Generated code has diffs resembling:
>
> |@@ -59,10 +59,12 @@ void visit_type_ACPIOSTInfo(Visitor *v,
> |         goto out_obj;
> |     }
> |     visit_type_ACPIOSTInfo_fields(v, obj, &err);
> |+    if (err) {
> |+        goto out_obj;
> |+    }
> |+    visit_check_struct(v, &err);
> | out_obj:
> |-    error_propagate(errp, err);
> |-    err = NULL;
> |-    visit_end_struct(v, &err);
> |+    visit_end_struct(v);
> | out:

Basically, split visit_end_struct() into a part that runs only when
there have been no errors and a part that runs as before, i.e. when
visit_start_struct() succeeded, even when there have been errors since.
This second part cannot fail, which simplifies error handling a bit.

To review: skipping the first part when there have been errors is safe.

>
> Signed-off-by: Eric Blake <eblake@redhat.com>
>
> ---
> v9: rebase to earlier changes, drop Marc-Andre's R-b
> v8: rebase to 'name' motion
> v7: rebase to earlier changes
> v6: new patch, revised version of RFC based on discussion of v5 7/46
> ---
>  hmp.c                          |  7 ++++---
>  hw/ppc/spapr_drc.c             |  3 ++-
>  hw/virtio/virtio-balloon.c     | 12 ++++++------
>  include/qapi/visitor-impl.h    |  4 +++-
>  include/qapi/visitor.h         | 13 ++++++++++---
>  qapi/opts-visitor.c            | 17 +++++++++++++++--
>  qapi/qapi-dealloc-visitor.c    |  2 +-
>  qapi/qapi-visit-core.c         | 11 +++++++++--
>  qapi/qmp-input-visitor.c       | 34 +++++++++++++++++++---------------
>  qapi/qmp-output-visitor.c      |  2 +-
>  qom/object.c                   |  5 ++---
>  scripts/qapi-event.py          |  3 ++-
>  scripts/qapi-visit.py          |  9 ++++-----
>  tests/test-qmp-input-visitor.c |  3 ++-
>  vl.c                           | 11 +++++------
>  15 files changed, 85 insertions(+), 51 deletions(-)
>
> diff --git a/hmp.c b/hmp.c
> index a4b74df..245617d 100644
> --- a/hmp.c
> +++ b/hmp.c
> @@ -1687,13 +1687,14 @@ void hmp_object_add(Monitor *mon, const QDict *qdict)
>      }
>
>      object_add(type, id, pdict, v, &err);
> -
> -out_end:
> -    visit_end_struct(v, &err_end);
> +    visit_check_struct(v, &err_end);
>      if (!err && err_end) {
>          qmp_object_del(id, NULL);
>      }
>      error_propagate(&err, err_end);
> +
> +out_end:
> +    visit_end_struct(v);
>  out_clean:
>      opts_visitor_cleanup(ov);
>
> diff --git a/hw/ppc/spapr_drc.c b/hw/ppc/spapr_drc.c
> index 831ce23..3b27caa 100644
> --- a/hw/ppc/spapr_drc.c
> +++ b/hw/ppc/spapr_drc.c
> @@ -287,11 +287,12 @@ static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
>          case FDT_END_NODE:
>              /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
>              g_assert(fdt_depth > 0);
> -            visit_end_struct(v, &err);
> +            visit_check_struct(v, &err);
>              if (err) {
>                  error_propagate(errp, err);
>                  return;
>              }
> +            visit_end_struct(v);
>              fdt_depth--;
>              break;
>          case FDT_PROP: {

No longer calls visit_end_struct() when visit_end_struct() fails.
Contradicts the rule to always call it after visit_start_struct()
succeded.

> diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c
> index 24ecd87..cefd43f 100644
> --- a/hw/virtio/virtio-balloon.c
> +++ b/hw/virtio/virtio-balloon.c
> @@ -136,15 +136,15 @@ static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name,
>              goto out_nested;
>          }
>      }
> +    visit_check_struct(v, &err);
>  out_nested:
> -    error_propagate(errp, err);
> -    err = NULL;
> -    visit_end_struct(v, &err);
> +    visit_end_struct(v);
>
> +    if (!err) {
> +        visit_check_struct(v, &err);
> +    }
>  out_end:
> -    error_propagate(errp, err);
> -    err = NULL;
> -    visit_end_struct(v, &err);
> +    visit_end_struct(v);
>  out:
>      error_propagate(errp, err);
>  }
> diff --git a/include/qapi/visitor-impl.h b/include/qapi/visitor-impl.h
> index 3b68b7b..248b1e5 100644
> --- a/include/qapi/visitor-impl.h
> +++ b/include/qapi/visitor-impl.h
> @@ -28,8 +28,10 @@ struct Visitor
>       * currently visit structs). */
>      void (*start_struct)(Visitor *v, const char *name, void **obj,
>                           size_t size, Error **errp);
> +    /* May be NULL; most useful for input visitors. */
> +    void (*check_struct)(Visitor *v, Error **errp);

Well, "most useful" is certainly correct since other visitors have had
no use for it so far :)

"intended for input visitors"?

>      /* Must be provided if start_struct is present. */
> -    void (*end_struct)(Visitor *v, Error **errp);
> +    void (*end_struct)(Visitor *v);
>
>      /* May be NULL; most useful for input visitors. */
>      void (*start_implicit_struct)(Visitor *v, void **obj, size_t size,
> diff --git a/include/qapi/visitor.h b/include/qapi/visitor.h
> index d7a0110..e5dcde4 100644
> --- a/include/qapi/visitor.h
> +++ b/include/qapi/visitor.h
> @@ -68,12 +68,19 @@ typedef struct GenericList
>  void visit_start_struct(Visitor *v, const char *name, void **obj,
>                          size_t size, Error **errp);
>  /**
> + * Prepare for completing a struct visit.
> + * Should be called prior to visit_end_struct() if all other intermediate
> + * visit steps were successful, to allow the caller one last chance to
> + * report errors such as remaining data that was not consumed by the visit.
> + */
> +void visit_check_struct(Visitor *v, Error **errp);
> +/**
>   * Complete a struct visit started earlier.
>   * Must be called after any successful use of visit_start_struct(),
>   * even if intermediate processing was skipped due to errors, to allow
>   * the backend to release any resources.
>   */
> -void visit_end_struct(Visitor *v, Error **errp);
> +void visit_end_struct(Visitor *v);
>
>  /**
>   * Prepare to visit an implicit struct.
> @@ -96,8 +103,8 @@ void visit_start_implicit_struct(Visitor *v, void **obj, size_t size,
>   * Must be called after any successful use of visit_start_implicit_struct(),
>   * even if intermediate processing was skipped due to errors, to allow
>   * the backend to release any resources.  Unlike visit_end_struct(), this
> - * does not need to check for errors (detection of unused keys is only
> - * possible for the overall struct, not a subset).
> + * does not need a counterpart function to check for errors (detection of
> + * unused keys is only possible for the overall struct, not a subset).
>   */
>  void visit_end_implicit_struct(Visitor *v);
>
> diff --git a/qapi/opts-visitor.c b/qapi/opts-visitor.c
> index df312e6..b469573 100644
> --- a/qapi/opts-visitor.c
> +++ b/qapi/opts-visitor.c
> @@ -157,13 +157,13 @@ opts_start_struct(Visitor *v, const char *name, void **obj,
>
>
>  static void
> -opts_end_struct(Visitor *v, Error **errp)
> +opts_check_struct(Visitor *v, Error **errp)
>  {
>      OptsVisitor *ov = to_ov(v);
>      GHashTableIter iter;
>      GQueue *any;
>
> -    if (--ov->depth > 0) {
> +    if (ov->depth > 0) {
>          return;
>      }
>

Not fully visible in this diff: this function's only possible side
effect is setting an error.  Therefore, skipping this when there have
been errors is safe.

> @@ -175,6 +175,18 @@ opts_end_struct(Visitor *v, Error **errp)
>          first = g_queue_peek_head(any);
>          error_setg(errp, QERR_INVALID_PARAMETER, first->name);
>      }
> +}
> +
> +
> +static void
> +opts_end_struct(Visitor *v)
> +{
> +    OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
> +
> +    if (--ov->depth > 0) {
> +        return;
> +    }
> +
>      g_hash_table_destroy(ov->unprocessed_opts);
>      ov->unprocessed_opts = NULL;
>      if (ov->fake_id_opt) {
> @@ -506,6 +518,7 @@ opts_visitor_new(const QemuOpts *opts)
>      ov = g_malloc0(sizeof *ov);
>
>      ov->visitor.start_struct = &opts_start_struct;
> +    ov->visitor.check_struct = &opts_check_struct;
>      ov->visitor.end_struct   = &opts_end_struct;
>
>      ov->visitor.start_list = &opts_start_list;
> diff --git a/qapi/qapi-dealloc-visitor.c b/qapi/qapi-dealloc-visitor.c
> index 1a54864..a89e6d1 100644
> --- a/qapi/qapi-dealloc-visitor.c
> +++ b/qapi/qapi-dealloc-visitor.c
> @@ -65,7 +65,7 @@ static void qapi_dealloc_start_struct(Visitor *v, const char *name, void **obj,
>      qapi_dealloc_push(qov, obj);
>  }
>
> -static void qapi_dealloc_end_struct(Visitor *v, Error **errp)
> +static void qapi_dealloc_end_struct(Visitor *v)
>  {
>      QapiDeallocVisitor *qov = to_qov(v);
>      void **obj = qapi_dealloc_pop(qov);
> diff --git a/qapi/qapi-visit-core.c b/qapi/qapi-visit-core.c
> index 3360cdf..9506a02 100644
> --- a/qapi/qapi-visit-core.c
> +++ b/qapi/qapi-visit-core.c
> @@ -36,9 +36,16 @@ void visit_start_struct(Visitor *v, const char *name, void **obj,
>      v->start_struct(v, name, obj, size, errp);
>  }
>
> -void visit_end_struct(Visitor *v, Error **errp)
> +void visit_check_struct(Visitor *v, Error **errp)
>  {
> -    v->end_struct(v, errp);
> +    if (v->check_struct) {
> +        v->check_struct(v, errp);
> +    }
> +}
> +
> +void visit_end_struct(Visitor *v)
> +{
> +    v->end_struct(v);
>  }
>
>  void visit_start_implicit_struct(Visitor *v, void **obj, size_t size,
> diff --git a/qapi/qmp-input-visitor.c b/qapi/qmp-input-visitor.c
> index 91ef945..f256d9e 100644
> --- a/qapi/qmp-input-visitor.c
> +++ b/qapi/qmp-input-visitor.c
> @@ -89,8 +89,10 @@ static void qmp_input_push(QmpInputVisitor *qiv, QObject *obj, Error **errp)
>  }
>
>
> -static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
> +static void qmp_input_check_struct(Visitor *v, Error **errp)
>  {
> +    QmpInputVisitor *qiv = to_qiv(v);
> +
>      assert(qiv->nb_stack > 0);
>
>      if (qiv->strict) {

Same argument as for opts_check_struct().

> @@ -103,6 +105,19 @@ static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
>              if (g_hash_table_iter_next(&iter, (void **)&key, NULL)) {
>                  error_setg(errp, QERR_QMP_EXTRA_MEMBER, key);
>              }
> +        }
> +    }
> +}
> +
> +static void qmp_input_pop(Visitor *v)
> +{
> +    QmpInputVisitor *qiv = to_qiv(v);
> +
> +    assert(qiv->nb_stack > 0);
> +
> +    if (qiv->strict) {
> +        GHashTable * const top_ht = qiv->stack[qiv->nb_stack - 1].h;
> +        if (top_ht) {
>              g_hash_table_unref(top_ht);
>          }
>      }
> @@ -134,12 +149,6 @@ static void qmp_input_start_struct(Visitor *v, const char *name, void **obj,
>      }
>  }
>
> -static void qmp_input_end_struct(Visitor *v, Error **errp)
> -{
> -    QmpInputVisitor *qiv = to_qiv(v);
> -
> -    qmp_input_pop(qiv, errp);
> -}

Order is now funny: the end_struct() method preceeds the start_struct()
method.  I guess that's okay.

>
>  static void qmp_input_start_implicit_struct(Visitor *v, void **obj,
>                                              size_t size, Error **errp)
> @@ -193,12 +202,6 @@ static GenericList *qmp_input_next_list(Visitor *v, GenericList **list,
>      return entry;
>  }
>
> -static void qmp_input_end_list(Visitor *v)
> -{
> -    QmpInputVisitor *qiv = to_qiv(v);
> -
> -    qmp_input_pop(qiv, &error_abort);
> -}

Likewise.

>
>  static void qmp_input_get_next_type(Visitor *v, const char *name, QType *type,
>                                      bool promote_int, Error **errp)
> @@ -355,11 +358,12 @@ QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
>      v = g_malloc0(sizeof(*v));
>
>      v->visitor.start_struct = qmp_input_start_struct;
> -    v->visitor.end_struct = qmp_input_end_struct;
> +    v->visitor.check_struct = qmp_input_check_struct;
> +    v->visitor.end_struct = qmp_input_pop;
>      v->visitor.start_implicit_struct = qmp_input_start_implicit_struct;
>      v->visitor.start_list = qmp_input_start_list;
>      v->visitor.next_list = qmp_input_next_list;
> -    v->visitor.end_list = qmp_input_end_list;
> +    v->visitor.end_list = qmp_input_pop;
>      v->visitor.type_enum = input_type_enum;
>      v->visitor.type_int64 = qmp_input_type_int64;
>      v->visitor.type_uint64 = qmp_input_type_uint64;
> diff --git a/qapi/qmp-output-visitor.c b/qapi/qmp-output-visitor.c
> index 2eb200d..5376948 100644
> --- a/qapi/qmp-output-visitor.c
> +++ b/qapi/qmp-output-visitor.c
> @@ -116,7 +116,7 @@ static void qmp_output_start_struct(Visitor *v, const char *name, void **obj,
>      qmp_output_push(qov, dict);
>  }
>
> -static void qmp_output_end_struct(Visitor *v, Error **errp)
> +static void qmp_output_end_struct(Visitor *v)
>  {
>      QmpOutputVisitor *qov = to_qov(v);
>      qmp_output_pop(qov, QTYPE_QDICT);
> diff --git a/qom/object.c b/qom/object.c
> index e5b0566..4644cbd 100644
> --- a/qom/object.c
> +++ b/qom/object.c
> @@ -2034,10 +2034,9 @@ static void property_get_tm(Object *obj, Visitor *v, const char *name,
>      if (err) {
>          goto out_end;
>      }
> +    visit_check_struct(v, &err);
>  out_end:
> -    error_propagate(errp, err);
> -    err = NULL;
> -    visit_end_struct(v, errp);
> +    visit_end_struct(v);
>  out:
>      error_propagate(errp, err);
>
> diff --git a/scripts/qapi-event.py b/scripts/qapi-event.py
> index cc55de7..dea45c4 100644
> --- a/scripts/qapi-event.py
> +++ b/scripts/qapi-event.py
> @@ -71,8 +71,9 @@ def gen_event_send(name, arg_type):
>          ret += gen_visit_fields(arg_type.members, need_cast=True,
>                                  label='out_obj')
>          ret += mcgen('''
> +    visit_check_struct(v, &err);
>  out_obj:
> -    visit_end_struct(v, &err);
> +    visit_end_struct(v);
>      if (err) {
>          goto out;
>      }
> diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py
> index 7a44c13..8039b97 100644
> --- a/scripts/qapi-visit.py
> +++ b/scripts/qapi-visit.py
> @@ -255,8 +255,7 @@ void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error
>      visit_type_%(c_name)s_fields(v, %(cast)sobj, &err);
>  ''',
>                       c_name=type_name, cast=cast)
> -        if variants:
> -            ret += gen_err_check(label='out_obj')
> +        ret += gen_err_check(label='out_obj')

Can't immediately see why we need this change.  Care to explain?

>
>      if variants:
>          ret += mcgen('''
> @@ -293,12 +292,12 @@ void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error
>          abort();
>      }
>  ''')
> +        ret += gen_err_check(label='out_obj')

Yes, because we need to skip...

>
>      ret += mcgen('''
> +    visit_check_struct(v, &err);

... this one.

>  out_obj:
> -    error_propagate(errp, err);
> -    err = NULL;
> -    visit_end_struct(v, &err);
> +    visit_end_struct(v);
>  out:
>      error_propagate(errp, err);
>  }
> diff --git a/tests/test-qmp-input-visitor.c b/tests/test-qmp-input-visitor.c
> index 6489e4a..cd5e765 100644
> --- a/tests/test-qmp-input-visitor.c
> +++ b/tests/test-qmp-input-visitor.c
> @@ -290,7 +290,8 @@ static void test_visitor_in_null(TestInputVisitorData *data,
>      v = visitor_input_test_init(data, "{ 'a': null }");
>      visit_start_struct(v, NULL, NULL, 0, &error_abort);
>      visit_type_null(v, "a", &error_abort);
> -    visit_end_struct(v, &error_abort);
> +    visit_check_struct(v, &error_abort);
> +    visit_end_struct(v);
>
>      /* Check that qnull reference counting is sane:
>       * 1 for global use, 1 for our qnull() use, and 1 still owned by 'v'
> diff --git a/vl.c b/vl.c
> index 22062b5..00c92b8 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -2842,11 +2842,10 @@ static int object_create(void *opaque, QemuOpts *opts, Error **errp)
>      qdict_del(pdict, "qom-type");
>      visit_type_str(v, "qom-type", &type, &err);
>      if (err) {
> -        goto out;
> +        goto out_end;

Before: visit_end_struct() not called.

After: visit_end_struct() called.

Shouldn't this be fixed in PATCH 05?

>      }
>      if (!type_predicate(type)) {
> -        visit_end_struct(v, NULL);
> -        goto out;
> +        goto out_end;
>      }
>
>      qdict_del(pdict, "id");
> @@ -2856,14 +2855,14 @@ static int object_create(void *opaque, QemuOpts *opts, Error **errp)
>      }
>
>      object_add(type, id, pdict, v, &err);
> -
> -out_end:
> -    visit_end_struct(v, &err_end);
> +    visit_check_struct(v, &err_end);
>      if (!err && err_end) {
>          qmp_object_del(id, NULL);
>      }
>      error_propagate(&err, err_end);
>
> +out_end:
> +    visit_end_struct(v);
>  out:
>      opts_visitor_cleanup(ov);
diff mbox

Patch

diff --git a/hmp.c b/hmp.c
index a4b74df..245617d 100644
--- a/hmp.c
+++ b/hmp.c
@@ -1687,13 +1687,14 @@  void hmp_object_add(Monitor *mon, const QDict *qdict)
     }

     object_add(type, id, pdict, v, &err);
-
-out_end:
-    visit_end_struct(v, &err_end);
+    visit_check_struct(v, &err_end);
     if (!err && err_end) {
         qmp_object_del(id, NULL);
     }
     error_propagate(&err, err_end);
+
+out_end:
+    visit_end_struct(v);
 out_clean:
     opts_visitor_cleanup(ov);

diff --git a/hw/ppc/spapr_drc.c b/hw/ppc/spapr_drc.c
index 831ce23..3b27caa 100644
--- a/hw/ppc/spapr_drc.c
+++ b/hw/ppc/spapr_drc.c
@@ -287,11 +287,12 @@  static void prop_get_fdt(Object *obj, Visitor *v, const char *name,
         case FDT_END_NODE:
             /* shouldn't ever see an FDT_END_NODE before FDT_BEGIN_NODE */
             g_assert(fdt_depth > 0);
-            visit_end_struct(v, &err);
+            visit_check_struct(v, &err);
             if (err) {
                 error_propagate(errp, err);
                 return;
             }
+            visit_end_struct(v);
             fdt_depth--;
             break;
         case FDT_PROP: {
diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c
index 24ecd87..cefd43f 100644
--- a/hw/virtio/virtio-balloon.c
+++ b/hw/virtio/virtio-balloon.c
@@ -136,15 +136,15 @@  static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name,
             goto out_nested;
         }
     }
+    visit_check_struct(v, &err);
 out_nested:
-    error_propagate(errp, err);
-    err = NULL;
-    visit_end_struct(v, &err);
+    visit_end_struct(v);

+    if (!err) {
+        visit_check_struct(v, &err);
+    }
 out_end:
-    error_propagate(errp, err);
-    err = NULL;
-    visit_end_struct(v, &err);
+    visit_end_struct(v);
 out:
     error_propagate(errp, err);
 }
diff --git a/include/qapi/visitor-impl.h b/include/qapi/visitor-impl.h
index 3b68b7b..248b1e5 100644
--- a/include/qapi/visitor-impl.h
+++ b/include/qapi/visitor-impl.h
@@ -28,8 +28,10 @@  struct Visitor
      * currently visit structs). */
     void (*start_struct)(Visitor *v, const char *name, void **obj,
                          size_t size, Error **errp);
+    /* May be NULL; most useful for input visitors. */
+    void (*check_struct)(Visitor *v, Error **errp);
     /* Must be provided if start_struct is present. */
-    void (*end_struct)(Visitor *v, Error **errp);
+    void (*end_struct)(Visitor *v);

     /* May be NULL; most useful for input visitors. */
     void (*start_implicit_struct)(Visitor *v, void **obj, size_t size,
diff --git a/include/qapi/visitor.h b/include/qapi/visitor.h
index d7a0110..e5dcde4 100644
--- a/include/qapi/visitor.h
+++ b/include/qapi/visitor.h
@@ -68,12 +68,19 @@  typedef struct GenericList
 void visit_start_struct(Visitor *v, const char *name, void **obj,
                         size_t size, Error **errp);
 /**
+ * Prepare for completing a struct visit.
+ * Should be called prior to visit_end_struct() if all other intermediate
+ * visit steps were successful, to allow the caller one last chance to
+ * report errors such as remaining data that was not consumed by the visit.
+ */
+void visit_check_struct(Visitor *v, Error **errp);
+/**
  * Complete a struct visit started earlier.
  * Must be called after any successful use of visit_start_struct(),
  * even if intermediate processing was skipped due to errors, to allow
  * the backend to release any resources.
  */
-void visit_end_struct(Visitor *v, Error **errp);
+void visit_end_struct(Visitor *v);

 /**
  * Prepare to visit an implicit struct.
@@ -96,8 +103,8 @@  void visit_start_implicit_struct(Visitor *v, void **obj, size_t size,
  * Must be called after any successful use of visit_start_implicit_struct(),
  * even if intermediate processing was skipped due to errors, to allow
  * the backend to release any resources.  Unlike visit_end_struct(), this
- * does not need to check for errors (detection of unused keys is only
- * possible for the overall struct, not a subset).
+ * does not need a counterpart function to check for errors (detection of
+ * unused keys is only possible for the overall struct, not a subset).
  */
 void visit_end_implicit_struct(Visitor *v);

diff --git a/qapi/opts-visitor.c b/qapi/opts-visitor.c
index df312e6..b469573 100644
--- a/qapi/opts-visitor.c
+++ b/qapi/opts-visitor.c
@@ -157,13 +157,13 @@  opts_start_struct(Visitor *v, const char *name, void **obj,


 static void
-opts_end_struct(Visitor *v, Error **errp)
+opts_check_struct(Visitor *v, Error **errp)
 {
     OptsVisitor *ov = to_ov(v);
     GHashTableIter iter;
     GQueue *any;

-    if (--ov->depth > 0) {
+    if (ov->depth > 0) {
         return;
     }

@@ -175,6 +175,18 @@  opts_end_struct(Visitor *v, Error **errp)
         first = g_queue_peek_head(any);
         error_setg(errp, QERR_INVALID_PARAMETER, first->name);
     }
+}
+
+
+static void
+opts_end_struct(Visitor *v)
+{
+    OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
+
+    if (--ov->depth > 0) {
+        return;
+    }
+
     g_hash_table_destroy(ov->unprocessed_opts);
     ov->unprocessed_opts = NULL;
     if (ov->fake_id_opt) {
@@ -506,6 +518,7 @@  opts_visitor_new(const QemuOpts *opts)
     ov = g_malloc0(sizeof *ov);

     ov->visitor.start_struct = &opts_start_struct;
+    ov->visitor.check_struct = &opts_check_struct;
     ov->visitor.end_struct   = &opts_end_struct;

     ov->visitor.start_list = &opts_start_list;
diff --git a/qapi/qapi-dealloc-visitor.c b/qapi/qapi-dealloc-visitor.c
index 1a54864..a89e6d1 100644
--- a/qapi/qapi-dealloc-visitor.c
+++ b/qapi/qapi-dealloc-visitor.c
@@ -65,7 +65,7 @@  static void qapi_dealloc_start_struct(Visitor *v, const char *name, void **obj,
     qapi_dealloc_push(qov, obj);
 }

-static void qapi_dealloc_end_struct(Visitor *v, Error **errp)
+static void qapi_dealloc_end_struct(Visitor *v)
 {
     QapiDeallocVisitor *qov = to_qov(v);
     void **obj = qapi_dealloc_pop(qov);
diff --git a/qapi/qapi-visit-core.c b/qapi/qapi-visit-core.c
index 3360cdf..9506a02 100644
--- a/qapi/qapi-visit-core.c
+++ b/qapi/qapi-visit-core.c
@@ -36,9 +36,16 @@  void visit_start_struct(Visitor *v, const char *name, void **obj,
     v->start_struct(v, name, obj, size, errp);
 }

-void visit_end_struct(Visitor *v, Error **errp)
+void visit_check_struct(Visitor *v, Error **errp)
 {
-    v->end_struct(v, errp);
+    if (v->check_struct) {
+        v->check_struct(v, errp);
+    }
+}
+
+void visit_end_struct(Visitor *v)
+{
+    v->end_struct(v);
 }

 void visit_start_implicit_struct(Visitor *v, void **obj, size_t size,
diff --git a/qapi/qmp-input-visitor.c b/qapi/qmp-input-visitor.c
index 91ef945..f256d9e 100644
--- a/qapi/qmp-input-visitor.c
+++ b/qapi/qmp-input-visitor.c
@@ -89,8 +89,10 @@  static void qmp_input_push(QmpInputVisitor *qiv, QObject *obj, Error **errp)
 }


-static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
+static void qmp_input_check_struct(Visitor *v, Error **errp)
 {
+    QmpInputVisitor *qiv = to_qiv(v);
+
     assert(qiv->nb_stack > 0);

     if (qiv->strict) {
@@ -103,6 +105,19 @@  static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
             if (g_hash_table_iter_next(&iter, (void **)&key, NULL)) {
                 error_setg(errp, QERR_QMP_EXTRA_MEMBER, key);
             }
+        }
+    }
+}
+
+static void qmp_input_pop(Visitor *v)
+{
+    QmpInputVisitor *qiv = to_qiv(v);
+
+    assert(qiv->nb_stack > 0);
+
+    if (qiv->strict) {
+        GHashTable * const top_ht = qiv->stack[qiv->nb_stack - 1].h;
+        if (top_ht) {
             g_hash_table_unref(top_ht);
         }
     }
@@ -134,12 +149,6 @@  static void qmp_input_start_struct(Visitor *v, const char *name, void **obj,
     }
 }

-static void qmp_input_end_struct(Visitor *v, Error **errp)
-{
-    QmpInputVisitor *qiv = to_qiv(v);
-
-    qmp_input_pop(qiv, errp);
-}

 static void qmp_input_start_implicit_struct(Visitor *v, void **obj,
                                             size_t size, Error **errp)
@@ -193,12 +202,6 @@  static GenericList *qmp_input_next_list(Visitor *v, GenericList **list,
     return entry;
 }

-static void qmp_input_end_list(Visitor *v)
-{
-    QmpInputVisitor *qiv = to_qiv(v);
-
-    qmp_input_pop(qiv, &error_abort);
-}

 static void qmp_input_get_next_type(Visitor *v, const char *name, QType *type,
                                     bool promote_int, Error **errp)
@@ -355,11 +358,12 @@  QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
     v = g_malloc0(sizeof(*v));

     v->visitor.start_struct = qmp_input_start_struct;
-    v->visitor.end_struct = qmp_input_end_struct;
+    v->visitor.check_struct = qmp_input_check_struct;
+    v->visitor.end_struct = qmp_input_pop;
     v->visitor.start_implicit_struct = qmp_input_start_implicit_struct;
     v->visitor.start_list = qmp_input_start_list;
     v->visitor.next_list = qmp_input_next_list;
-    v->visitor.end_list = qmp_input_end_list;
+    v->visitor.end_list = qmp_input_pop;
     v->visitor.type_enum = input_type_enum;
     v->visitor.type_int64 = qmp_input_type_int64;
     v->visitor.type_uint64 = qmp_input_type_uint64;
diff --git a/qapi/qmp-output-visitor.c b/qapi/qmp-output-visitor.c
index 2eb200d..5376948 100644
--- a/qapi/qmp-output-visitor.c
+++ b/qapi/qmp-output-visitor.c
@@ -116,7 +116,7 @@  static void qmp_output_start_struct(Visitor *v, const char *name, void **obj,
     qmp_output_push(qov, dict);
 }

-static void qmp_output_end_struct(Visitor *v, Error **errp)
+static void qmp_output_end_struct(Visitor *v)
 {
     QmpOutputVisitor *qov = to_qov(v);
     qmp_output_pop(qov, QTYPE_QDICT);
diff --git a/qom/object.c b/qom/object.c
index e5b0566..4644cbd 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -2034,10 +2034,9 @@  static void property_get_tm(Object *obj, Visitor *v, const char *name,
     if (err) {
         goto out_end;
     }
+    visit_check_struct(v, &err);
 out_end:
-    error_propagate(errp, err);
-    err = NULL;
-    visit_end_struct(v, errp);
+    visit_end_struct(v);
 out:
     error_propagate(errp, err);

diff --git a/scripts/qapi-event.py b/scripts/qapi-event.py
index cc55de7..dea45c4 100644
--- a/scripts/qapi-event.py
+++ b/scripts/qapi-event.py
@@ -71,8 +71,9 @@  def gen_event_send(name, arg_type):
         ret += gen_visit_fields(arg_type.members, need_cast=True,
                                 label='out_obj')
         ret += mcgen('''
+    visit_check_struct(v, &err);
 out_obj:
-    visit_end_struct(v, &err);
+    visit_end_struct(v);
     if (err) {
         goto out;
     }
diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py
index 7a44c13..8039b97 100644
--- a/scripts/qapi-visit.py
+++ b/scripts/qapi-visit.py
@@ -255,8 +255,7 @@  void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error
     visit_type_%(c_name)s_fields(v, %(cast)sobj, &err);
 ''',
                      c_name=type_name, cast=cast)
-        if variants:
-            ret += gen_err_check(label='out_obj')
+        ret += gen_err_check(label='out_obj')

     if variants:
         ret += mcgen('''
@@ -293,12 +292,12 @@  void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s **obj, Error
         abort();
     }
 ''')
+        ret += gen_err_check(label='out_obj')

     ret += mcgen('''
+    visit_check_struct(v, &err);
 out_obj:
-    error_propagate(errp, err);
-    err = NULL;
-    visit_end_struct(v, &err);
+    visit_end_struct(v);
 out:
     error_propagate(errp, err);
 }
diff --git a/tests/test-qmp-input-visitor.c b/tests/test-qmp-input-visitor.c
index 6489e4a..cd5e765 100644
--- a/tests/test-qmp-input-visitor.c
+++ b/tests/test-qmp-input-visitor.c
@@ -290,7 +290,8 @@  static void test_visitor_in_null(TestInputVisitorData *data,
     v = visitor_input_test_init(data, "{ 'a': null }");
     visit_start_struct(v, NULL, NULL, 0, &error_abort);
     visit_type_null(v, "a", &error_abort);
-    visit_end_struct(v, &error_abort);
+    visit_check_struct(v, &error_abort);
+    visit_end_struct(v);

     /* Check that qnull reference counting is sane:
      * 1 for global use, 1 for our qnull() use, and 1 still owned by 'v'
diff --git a/vl.c b/vl.c
index 22062b5..00c92b8 100644
--- a/vl.c
+++ b/vl.c
@@ -2842,11 +2842,10 @@  static int object_create(void *opaque, QemuOpts *opts, Error **errp)
     qdict_del(pdict, "qom-type");
     visit_type_str(v, "qom-type", &type, &err);
     if (err) {
-        goto out;
+        goto out_end;
     }
     if (!type_predicate(type)) {
-        visit_end_struct(v, NULL);
-        goto out;
+        goto out_end;
     }

     qdict_del(pdict, "id");
@@ -2856,14 +2855,14 @@  static int object_create(void *opaque, QemuOpts *opts, Error **errp)
     }

     object_add(type, id, pdict, v, &err);
-
-out_end:
-    visit_end_struct(v, &err_end);
+    visit_check_struct(v, &err_end);
     if (!err && err_end) {
         qmp_object_del(id, NULL);
     }
     error_propagate(&err, err_end);

+out_end:
+    visit_end_struct(v);
 out:
     opts_visitor_cleanup(ov);