qapi: Fix error handling code on alternate conflict

The conflict check added by commit c0644771 ("qapi: Reject
alternates that can't work with keyval_parse()") doesn't work
with the following declaration:

{ 'alternate': 'Alt',
'data': { 'one': 'bool',
'two': 'str' } }

It crashes with:

Traceback (most recent call last):
File "./scripts/qapi-types.py", line 295, in <module>
schema = QAPISchema(input_file)
File "/home/ehabkost/rh/proj/virt/qemu/scripts/qapi.py", line 1468, in __init__
self.exprs = check_exprs(parser.exprs)
File "/home/ehabkost/rh/proj/virt/qemu/scripts/qapi.py", line 958, in check_exprs
check_alternate(expr, info)
File "/home/ehabkost/rh/proj/virt/qemu/scripts/qapi.py", line 830, in check_alternate
% (name, key, types_seen[qtype]))
KeyError: 'QTYPE_QSTRING'

This happens because the previously-seen conflicting member
('one') can't be found at types_seen[qtype], but at
types_seen['QTYPE_BOOL'].

Fix the bug by moving the error check to the same loop that adds
new items to types_seen, raising an exception if types_seen[qt]
is already set.

Backports commit fda72ab4510bcc680a3c4fe55997aa29589884f7 from qemu
This commit is contained in:
Eduardo Habkost 2018-03-07 16:57:53 -05:00 committed by Lioncash
parent 49166d71ab
commit 9d412ac4b3
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7

View file

@ -833,11 +833,11 @@ def check_alternate(expr, info):
else:
conflicting.add('QTYPE_QNUM')
conflicting.add('QTYPE_QBOOL')
if conflicting & set(types_seen):
for qt in conflicting:
if qt in types_seen:
raise QAPISemError(info, "Alternate '%s' member '%s' can't "
"be distinguished from member '%s'"
% (name, key, types_seen[qtype]))
for qt in conflicting:
% (name, key, types_seen[qt]))
types_seen[qt] = key