Skip to contentSkip to conversation

qapi: Fix error handling code on alternate conflict

PATCH In review Eduardo Habkost 3 participants 5 messages Jul 17, 2017 – Jul 20, 2017

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 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.

Add two additional test cases that can detect the bug.

---
Changes v1 -> v2:
* Fix mistake on *.err files (wrong line number on error message)

---
 scripts/qapi.py                                       | 8 ++++----
 tests/Makefile.include                                | 2 ++
 tests/qapi-schema/alternate-conflict-bool-string.err  | 1 +
 tests/qapi-schema/alternate-conflict-bool-string.exit | 1 +
 tests/qapi-schema/alternate-conflict-bool-string.json | 4 ++++
 tests/qapi-schema/alternate-conflict-bool-string.out  | 0
 tests/qapi-schema/alternate-conflict-num-string.err   | 1 +
 tests/qapi-schema/alternate-conflict-num-string.exit  | 1 +
 tests/qapi-schema/alternate-conflict-num-string.json  | 4 ++++
 tests/qapi-schema/alternate-conflict-num-string.out   | 0
 tests/qapi-schema/alternate-multi-conflict.json       | 0
 11 files changed, 18 insertions(+), 4 deletions(-)
 create mode 100644 tests/qapi-schema/alternate-conflict-bool-string.err
 create mode 100644 tests/qapi-schema/alternate-conflict-bool-string.exit
 create mode 100644 tests/qapi-schema/alternate-conflict-bool-string.json
 create mode 100644 tests/qapi-schema/alternate-conflict-bool-string.out
 create mode 100644 tests/qapi-schema/alternate-conflict-num-string.err
 create mode 100644 tests/qapi-schema/alternate-conflict-num-string.exit
 create mode 100644 tests/qapi-schema/alternate-conflict-num-string.json
 create mode 100644 tests/qapi-schema/alternate-conflict-num-string.out
 create mode 100644 tests/qapi-schema/alternate-multi-conflict.json
scripts/qapi.py +4 -4
index 84e2eb4..a9c12e5 100644
@@ -824,11 +824,11 @@ def check_alternate(expr, info):
             else:
                 conflicting.add('QTYPE_QNUM')
                 conflicting.add('QTYPE_QBOOL')
-        if conflicting & set(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:
+            if qt in types_seen:
+                raise QAPISemError(info, "Alternate '%s' member '%s' can't "
+                                   "be distinguished from member '%s'"
+                                   % (name, key, types_seen[qt]))
             types_seen[qt] = key
 
 
tests/Makefile.include +2 -0
index cfbb689..16d7a3a 100644
@@ -346,6 +346,8 @@ qapi-schema += alternate-conflict-dict.json
 qapi-schema += alternate-conflict-enum-bool.json
 qapi-schema += alternate-conflict-enum-int.json
 qapi-schema += alternate-conflict-string.json
+qapi-schema += alternate-conflict-bool-string.json
+qapi-schema += alternate-conflict-num-string.json
 qapi-schema += alternate-empty.json
 qapi-schema += alternate-nested.json
 qapi-schema += alternate-unknown.json
tests/qapi-schema/alternate-conflict-bool-string.err new file +1 -0
new file mode 100644
index 0000000..e52fee7
@@ -0,0 +1 @@
+tests/qapi-schema/alternate-conflict-bool-string.json:2: Alternate 'Alt' member 'two' can't be distinguished from member 'one'
tests/qapi-schema/alternate-conflict-bool-string.exit new file +1 -0
new file mode 100644
index 0000000..d00491f
@@ -0,0 +1 @@
+1
tests/qapi-schema/alternate-conflict-bool-string.json new file +4 -0
new file mode 100644
index 0000000..0544de1
@@ -0,0 +1,4 @@
+# alternate branches of 'str' type conflict with all scalar types
+{ 'alternate': 'Alt',
+  'data': { 'one': 'bool',
+            'two': 'str' } }
tests/qapi-schema/alternate-conflict-bool-string.out new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/alternate-conflict-num-string.err new file +1 -0
new file mode 100644
index 0000000..5ba3827
@@ -0,0 +1 @@
+tests/qapi-schema/alternate-conflict-num-string.json:2: Alternate 'Alt' member 'two' can't be distinguished from member 'one'
tests/qapi-schema/alternate-conflict-num-string.exit new file +1 -0
new file mode 100644
index 0000000..d00491f
@@ -0,0 +1 @@
+1
tests/qapi-schema/alternate-conflict-num-string.json new file +4 -0
new file mode 100644
index 0000000..ae90144
@@ -0,0 +1,4 @@
+# alternate branches of 'str' type conflict with all scalar types
+{ 'alternate': 'Alt',
+  'data': { 'one': 'number',
+            'two': 'str' } }
tests/qapi-schema/alternate-conflict-num-string.out new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/alternate-multi-conflict.json new file +0 -0
new file mode 100644
index 0000000..e69de29
Signature · 1 line
2.9.4
Reviewed-by on v2 1/1
#1 lore raw
Quoted 25 lines · Eduardo Habkost

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'].

Quoted 8 lines

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.

Add two additional test cases that can detect the bug.

---

If this misses tomorrow's freeze deadline, it's borderline for inclusion in 2.10 (it does not change the qemu binary proper, but rather fixes a build tool crash, where the crash is only provoked by bad input and we won't have any bad input at the time of the freeze) - I'll leave it up to Markus on whether to take now or defer to 2.11. But either way:

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 1/1
#2 lore raw
In reply to Eduardo Habkost
Quoted 21 lines · Eduardo Habkost

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'

Quoted 3 lines

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

Good catch.

Quoted 7 lines

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.

Add two additional test cases that can detect the bug.

#3 lore raw
In reply to Eduardo Habkost
Quoted 32 lines · Eduardo Habkost

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.

Add two additional test cases that can detect the bug.

[...]

tests/qapi-schema/alternate-multi-conflict.json

Quoted 6 lines
tests/qapi-schema/alternate-conflict-num-string.out new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/alternate-multi-conflict.json new file +0 -0
new file mode 100644
index 0000000..e69de29

Accident? Can drop on commit.

#4 lore raw
Quoted 35 lines · Markus Armbruster
Quoted 32 lines · Eduardo Habkost

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.

Add two additional test cases that can detect the bug.

[...]

Quoted 8 lines

tests/qapi-schema/alternate-multi-conflict.json

Quoted 6 lines
tests/qapi-schema/alternate-conflict-num-string.out new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/alternate-multi-conflict.json new file +0 -0
new file mode 100644
index 0000000..e69de29

Accident? Can drop on commit.

Yes, sorry. Thanks for spotting it.

Signature · 1 line
Eduardo