Browse Source

qapi.py: Fix schema parser to check syntax systematically

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>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-id: 1374939721-7876-7-git-send-email-armbru@redhat.com
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Markus Armbruster 12 years ago
parent
commit
6974ccd542

+ 30 - 10
scripts/qapi.py

@@ -106,24 +106,42 @@ def accept(self):
 
     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 == '{':
@@ -132,9 +150,11 @@ def get_expr(self):
         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):

+ 1 - 0
tests/qapi-schema/missing-colon.err

@@ -0,0 +1 @@
+<stdin>:1:10: Expected ":"

+ 1 - 1
tests/qapi-schema/missing-colon.exit

@@ -1 +1 @@
-0
+1

+ 0 - 3
tests/qapi-schema/missing-colon.out

@@ -1,3 +0,0 @@
-[OrderedDict([('enum', None), ('data', ['good', 'bad', 'ugly'])])]
-[None]
-[]

+ 1 - 0
tests/qapi-schema/missing-comma-list.err

@@ -0,0 +1 @@
+<stdin>:2:20: Expected "," or "]"

+ 1 - 1
tests/qapi-schema/missing-comma-list.exit

@@ -1 +1 @@
-0
+1

+ 0 - 3
tests/qapi-schema/missing-comma-list.out

@@ -1,3 +0,0 @@
-[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
-['Status']
-[]

+ 1 - 0
tests/qapi-schema/missing-comma-object.err

@@ -0,0 +1 @@
+<stdin>:2:3: Expected "," or "}"

+ 1 - 1
tests/qapi-schema/missing-comma-object.exit

@@ -1 +1 @@
-0
+1

+ 0 - 3
tests/qapi-schema/missing-comma-object.out

@@ -1,3 +0,0 @@
-[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
-['Status']
-[]

+ 1 - 0
tests/qapi-schema/trailing-comma-list.err

@@ -0,0 +1 @@
+<stdin>:2:36: Expected "{", "[" or string

+ 1 - 1
tests/qapi-schema/trailing-comma-list.exit

@@ -1 +1 @@
-0
+1

+ 0 - 3
tests/qapi-schema/trailing-comma-list.out

@@ -1,3 +0,0 @@
-[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
-['Status']
-[]

+ 1 - 0
tests/qapi-schema/trailing-comma-object.err

@@ -0,0 +1 @@
+<stdin>:2:38: Expected string

+ 1 - 1
tests/qapi-schema/trailing-comma-object.exit

@@ -1 +1 @@
-0
+1

+ 0 - 3
tests/qapi-schema/trailing-comma-object.out

@@ -1,3 +0,0 @@
-[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
-['Status']
-[]

+ 1 - 1
tests/qapi-schema/unclosed-list.err

@@ -1 +1 @@
-Crashed: <type 'exceptions.IndexError'>
+<stdin>:1:20: Expected "," or "]"

+ 1 - 1
tests/qapi-schema/unclosed-object.err

@@ -1 +1 @@
-Crashed: <type 'exceptions.IndexError'>
+<stdin>:1:21: Expected "," or "}"