Skip to content

Commit

Permalink
qapi: Consolidate QMP input visitor creation
Browse files Browse the repository at this point in the history
Rather than having two separate ways to create a QMP input
visitor, where the safer approach has the more verbose name,
it is better to consolidate things into a single function
where the caller must explicitly choose whether to be strict
or to ignore excess input.  This patch is the strictly
mechanical conversion; the next patch will then audit which
uses can be made stricter.

Signed-off-by: Eric Blake <[email protected]>
Message-Id: <[email protected]>
Signed-off-by: Markus Armbruster <[email protected]>
  • Loading branch information
ebblake authored and Markus Armbruster committed May 12, 2016
1 parent b471d01 commit fc471c1
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/qapi-code-gen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ Example:
{
Error *err = NULL;
UserDefOne *retval;
QmpInputVisitor *qiv = qmp_input_visitor_new_strict(QOBJECT(args));
QmpInputVisitor *qiv = qmp_input_visitor_new(QOBJECT(args), true);
QapiDeallocVisitor *qdv;
Visitor *v;
UserDefOneList *arg1 = NULL;
Expand Down
9 changes: 7 additions & 2 deletions include/qapi/qmp-input-visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@

typedef struct QmpInputVisitor QmpInputVisitor;

QmpInputVisitor *qmp_input_visitor_new(QObject *obj);
QmpInputVisitor *qmp_input_visitor_new_strict(QObject *obj);
/*
* Return a new input visitor that converts QMP to QAPI.
*
* Set @strict to reject a parse that doesn't consume all keys of a
* dictionary; otherwise excess input is ignored.
*/
QmpInputVisitor *qmp_input_visitor_new(QObject *obj, bool strict);

void qmp_input_visitor_cleanup(QmpInputVisitor *v);

Expand Down
13 changes: 2 additions & 11 deletions qapi/qmp-input-visitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ void qmp_input_visitor_cleanup(QmpInputVisitor *v)
g_free(v);
}

QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
QmpInputVisitor *qmp_input_visitor_new(QObject *obj, bool strict)
{
QmpInputVisitor *v;

Expand All @@ -376,19 +376,10 @@ QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
v->visitor.type_number = qmp_input_type_number;
v->visitor.type_any = qmp_input_type_any;
v->visitor.optional = qmp_input_optional;
v->strict = strict;

qmp_input_push(v, obj, NULL);
qobject_incref(obj);

return v;
}

QmpInputVisitor *qmp_input_visitor_new_strict(QObject *obj)
{
QmpInputVisitor *v;

v = qmp_input_visitor_new(obj);
v->strict = true;

return v;
}
2 changes: 1 addition & 1 deletion qmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ void qmp_object_add(const char *type, const char *id,
}
}

qiv = qmp_input_visitor_new(props);
qiv = qmp_input_visitor_new(props, false);
obj = user_creatable_add_type(type, id, pdict,
qmp_input_get_visitor(qiv), errp);
qmp_input_visitor_cleanup(qiv);
Expand Down
2 changes: 1 addition & 1 deletion qom/qom-qobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void object_property_set_qobject(Object *obj, QObject *value,
const char *name, Error **errp)
{
QmpInputVisitor *qiv;
qiv = qmp_input_visitor_new(value);
qiv = qmp_input_visitor_new(value, false);
object_property_set(obj, qmp_input_get_visitor(qiv), name, errp);

qmp_input_visitor_cleanup(qiv);
Expand Down
2 changes: 1 addition & 1 deletion replay/replay-input.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static InputEvent *qapi_clone_InputEvent(InputEvent *src)
return NULL;
}

qiv = qmp_input_visitor_new(obj);
qiv = qmp_input_visitor_new(obj, false);
iv = qmp_input_get_visitor(qiv);
visit_type_InputEvent(iv, NULL, &dst, &error_abort);
qmp_input_visitor_cleanup(qiv);
Expand Down
2 changes: 1 addition & 1 deletion scripts/qapi-commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def gen_marshal(name, arg_type, ret_type):

if arg_type and arg_type.members:
ret += mcgen('''
QmpInputVisitor *qiv = qmp_input_visitor_new_strict(QOBJECT(args));
QmpInputVisitor *qiv = qmp_input_visitor_new(QOBJECT(args), true);
QapiDeallocVisitor *qdv;
Visitor *v;
%(c_name)s arg = {0};
Expand Down
2 changes: 1 addition & 1 deletion tests/test-qmp-commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ static void test_dealloc_partial(void)
ud2_dict = qdict_new();
qdict_put_obj(ud2_dict, "string0", QOBJECT(qstring_from_str(text)));

qiv = qmp_input_visitor_new(QOBJECT(ud2_dict));
qiv = qmp_input_visitor_new(QOBJECT(ud2_dict), false);
visit_type_UserDefTwo(qmp_input_get_visitor(qiv), NULL, &ud2, &err);
qmp_input_visitor_cleanup(qiv);
QDECREF(ud2_dict);
Expand Down
2 changes: 1 addition & 1 deletion tests/test-qmp-input-strict.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static Visitor *validate_test_init_internal(TestInputVisitorData *data,
data->obj = qobject_from_jsonv(json_string, ap);
g_assert(data->obj);

data->qiv = qmp_input_visitor_new_strict(data->obj);
data->qiv = qmp_input_visitor_new(data->obj, true);
g_assert(data->qiv);

v = qmp_input_get_visitor(data->qiv);
Expand Down
2 changes: 1 addition & 1 deletion tests/test-qmp-input-visitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static Visitor *visitor_input_test_init_internal(TestInputVisitorData *data,
data->obj = qobject_from_jsonv(json_string, ap);
g_assert(data->obj);

data->qiv = qmp_input_visitor_new(data->obj);
data->qiv = qmp_input_visitor_new(data->obj, false);
g_assert(data->qiv);

v = qmp_input_get_visitor(data->qiv);
Expand Down
2 changes: 1 addition & 1 deletion tests/test-visitor-serialization.c
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ static void qmp_deserialize(void **native_out, void *datap,
obj = qobject_from_json(qstring_get_str(output_json));

QDECREF(output_json);
d->qiv = qmp_input_visitor_new(obj);
d->qiv = qmp_input_visitor_new(obj, false);
qobject_decref(obj_orig);
qobject_decref(obj);
visit(qmp_input_get_visitor(d->qiv), native_out, errp);
Expand Down
2 changes: 1 addition & 1 deletion util/qemu-sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ void qapi_copy_SocketAddress(SocketAddress **p_dest,
return;
}

qiv = qmp_input_visitor_new(obj);
qiv = qmp_input_visitor_new(obj, false);
iv = qmp_input_get_visitor(qiv);
visit_type_SocketAddress(iv, NULL, p_dest, &error_abort);
qmp_input_visitor_cleanup(qiv);
Expand Down

0 comments on commit fc471c1

Please sign in to comment.