qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Cc: aliguori@us.ibm.com, akong@redhat.com, mdroth@linux.vnet.ibm.com
Subject: [Qemu-devel] [PATCH 6/9] qapi.py: Fix schema parser to check syntax systematically
Date: Fri, 26 Jul 2013 14:39:44 +0200	[thread overview]
Message-ID: <1374842387-17146-7-git-send-email-armbru@redhat.com> (raw)
In-Reply-To: <1374842387-17146-1-git-send-email-armbru@redhat.com>

Fixes at least the following parser bugs:

* accepts any token in place of a colon

* treats comma as optional

* crashes when closing braces or brackets are missing

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 scripts/qapi.py                       | 40 ++++++++++++++++++++++++++---------
 tests/qapi-schema/missing-colon.err   |  1 +
 tests/qapi-schema/missing-colon.exit  |  2 +-
 tests/qapi-schema/missing-colon.out   |  3 ---
 tests/qapi-schema/missing-comma.err   |  1 +
 tests/qapi-schema/missing-comma.exit  |  2 +-
 tests/qapi-schema/missing-comma.out   |  3 ---
 tests/qapi-schema/unclosed-object.err |  2 +-
 8 files changed, 35 insertions(+), 19 deletions(-)

diff --git a/scripts/qapi.py b/scripts/qapi.py
index f72a0e3..a7feccb 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -91,24 +91,42 @@ class QAPISchema:
 
     def get_members(self):
         expr = OrderedDict()
-        while self.tok != '}':
+        if self.tok == '}':
+            self.accept()
+            return expr
+        if self.tok != "'":
+            raise QAPISchemaError(self, 'Expected string or "}"')
+        while True:
             key = self.val
             self.accept()
-            self.accept()        # :
+            if self.tok != ':':
+                raise QAPISchemaError(self, 'Expected ":"')
+            self.accept()
             expr[key] = self.get_expr()
-            if self.tok == ',':
+            if self.tok == '}':
                 self.accept()
-        self.accept()
-        return expr
+                return expr
+            if self.tok != ',':
+                raise QAPISchemaError(self, 'Expected "," or "}"')
+            self.accept()
+            if self.tok != "'":
+                raise QAPISchemaError(self, 'Expected string')
 
     def get_values(self):
         expr = []
-        while self.tok != ']':
+        if self.tok == ']':
+            self.accept()
+            return expr
+        if not self.tok in [ '{', '[', "'" ]:
+            raise QAPISchemaError(self, 'Expected "{", "[", "]" or string')
+        while True:
             expr.append(self.get_expr())
-            if self.tok == ',':
+            if self.tok == ']':
                 self.accept()
-        self.accept()
-        return expr
+                return expr
+            if self.tok != ',':
+                raise QAPISchemaError(self, 'Expected "," or "]"')
+            self.accept()
 
     def get_expr(self):
         if self.tok == '{':
@@ -117,9 +135,11 @@ class QAPISchema:
         elif self.tok == '[':
             self.accept()
             expr = self.get_values()
-        else:
+        elif self.tok == "'":
             expr = self.val
             self.accept()
+        else:
+            raise QAPISchemaError(self, 'Expected "{", "[" or string')
         return expr
 
 def parse_schema(fp):
