[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v5 07/17] qapi/expr.py: Check type of union and alternate 'data'
From: |
John Snow |
Subject: |
[PATCH v5 07/17] qapi/expr.py: Check type of union and alternate 'data' member |
Date: |
Wed, 21 Apr 2021 14:20:22 -0400 |
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>
---
scripts/qapi/expr.py | 7 +++++++
tests/qapi-schema/alternate-data-invalid.err | 2 ++
tests/qapi-schema/alternate-data-invalid.json | 4 ++++
tests/qapi-schema/alternate-data-invalid.out | 0
tests/qapi-schema/meson.build | 2 ++
tests/qapi-schema/union-invalid-data.err | 2 ++
tests/qapi-schema/union-invalid-data.json | 6 ++++++
tests/qapi-schema/union-invalid-data.out | 0
8 files changed, 23 insertions(+)
create mode 100644 tests/qapi-schema/alternate-data-invalid.err
create mode 100644 tests/qapi-schema/alternate-data-invalid.json
create mode 100644 tests/qapi-schema/alternate-data-invalid.out
create mode 100644 tests/qapi-schema/union-invalid-data.err
create mode 100644 tests/qapi-schema/union-invalid-data.json
create mode 100644 tests/qapi-schema/union-invalid-data.out
diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py
index c0d18dcc018..03624bdf3f3 100644
--- a/scripts/qapi/expr.py
+++ b/scripts/qapi/expr.py
@@ -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)
diff --git a/tests/qapi-schema/alternate-data-invalid.err
b/tests/qapi-schema/alternate-data-invalid.err
new file mode 100644
index 00000000000..55f1033aef5
--- /dev/null
+++ b/tests/qapi-schema/alternate-data-invalid.err
@@ -0,0 +1,2 @@
+alternate-data-invalid.json: In alternate 'Alt':
+alternate-data-invalid.json:2: 'data' must be an object
diff --git a/tests/qapi-schema/alternate-data-invalid.json
b/tests/qapi-schema/alternate-data-invalid.json
new file mode 100644
index 00000000000..7d5d9055811
--- /dev/null
+++ b/tests/qapi-schema/alternate-data-invalid.json
@@ -0,0 +1,4 @@
+# Alternate type requires an object for 'data'
+{ 'alternate': 'Alt',
+ 'data': ['rubbish', 'nonsense']
+}
diff --git a/tests/qapi-schema/alternate-data-invalid.out
b/tests/qapi-schema/alternate-data-invalid.out
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/tests/qapi-schema/meson.build b/tests/qapi-schema/meson.build
index 8ba69171329..d7163e6601c 100644
--- a/tests/qapi-schema/meson.build
+++ b/tests/qapi-schema/meson.build
@@ -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',
diff --git a/tests/qapi-schema/union-invalid-data.err
b/tests/qapi-schema/union-invalid-data.err
new file mode 100644
index 00000000000..e26cf769a3e
--- /dev/null
+++ b/tests/qapi-schema/union-invalid-data.err
@@ -0,0 +1,2 @@
+union-invalid-data.json: In union 'TestUnion':
+union-invalid-data.json:2: 'data' must be an object
diff --git a/tests/qapi-schema/union-invalid-data.json
b/tests/qapi-schema/union-invalid-data.json
new file mode 100644
index 00000000000..395ba24d395
--- /dev/null
+++ b/tests/qapi-schema/union-invalid-data.json
@@ -0,0 +1,6 @@
+# the union data type must be an object.
+{ 'union': 'TestUnion',
+ 'base': 'int',
+ 'discriminator': 'int',
+ 'data': ['rubbish', 'nonsense']
+}
diff --git a/tests/qapi-schema/union-invalid-data.out
b/tests/qapi-schema/union-invalid-data.out
new file mode 100644
index 00000000000..e69de29bb2d
--
2.30.2
- [PATCH v5 00/17] qapi: static typing conversion, pt3, John Snow, 2021/04/21
- [PATCH v5 01/17] qapi/expr: Comment cleanup, John Snow, 2021/04/21
- [PATCH v5 05/17] qapi/expr.py: Add assertion for union type 'check_dict', John Snow, 2021/04/21
- [PATCH v5 03/17] qapi/expr.py: Check for dict instead of OrderedDict, John Snow, 2021/04/21
- [PATCH v5 06/17] qapi/expr.py: move string check upwards in check_type, John Snow, 2021/04/21
- [PATCH v5 12/17] qapi/expr.py: Remove single-letter variable, John Snow, 2021/04/21
- [PATCH v5 10/17] qapi/expr.py: add type hint annotations, John Snow, 2021/04/21
- [PATCH v5 07/17] qapi/expr.py: Check type of union and alternate 'data' member,
John Snow <=
- [PATCH v5 13/17] qapi/expr.py: enable pylint checks, John Snow, 2021/04/21
- [PATCH v5 09/17] qapi/expr.py: Modify check_keys to accept any Collection, John Snow, 2021/04/21
- [PATCH v5 04/17] qapi/expr.py: constrain incoming expression types, John Snow, 2021/04/21
- [PATCH v5 11/17] qapi/expr.py: Consolidate check_if_str calls in check_if, John Snow, 2021/04/21
- [PATCH v5 16/17] qapi/expr.py: Use tuples instead of lists for static data, John Snow, 2021/04/21
- [PATCH v5 02/17] qapi/expr.py: Remove 'info' argument from nested check_if_str, John Snow, 2021/04/21
- [PATCH v5 08/17] qapi/expr.py: Add casts in a few select cases, John Snow, 2021/04/21
- [PATCH v5 15/17] qapi/expr.py: Add docstrings, John Snow, 2021/04/21
- [PATCH v5 17/17] qapi/expr: Update authorship and copyright information, John Snow, 2021/04/21
- [PATCH v5 14/17] qapi/expr: Only explicitly prohibit 'Kind' nor 'List' for type names, John Snow, 2021/04/21