@@ -22,7 +22,7 @@ struct Visitor
const char *name, size_t size, Error **errp);
void (*end_struct)(Visitor *v, Error **errp);
- void (*start_implicit_struct)(Visitor *v, void **obj, size_t size,
+ bool (*start_implicit_struct)(Visitor *v, void **obj, size_t size,
Error **errp);
void (*end_implicit_struct)(Visitor *v, Error **errp);
@@ -57,8 +57,6 @@ struct Visitor
void (*type_int64)(Visitor *v, int64_t *obj, const char *name, Error **errp);
/* visit_type_size() falls back to (*type_uint64)() if type_size is unset */
void (*type_size)(Visitor *v, uint64_t *obj, const char *name, Error **errp);
- bool (*start_union)(Visitor *v, bool data_present, Error **errp);
- void (*end_union)(Visitor *v, bool data_present, Error **errp);
};
void input_type_enum(Visitor *v, int *obj, const char * const strings[],
@@ -33,7 +33,7 @@ void visit_end_handle(Visitor *v, Error **errp);
void visit_start_struct(Visitor *v, void **obj, const char *kind,
const char *name, size_t size, Error **errp);
void visit_end_struct(Visitor *v, Error **errp);
-void visit_start_implicit_struct(Visitor *v, void **obj, size_t size,
+bool visit_start_implicit_struct(Visitor *v, void **obj, size_t size,
Error **errp);
void visit_end_implicit_struct(Visitor *v, Error **errp);
void visit_start_list(Visitor *v, const char *name, Error **errp);
@@ -59,7 +59,5 @@ void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp);
void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp);
void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp);
void visit_type_any(Visitor *v, QObject **obj, const char *name, Error **errp);
-bool visit_start_union(Visitor *v, bool data_present, Error **errp);
-void visit_end_union(Visitor *v, bool data_present, Error **errp);
#endif
@@ -76,13 +76,17 @@ static void qapi_dealloc_end_struct(Visitor *v, Error **errp)
}
}
-static void qapi_dealloc_start_implicit_struct(Visitor *v,
+static bool qapi_dealloc_start_implicit_struct(Visitor *v,
void **obj,
size_t size,
Error **errp)
{
QapiDeallocVisitor *qov = to_qov(v);
- qapi_dealloc_push(qov, obj);
+ if (obj && *obj) {
+ qapi_dealloc_push(qov, obj);
+ return true;
+ }
+ return false;
}
static void qapi_dealloc_end_implicit_struct(Visitor *v, Error **errp)
@@ -90,6 +94,7 @@ static void qapi_dealloc_end_implicit_struct(Visitor *v, Error **errp)
QapiDeallocVisitor *qov = to_qov(v);
void **obj = qapi_dealloc_pop(qov);
if (obj) {
+ assert(*obj);
g_free(*obj);
}
}
@@ -171,31 +176,6 @@ static void qapi_dealloc_type_enum(Visitor *v, int *obj,
{
}
-/* If there's no data present, the dealloc visitor has nothing to free.
- * Thus, indicate to visitor code that the subsequent union fields can
- * be skipped. This is not an error condition, since the cleanup of the
- * rest of an object can continue unhindered, so leave errp unset in
- * these cases.
- *
- * NOTE: In cases where we're attempting to deallocate an object that
- * may have missing fields, the field indicating the union type may
- * be missing. In such a case, it's possible we don't have enough
- * information to differentiate data_present == false from a case where
- * data *is* present but happens to be a scalar with a value of 0.
- * This is okay, since in the case of the dealloc visitor there's no
- * work that needs to done in either situation.
- *
- * The current inability in QAPI code to more thoroughly verify a union
- * type in such cases will likely need to be addressed if we wish to
- * implement this interface for other types of visitors in the future,
- * however.
- */
-static bool qapi_dealloc_start_union(Visitor *v, bool data_present,
- Error **errp)
-{
- return data_present;
-}
-
Visitor *qapi_dealloc_get_visitor(QapiDeallocVisitor *v)
{
return &v->visitor;
@@ -226,7 +206,6 @@ QapiDeallocVisitor *qapi_dealloc_visitor_new(void)
v->visitor.type_number = qapi_dealloc_type_number;
v->visitor.type_any = qapi_dealloc_type_anything;
v->visitor.type_size = qapi_dealloc_type_size;
- v->visitor.start_union = qapi_dealloc_start_union;
QTAILQ_INIT(&v->stack);
@@ -28,12 +28,13 @@ void visit_end_struct(Visitor *v, Error **errp)
v->end_struct(v, errp);
}
-void visit_start_implicit_struct(Visitor *v, void **obj, size_t size,
+bool visit_start_implicit_struct(Visitor *v, void **obj, size_t size,
Error **errp)
{
if (v->start_implicit_struct) {
- v->start_implicit_struct(v, obj, size, errp);
+ return v->start_implicit_struct(v, obj, size, errp);
}
+ return true;
}
void visit_end_implicit_struct(Visitor *v, Error **errp)
@@ -58,21 +59,6 @@ void visit_end_list(Visitor *v, Error **errp)
v->end_list(v, errp);
}
-bool visit_start_union(Visitor *v, bool data_present, Error **errp)
-{
- if (v->start_union) {
- return v->start_union(v, data_present, errp);
- }
- return true;
-}
-
-void visit_end_union(Visitor *v, bool data_present, Error **errp)
-{
- if (v->end_union) {
- v->end_union(v, data_present, errp);
- }
-}
-
void visit_optional(Visitor *v, bool *present, const char *name,
Error **errp)
{
@@ -145,12 +145,13 @@ static void qmp_input_end_struct(Visitor *v, Error **errp)
qmp_input_pop(qiv, errp);
}
-static void qmp_input_start_implicit_struct(Visitor *v, void **obj,
+static bool qmp_input_start_implicit_struct(Visitor *v, void **obj,
size_t size, Error **errp)
{
if (obj) {
*obj = g_malloc0(size);
}
+ return true;
}
static void qmp_input_end_implicit_struct(Visitor *v, Error **errp)
@@ -47,8 +47,7 @@ static void visit_type_implicit_%(c_type)s(Visitor *m, %(c_type)s **obj, Error *
{
Error *err = NULL;
- visit_start_implicit_struct(m, (void **)obj, sizeof(%(c_type)s), &err);
- if (!err) {
+ if (visit_start_implicit_struct(m, (void **)obj, sizeof(%(c_type)s), &err) && !err) {
visit_type_%(c_type)s_fields(m, obj, errp);
visit_end_implicit_struct(m, &err);
}
@@ -288,9 +287,6 @@ void visit_type_%(c_name)s(Visitor *m, %(c_name)s **obj, const char *name, Error
c_type=variants.tag_member.type.c_name(),
c_name=c_name(tag_key), name=tag_key)
ret += mcgen('''
- if (!visit_start_union(m, !!(*obj)->data, &err) || err) {
- goto out_obj;
- }
switch ((*obj)->%(c_name)s) {
''',
c_name=c_name(tag_key))
@@ -325,9 +321,6 @@ void visit_type_%(c_name)s(Visitor *m, %(c_name)s **obj, const char *name, Error
out_obj:
error_propagate(errp, err);
err = NULL;
- visit_end_union(m, !!(*obj)->data, &err);
- error_propagate(errp, err);
- err = NULL;
}
visit_end_struct(m, &err);
out:
Commit cee2dedb noticed that if you have a partial flat union (such as if an input parse failed due to a missing discriminator), calling the dealloc visitor could result in trying to dereference the NULL pointer. But the fix it proposed requires the use of a 'data' member in the union, which may or may not be the same size as other branches of the union (consider a 32-bit platform where one of the branches is an int64), so it feels fairly dirty. A better, and much shorter, fix is to tweak all of the generated visit_implicit_FOO() functions to avoid dereferencing NULL in the first place, by enhancing the dealloc visitor to not try to descend through an implicit struct that was not allocated. The lack of documentation on the visitor interface is appalling, but I'm not fixing it here. My intended semantics is that if visit_start_implicit_struct() returns true, then the fields of the struct will be visited and visit_end_impicit_struct() must also be called; if it returns false, then everything else about the struct will be skipped. Perhaps we should guarantee that visit_end_implicit_struct() will always be called? Perhaps we should pass a bool parameter to visit_end_implicit_struct() that preserves the value returned by visit_start_implicit_struct()? Signed-off-by: Eric Blake <eblake@redhat.com> --- include/qapi/visitor-impl.h | 4 +--- include/qapi/visitor.h | 4 +--- qapi/qapi-dealloc-visitor.c | 35 +++++++---------------------------- qapi/qapi-visit-core.c | 20 +++----------------- qapi/qmp-input-visitor.c | 3 ++- scripts/qapi-visit.py | 9 +-------- 6 files changed, 15 insertions(+), 60 deletions(-)