提交 46534309 编写于 作者: E Eric Blake 提交者: Markus Armbruster

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: NEric Blake <eblake@redhat.com>
Message-Id: <1455778109-6278-4-git-send-email-eblake@redhat.com>
[Commit message tweaked]
Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
上级 02a57ae3
...@@ -629,7 +629,10 @@ def check_alternate(expr, expr_info): ...@@ -629,7 +629,10 @@ def check_alternate(expr, expr_info):
value, value,
allow_metas=['built-in', 'union', 'struct', 'enum']) allow_metas=['built-in', 'union', 'struct', 'enum'])
qtype = find_alternate_member_qtype(value) 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: if qtype in types_seen:
raise QAPIExprError(expr_info, raise QAPIExprError(expr_info,
"Alternate '%s' member '%s' can't " "Alternate '%s' member '%s' can't "
......
...@@ -241,6 +241,7 @@ check-qtest-xtensaeb-y = $(check-qtest-xtensa-y) ...@@ -241,6 +241,7 @@ check-qtest-xtensaeb-y = $(check-qtest-xtensa-y)
check-qtest-generic-y += tests/qom-test$(EXESUF) check-qtest-generic-y += tests/qom-test$(EXESUF)
qapi-schema += alternate-any.json
qapi-schema += alternate-array.json qapi-schema += alternate-array.json
qapi-schema += alternate-base.json qapi-schema += alternate-base.json
qapi-schema += alternate-clash.json qapi-schema += alternate-clash.json
......
tests/qapi-schema/alternate-any.json:2: Alternate 'Alt' member 'one' cannot use type 'any'
# we do not allow the 'any' type as an alternate branch
{ 'alternate': 'Alt',
'data': { 'one': 'any',
'two': 'int' } }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册