mirror of
https://github.com/qemu/qemu.git
synced 2024-11-24 03:13:44 +08:00
qapi/expr.py: Check type of union and alternate 'data' member
Prior to this commit, specifying a non-object value here causes the QAPI parser to crash in expr.py with a stack trace with (likely) an AttributeError when we attempt to call that value's items() method. This member needs to be an object (Dict), and not anything else. Add a check for this with a nicer error message, and formalize that check with new test cases that exercise that error. Signed-off-by: John Snow <jsnow@redhat.com> Message-Id: <20210421182032.3521476-8-jsnow@redhat.com> Reviewed-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Markus Armbruster <armbru@redhat.com>
This commit is contained in:
parent
926bb8add7
commit
4918bb7def
@ -283,6 +283,9 @@ def check_union(expr, info):
|
||||
raise QAPISemError(info, "'discriminator' requires 'base'")
|
||||
check_name_is_str(discriminator, info, "'discriminator'")
|
||||
|
||||
if not isinstance(members, dict):
|
||||
raise QAPISemError(info, "'data' must be an object")
|
||||
|
||||
for (key, value) in members.items():
|
||||
source = "'data' member '%s'" % key
|
||||
if discriminator is None:
|
||||
@ -298,6 +301,10 @@ def check_alternate(expr, info):
|
||||
|
||||
if not members:
|
||||
raise QAPISemError(info, "'data' must not be empty")
|
||||
|
||||
if not isinstance(members, dict):
|
||||
raise QAPISemError(info, "'data' must be an object")
|
||||
|
||||
for (key, value) in members.items():
|
||||
source = "'data' member '%s'" % key
|
||||
check_name_lower(key, info, source)
|
||||
|
2
tests/qapi-schema/alternate-data-invalid.err
Normal file
2
tests/qapi-schema/alternate-data-invalid.err
Normal file
@ -0,0 +1,2 @@
|
||||
alternate-data-invalid.json: In alternate 'Alt':
|
||||
alternate-data-invalid.json:2: 'data' must be an object
|
4
tests/qapi-schema/alternate-data-invalid.json
Normal file
4
tests/qapi-schema/alternate-data-invalid.json
Normal file
@ -0,0 +1,4 @@
|
||||
# Alternate type requires an object for 'data'
|
||||
{ 'alternate': 'Alt',
|
||||
'data': ['rubbish', 'nonsense']
|
||||
}
|
0
tests/qapi-schema/alternate-data-invalid.out
Normal file
0
tests/qapi-schema/alternate-data-invalid.out
Normal file
@ -14,6 +14,7 @@ schemas = [
|
||||
'alternate-conflict-string.json',
|
||||
'alternate-conflict-bool-string.json',
|
||||
'alternate-conflict-num-string.json',
|
||||
'alternate-data-invalid.json',
|
||||
'alternate-empty.json',
|
||||
'alternate-invalid-dict.json',
|
||||
'alternate-nested.json',
|
||||
@ -192,6 +193,7 @@ schemas = [
|
||||
'union-clash-branches.json',
|
||||
'union-empty.json',
|
||||
'union-invalid-base.json',
|
||||
'union-invalid-data.json',
|
||||
'union-optional-branch.json',
|
||||
'union-unknown.json',
|
||||
'unknown-escape.json',
|
||||
|
2
tests/qapi-schema/union-invalid-data.err
Normal file
2
tests/qapi-schema/union-invalid-data.err
Normal file
@ -0,0 +1,2 @@
|
||||
union-invalid-data.json: In union 'TestUnion':
|
||||
union-invalid-data.json:2: 'data' must be an object
|
6
tests/qapi-schema/union-invalid-data.json
Normal file
6
tests/qapi-schema/union-invalid-data.json
Normal file
@ -0,0 +1,6 @@
|
||||
# the union data type must be an object.
|
||||
{ 'union': 'TestUnion',
|
||||
'base': 'int',
|
||||
'discriminator': 'int',
|
||||
'data': ['rubbish', 'nonsense']
|
||||
}
|
0
tests/qapi-schema/union-invalid-data.out
Normal file
0
tests/qapi-schema/union-invalid-data.out
Normal file
Loading…
Reference in New Issue
Block a user