[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v1 3/6] qapi: assert that visitor impls have require
From: |
Daniel P. Berrange |
Subject: |
[Qemu-devel] [PATCH v1 3/6] qapi: assert that visitor impls have required callbacks |
Date: |
Tue, 7 Jun 2016 11:11:12 +0100 |
Not all visitor implementations supply the full set of
visitor callback functions. For example, the string
output visitor does not provide 'start_struct' and
friends. If you don't know this and feed it an object
that uses structs, you'll get a crash:
Segmentation fault (core dumped)
Crashing is fine, because this is a programmer mistake,
but we can improve the error message upon crash to make
it obvious what failed by adding assert()s:
qapi/qapi-visit-core.c:32: visit_start_struct: Assertion `v->start_struct !=
((void *)0)' failed.
Signed-off-by: Daniel P. Berrange <address@hidden>
---
qapi/qapi-visit-core.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/qapi/qapi-visit-core.c b/qapi/qapi-visit-core.c
index eada467..3b5efbe 100644
--- a/qapi/qapi-visit-core.c
+++ b/qapi/qapi-visit-core.c
@@ -29,6 +29,7 @@ void visit_start_struct(Visitor *v, const char *name, void
**obj,
assert(size);
assert(v->type != VISITOR_OUTPUT || *obj);
}
+ assert(v->start_struct != NULL);
v->start_struct(v, name, obj, size, &err);
if (obj && v->type == VISITOR_INPUT) {
assert(!err != !*obj);
@@ -45,6 +46,7 @@ void visit_check_struct(Visitor *v, Error **errp)
void visit_end_struct(Visitor *v)
{
+ assert(v->end_struct != NULL);
v->end_struct(v);
}
@@ -54,6 +56,7 @@ void visit_start_list(Visitor *v, const char *name,
GenericList **list,
Error *err = NULL;
assert(!list || size >= sizeof(GenericList));
+ assert(v->start_list != NULL);
v->start_list(v, name, list, size, &err);
if (list && v->type == VISITOR_INPUT) {
assert(!(err && *list));
@@ -64,11 +67,13 @@ void visit_start_list(Visitor *v, const char *name,
GenericList **list,
GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size)
{
assert(tail && size >= sizeof(GenericList));
+ assert(v->next_list != NULL);
return v->next_list(v, tail, size);
}
void visit_end_list(Visitor *v)
{
+ assert(v->end_list != NULL);
v->end_list(v);
}
@@ -112,6 +117,7 @@ bool visit_is_input(Visitor *v)
void visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp)
{
assert(obj);
+ assert(v->type_int64 != NULL);
v->type_int64(v, name, obj, errp);
}
@@ -121,6 +127,7 @@ static void visit_type_uintN(Visitor *v, uint64_t *obj,
const char *name,
Error *err = NULL;
uint64_t value = *obj;
+ assert(v->type_uint64 != NULL);
v->type_uint64(v, name, &value, &err);
if (err) {
error_propagate(errp, err);
@@ -160,6 +167,7 @@ void visit_type_uint64(Visitor *v, const char *name,
uint64_t *obj,
Error **errp)
{
assert(obj);
+ assert(v->type_uint64 != NULL);
v->type_uint64(v, name, obj, errp);
}
@@ -170,6 +178,7 @@ static void visit_type_intN(Visitor *v, int64_t *obj, const
char *name,
Error *err = NULL;
int64_t value = *obj;
+ assert(v->type_int64 != NULL);
v->type_int64(v, name, &value, &err);
if (err) {
error_propagate(errp, err);
@@ -218,6 +227,7 @@ void visit_type_size(Visitor *v, const char *name, uint64_t
*obj,
if (v->type_size) {
v->type_size(v, name, obj, errp);
} else {
+ assert(v->type_uint64 != NULL);
v->type_uint64(v, name, obj, errp);
}
}
@@ -225,6 +235,7 @@ void visit_type_size(Visitor *v, const char *name, uint64_t
*obj,
void visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
{
assert(obj);
+ assert(v->type_bool != NULL);
v->type_bool(v, name, obj, errp);
}
@@ -237,6 +248,7 @@ void visit_type_str(Visitor *v, const char *name, char
**obj, Error **errp)
* can enable:
assert(v->type != VISITOR_OUTPUT || *obj);
*/
+ assert(v->type_str != NULL);
v->type_str(v, name, obj, &err);
if (v->type == VISITOR_INPUT) {
assert(!err != !*obj);
@@ -248,6 +260,7 @@ void visit_type_number(Visitor *v, const char *name, double
*obj,
Error **errp)
{
assert(obj);
+ assert(v->type_number != NULL);
v->type_number(v, name, obj, errp);
}
@@ -257,6 +270,7 @@ void visit_type_any(Visitor *v, const char *name, QObject
**obj, Error **errp)
assert(obj);
assert(v->type != VISITOR_OUTPUT || *obj);
+ assert(v->type_any != NULL);
v->type_any(v, name, obj, &err);
if (v->type == VISITOR_INPUT) {
assert(!err != !*obj);
@@ -266,6 +280,7 @@ void visit_type_any(Visitor *v, const char *name, QObject
**obj, Error **errp)
void visit_type_null(Visitor *v, const char *name, Error **errp)
{
+ assert(v->type_null != NULL);
v->type_null(v, name, errp);
}
--
2.5.5