提交 6974ccd5 编写于 作者: M Markus Armbruster 提交者: Anthony Liguori

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: NMarkus Armbruster <armbru@redhat.com>
Reviewed-by: NEric Blake <eblake@redhat.com>
Message-id: 1374939721-7876-7-git-send-email-armbru@redhat.com
Signed-off-by: NAnthony Liguori <aliguori@us.ibm.com>
上级 9213aa53
......@@ -106,24 +106,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() # :
expr[key] = self.get_expr()
if self.tok == ',':
if self.tok != ':':
raise QAPISchemaError(self, 'Expected ":"')
self.accept()
expr[key] = self.get_expr()
if self.tok == '}':
self.accept()
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 != ']':
expr.append(self.get_expr())
if 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 == ']':
self.accept()
return expr
if self.tok != ',':
raise QAPISchemaError(self, 'Expected "," or "]"')
self.accept()
def get_expr(self):
if self.tok == '{':
......@@ -132,9 +150,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):
......
[OrderedDict([('enum', None), ('data', ['good', 'bad', 'ugly'])])]
[None]
[]
[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
['Status']
[]
[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
['Status']
[]
[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
['Status']
[]
[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
['Status']
[]
Crashed: <type 'exceptions.IndexError'>
<stdin>:1:20: Expected "," or "]"
Crashed: <type 'exceptions.IndexError'>
<stdin>:1:21: Expected "," or "}"
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册