From 4b35991a3bd5f9e03333d5b1bd4a7bcf9941aac5 Mon Sep 17 00:00:00 2001 From: Wenchao Xia Date: Tue, 4 Mar 2014 18:44:32 -0800 Subject: [PATCH] qapi script: add check for duplicated key It is bad that same key was specified twice, especially when a union has two branches with same condition. This patch can prevent it. Signed-off-by: Wenchao Xia Reviewed-by: Eric Blake Reviewed-by: Markus Armbruster Signed-off-by: Luiz Capitulino --- scripts/qapi.py | 2 ++ tests/Makefile | 3 ++- tests/qapi-schema/duplicate-key.err | 1 + tests/qapi-schema/duplicate-key.exit | 1 + tests/qapi-schema/duplicate-key.json | 2 ++ tests/qapi-schema/duplicate-key.out | 0 6 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 tests/qapi-schema/duplicate-key.err create mode 100644 tests/qapi-schema/duplicate-key.exit create mode 100644 tests/qapi-schema/duplicate-key.json create mode 100644 tests/qapi-schema/duplicate-key.out diff --git a/scripts/qapi.py b/scripts/qapi.py index 023930ea0c..d0e793452a 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -116,6 +116,8 @@ class QAPISchema: if self.tok != ':': raise QAPISchemaError(self, 'Expected ":"') self.accept() + if key in expr: + raise QAPISchemaError(self, 'Duplicate key "%s"' % key) expr[key] = self.get_expr(True) if self.tok == '}': self.accept() diff --git a/tests/Makefile b/tests/Makefile index b17d41e4e2..dfe06eb1f4 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -142,7 +142,8 @@ check-qapi-schema-y := $(addprefix tests/qapi-schema/, \ missing-comma-object.json non-objects.json \ qapi-schema-test.json quoted-structural-chars.json \ trailing-comma-list.json trailing-comma-object.json \ - unclosed-list.json unclosed-object.json unclosed-string.json) + unclosed-list.json unclosed-object.json unclosed-string.json \ + duplicate-key.json) GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h tests/test-qmp-commands.h diff --git a/tests/qapi-schema/duplicate-key.err b/tests/qapi-schema/duplicate-key.err new file mode 100644 index 0000000000..0801c6a9bb --- /dev/null +++ b/tests/qapi-schema/duplicate-key.err @@ -0,0 +1 @@ +:2:10: Duplicate key "key" diff --git a/tests/qapi-schema/duplicate-key.exit b/tests/qapi-schema/duplicate-key.exit new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/tests/qapi-schema/duplicate-key.exit @@ -0,0 +1 @@ +1 diff --git a/tests/qapi-schema/duplicate-key.json b/tests/qapi-schema/duplicate-key.json new file mode 100644 index 0000000000..1b55d88107 --- /dev/null +++ b/tests/qapi-schema/duplicate-key.json @@ -0,0 +1,2 @@ +{ 'key': 'value', + 'key': 'value' } diff --git a/tests/qapi-schema/duplicate-key.out b/tests/qapi-schema/duplicate-key.out new file mode 100644 index 0000000000..e69de29bb2 -- 2.11.0