tests: Cover input visit beyond end of list

When you try to visit beyond the end of a list, the qobject input
visitor crashes, and the string visitor screws returns garbage.  The
generated list visits never go beyond the list end, but manual visits
could.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <1488544368-30622-27-git-send-email-armbru@redhat.com>
This commit is contained in:
Markus Armbruster 2017-03-03 13:32:46 +01:00
parent a4a1c70dc7
commit a9416dc62c
3 changed files with 65 additions and 0 deletions

View File

@ -210,6 +210,43 @@ test_opts_range_unvisited(void)
qemu_opts_del(opts);
}
static void
test_opts_range_beyond(void)
{
Error *err = NULL;
intList *list = NULL;
intList *tail;
QemuOpts *opts;
Visitor *v;
int64_t val;
opts = qemu_opts_parse(qemu_find_opts("userdef"), "ilist=0", false,
&error_abort);
v = opts_visitor_new(opts);
visit_start_struct(v, NULL, NULL, 0, &error_abort);
/* Would be simpler if the visitor genuinely supported virtual walks */
visit_start_list(v, "ilist", (GenericList **)&list, sizeof(*list),
&error_abort);
tail = list;
visit_type_int(v, NULL, &tail->value, &error_abort);
g_assert_cmpint(tail->value, ==, 0);
tail = (intList *)visit_next_list(v, (GenericList *)tail, sizeof(*tail));
g_assert(!tail);
visit_type_int(v, NULL, &val, &err);
error_free_or_abort(&err);
visit_end_list(v, (void **)&list);
visit_check_struct(v, &error_abort);
visit_end_struct(v, NULL);
qapi_free_intList(list);
visit_free(v);
qemu_opts_del(opts);
}
int
main(int argc, char **argv)
{
@ -303,6 +340,8 @@ main(int argc, char **argv)
g_test_add_func("/visitor/opts/range/unvisited",
test_opts_range_unvisited);
g_test_add_func("/visitor/opts/range/beyond",
test_opts_range_beyond);
g_test_run();
return 0;

View File

@ -948,6 +948,16 @@ static void test_visitor_in_fail_list(TestInputVisitorData *data,
visit_check_list(v, &err);
error_free_or_abort(&err);
visit_end_list(v, NULL);
/* Visit beyond end of list */
v = visitor_input_test_init(data, "[]");
visit_start_list(v, NULL, NULL, 0, &error_abort);
#if 0 /* FIXME crash */
visit_type_int(v, NULL, &i64, &err);
error_free_or_abort(&err);
#endif
visit_end_list(v, NULL);
}
static void test_visitor_in_fail_list_nested(TestInputVisitorData *data,

View File

@ -123,6 +123,7 @@ static void test_visitor_in_intList(TestInputVisitorData *data,
int64List *res = NULL;
int64List *tail;
Visitor *v;
int64_t val;
/* Valid lists */
@ -175,6 +176,21 @@ static void test_visitor_in_intList(TestInputVisitorData *data,
visit_end_list(v, (void **)&res);
qapi_free_int64List(res);
/* Visit beyond end of list */
v = visitor_input_test_init(data, "0");
visit_start_list(v, NULL, (GenericList **)&res, sizeof(*res),
&error_abort);
tail = res;
visit_type_int64(v, NULL, &tail->value, &err);
g_assert_cmpint(tail->value, ==, 0);
visit_type_int64(v, NULL, &val, &err);
g_assert_cmpint(val, ==, 1); /* BUG */
visit_check_list(v, &error_abort);
visit_end_list(v, (void **)&res);
qapi_free_int64List(res);
}
static void test_visitor_in_bool(TestInputVisitorData *data,