diff options
author | Eric Blake <eblake@redhat.com> | 2016-02-17 23:48:17 -0700 |
---|---|---|
committer | Timothy Pearson <tpearson@raptorengineering.com> | 2019-11-29 19:45:30 -0600 |
commit | 7926f374f314eb5d1cb9213c99f3ccd29c15a7ea (patch) | |
tree | 4c591476cdb2c3fd319fe407ba380e72df35e23f | |
parent | 6c5dc2fd9a9065a7e5738a5b06e9ce1f51b0ef55 (diff) | |
download | hqemu-7926f374f314eb5d1cb9213c99f3ccd29c15a7ea.zip hqemu-7926f374f314eb5d1cb9213c99f3ccd29c15a7ea.tar.gz |
qapi: Forbid 'any' inside an alternate
The whole point of an alternate is to allow some type-safety while
still accepting more than one JSON type. Meanwhile, the 'any'
type exists to bypass type-safety altogether. The two are
incompatible: you can't accept every type, and still tell which
branch of the alternate to use for the parse; fix this to give a
sane error instead of a Python stack trace.
Note that other types that can't be alternate members are caught
earlier, by check_type().
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1455778109-6278-4-git-send-email-eblake@redhat.com>
[Commit message tweaked]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
-rw-r--r-- | scripts/qapi.py | 5 | ||||
-rw-r--r-- | tests/Makefile | 1 | ||||
-rw-r--r-- | tests/qapi-schema/alternate-any.err | 1 | ||||
-rw-r--r-- | tests/qapi-schema/alternate-any.exit | 1 | ||||
-rw-r--r-- | tests/qapi-schema/alternate-any.json | 4 | ||||
-rw-r--r-- | tests/qapi-schema/alternate-any.out | 0 |
6 files changed, 11 insertions, 1 deletions
diff --git a/scripts/qapi.py b/scripts/qapi.py index f97236f..17bf633 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -629,7 +629,10 @@ def check_alternate(expr, expr_info): value, allow_metas=['built-in', 'union', 'struct', 'enum']) qtype = find_alternate_member_qtype(value) - assert qtype + if not qtype: + raise QAPIExprError(expr_info, + "Alternate '%s' member '%s' cannot use " + "type '%s'" % (name, key, value)) if qtype in types_seen: raise QAPIExprError(expr_info, "Alternate '%s' member '%s' can't " diff --git a/tests/Makefile b/tests/Makefile index 839d357..04e34b5 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -242,6 +242,7 @@ check-qtest-xtensaeb-y = $(check-qtest-xtensa-y) check-qtest-generic-y += tests/qom-test$(EXESUF) +qapi-schema += alternate-any.json qapi-schema += alternate-array.json qapi-schema += alternate-base.json qapi-schema += alternate-clash.json diff --git a/tests/qapi-schema/alternate-any.err b/tests/qapi-schema/alternate-any.err new file mode 100644 index 0000000..aaa0154 --- /dev/null +++ b/tests/qapi-schema/alternate-any.err @@ -0,0 +1 @@ +tests/qapi-schema/alternate-any.json:2: Alternate 'Alt' member 'one' cannot use type 'any' diff --git a/tests/qapi-schema/alternate-any.exit b/tests/qapi-schema/alternate-any.exit new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/tests/qapi-schema/alternate-any.exit @@ -0,0 +1 @@ +1 diff --git a/tests/qapi-schema/alternate-any.json b/tests/qapi-schema/alternate-any.json new file mode 100644 index 0000000..e47a73a --- /dev/null +++ b/tests/qapi-schema/alternate-any.json @@ -0,0 +1,4 @@ +# we do not allow the 'any' type as an alternate branch +{ 'alternate': 'Alt', + 'data': { 'one': 'any', + 'two': 'int' } } diff --git a/tests/qapi-schema/alternate-any.out b/tests/qapi-schema/alternate-any.out new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/qapi-schema/alternate-any.out |