diff --git a/tests/qapi-schema/missing-colon.err b/tests/qapi-schema/missing-colon.err
index e69de29..9f2a355 100644
--- a/tests/qapi-schema/missing-colon.err
+++ b/tests/qapi-schema/missing-colon.err
@@ -0,0 +1 @@
+<stdin>:1:10: Expected ":"
diff --git a/tests/qapi-schema/missing-colon.exit b/tests/qapi-schema/missing-colon.exit
index 573541a..d00491f 100644
--- a/tests/qapi-schema/missing-colon.exit
+++ b/tests/qapi-schema/missing-colon.exit
@@ -1 +1 @@
-0
+1
diff --git a/tests/qapi-schema/missing-colon.out b/tests/qapi-schema/missing-colon.out
index e67068c..e69de29 100644
--- a/tests/qapi-schema/missing-colon.out
+++ b/tests/qapi-schema/missing-colon.out
@@ -1,3 +0,0 @@
-[OrderedDict([('enum', None), ('data', ['good', 'bad', 'ugly'])])]
-[None]
-[]
diff --git a/tests/qapi-schema/missing-comma.err b/tests/qapi-schema/missing-comma.err
index e69de29..b0121b5 100644
--- a/tests/qapi-schema/missing-comma.err
+++ b/tests/qapi-schema/missing-comma.err
@@ -0,0 +1 @@
+<stdin>:2:3: Expected "," or "}"
diff --git a/tests/qapi-schema/missing-comma.exit b/tests/qapi-schema/missing-comma.exit
index 573541a..d00491f 100644
--- a/tests/qapi-schema/missing-comma.exit
+++ b/tests/qapi-schema/missing-comma.exit
@@ -1 +1 @@
-0
+1
diff --git a/tests/qapi-schema/missing-comma.out b/tests/qapi-schema/missing-comma.out
index e3bd904..e69de29 100644
--- a/tests/qapi-schema/missing-comma.out
+++ b/tests/qapi-schema/missing-comma.out
@@ -1,3 +0,0 @@
-[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
-['Status']
-[]
diff --git a/tests/qapi-schema/unclosed-object.err b/tests/qapi-schema/unclosed-object.err
index f9a9c2a..cc11626 100644
--- a/tests/qapi-schema/unclosed-object.err
+++ b/tests/qapi-schema/unclosed-object.err
@@ -1 +1 @@
-Crashed: <type 'exceptions.IndexError'>
+<stdin>:1:19: Expected "," or "]"
-- 
1.7.11.7

  parent reply	other threads:[~2013-07-26 12:40 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-26 12:39 [Qemu-devel] [PATCH 0/9] Our QAPI parser is a hack, replace it Markus Armbruster
2013-07-26 12:39 ` [Qemu-devel] [PATCH 1/9] tests: QAPI schema parser tests Markus Armbruster
2013-07-26 12:48   ` Eric Blake
2013-07-26 14:16     ` Markus Armbruster
2013-07-26 14:57       ` Eric Blake
2013-07-26 15:31         ` Markus Armbruster
2013-07-26 12:39 ` [Qemu-devel] [PATCH 2/9] tests: Use qapi-schema-test.json as schema parser test Markus Armbruster
2013-07-26 13:17   ` Eric Blake
2013-07-27 15:34     ` Markus Armbruster
2013-07-26 12:39 ` [Qemu-devel] [PATCH 3/9] qapi.py: Restructure lexer and parser Markus Armbruster
2013-07-26 13:54   ` Eric Blake
2013-07-26 12:39 ` [Qemu-devel] [PATCH 4/9] qapi.py: Decent syntax error reporting Markus Armbruster
2013-07-26 15:30   ` Eric Blake
2013-07-26 19:33     ` Markus Armbruster
2013-07-26 19:48       ` Paolo Bonzini
2013-07-26 19:57         ` Eric Blake
2013-07-26 12:39 ` [Qemu-devel] [PATCH 5/9] qapi.py: Reject invalid characters in schema file Markus Armbruster
2013-07-26 15:32   ` Eric Blake
2013-07-26 12:39 ` Markus Armbruster [this message]
2013-07-26 15:56   ` [Qemu-devel] [PATCH 6/9] qapi.py: Fix schema parser to check syntax systematically Eric Blake
2013-07-26 19:35     ` Markus Armbruster
2013-07-26 19:42       ` Eric Blake
2013-07-26 12:39 ` [Qemu-devel] [PATCH 7/9] qapi.py: Fix diagnosing non-objects at a schema's top-level Markus Armbruster
2013-07-26 16:03   ` Eric Blake
2013-07-26 12:39 ` [Qemu-devel] [PATCH 8/9] qapi.py: Rename expr_eval to expr in parse_schema() Markus Armbruster
2013-07-26 16:13   ` Eric Blake
2013-07-26 12:39 ` [Qemu-devel] [PATCH 9/9] qapi.py: Permit comments starting anywhere on the line Markus Armbruster
2013-07-26 16:15   ` Eric Blake
2013-07-26 14:41 ` [Qemu-devel] [PATCH 0/9] Our QAPI parser is a hack, replace it Anthony Liguori
2013-07-26 15:36   ` Markus Armbruster
2013-07-26 17:47     ` Anthony Liguori
2013-07-26 19:48       ` Markus Armbruster

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1374842387-17146-7-git-send-email-armbru@redhat.com \
    --to=armbru@redhat.com \
    --cc=akong@redhat.com \
    --cc=aliguori@us.ibm.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).