[glib] gdbus-codegen: Handle unexpected XML tags



commit 263ce3042ccf596f5cd59b1defa546df2602597d
Author: David Zeuthen <davidz redhat com>
Date:   Mon May 23 23:22:04 2011 -0400

    gdbus-codegen: Handle unexpected XML tags
    
    This was reported in bug 650874. Add tests.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=650874
    
    Signed-off-by: David Zeuthen <davidz redhat com>

 gio/gdbus-codegen/parser.py |   26 ++++++++++++++++----------
 gio/tests/test-codegen.xml  |   23 +++++++++++++++++++++++
 2 files changed, 39 insertions(+), 10 deletions(-)
---
diff --git a/gio/gdbus-codegen/parser.py b/gio/gdbus-codegen/parser.py
index 25e10ce..a324006 100644
--- a/gio/gdbus-codegen/parser.py
+++ b/gio/gdbus-codegen/parser.py
@@ -35,6 +35,7 @@ class DBusXMLParser:
     STATE_PROPERTY = 'property'
     STATE_ARG = 'arg'
     STATE_ANNOTATION = 'annotation'
+    STATE_IGNORED = 'ignored'
 
     def __init__(self, xml_data):
         self._parser = xml.parsers.expat.ParserCreate()
@@ -129,11 +130,13 @@ class DBusXMLParser:
     def handle_start_element(self, name, attrs):
         old_state = self.state
         old_cur_object = self._cur_object
-        if self.state == DBusXMLParser.STATE_TOP:
+        if self.state == DBusXMLParser.STATE_IGNORED:
+            self.state = DBusXMLParser.STATE_IGNORED
+        elif self.state == DBusXMLParser.STATE_TOP:
             if name == DBusXMLParser.STATE_NODE:
                 self.state = DBusXMLParser.STATE_NODE
             else:
-                raise RuntimeError('Cannot go from state "%s" to element with name "%s"'%(self.state, name))
+                self.state = DBusXMLParser.STATE_IGNORED
         elif self.state == DBusXMLParser.STATE_NODE:
             if name == DBusXMLParser.STATE_INTERFACE:
                 self.state = DBusXMLParser.STATE_INTERFACE
@@ -146,10 +149,10 @@ class DBusXMLParser:
                 self._cur_object.annotations.append(anno)
                 self._cur_object = anno
             else:
-                raise RuntimeError('Cannot go from state "%s" to element with name "%s"'%(self.state, name))
+                self.state = DBusXMLParser.STATE_IGNORED
 
             # assign docs, if any
-            if self.doc_comment_last_symbol == attrs['name']:
+            if attrs.has_key('name') and self.doc_comment_last_symbol == attrs['name']:
                 self._cur_object.doc_string = self.doc_comment_body
                 if self.doc_comment_params.has_key('short_description'):
                     short_description = self.doc_comment_params['short_description']
@@ -179,7 +182,7 @@ class DBusXMLParser:
                 self._cur_object.annotations.append(anno)
                 self._cur_object = anno
             else:
-                raise RuntimeError('Cannot go from state "%s" to element with name "%s"'%(self.state, name))
+                self.state = DBusXMLParser.STATE_IGNORED
 
             # assign docs, if any
             if attrs.has_key('name') and self.doc_comment_last_symbol == attrs['name']:
@@ -208,7 +211,7 @@ class DBusXMLParser:
                 self._cur_object.annotations.append(anno)
                 self._cur_object = anno
             else:
-                raise RuntimeError('Cannot go from state "%s" to element with name "%s"'%(self.state, name))
+                self.state = DBusXMLParser.STATE_IGNORED
 
             # assign docs, if any
             if self.doc_comment_last_symbol == old_cur_object.name:
@@ -234,7 +237,7 @@ class DBusXMLParser:
                 self._cur_object.annotations.append(anno)
                 self._cur_object = anno
             else:
-                raise RuntimeError('Cannot go from state "%s" to element with name "%s"'%(self.state, name))
+                self.state = DBusXMLParser.STATE_IGNORED
 
             # assign docs, if any
             if self.doc_comment_last_symbol == old_cur_object.name:
@@ -252,7 +255,8 @@ class DBusXMLParser:
                 self._cur_object.annotations.append(anno)
                 self._cur_object = anno
             else:
-                raise RuntimeError('Cannot go from state "%s" to element with name "%s"'%(self.state, name))
+                self.state = DBusXMLParser.STATE_IGNORED
+
         elif self.state == DBusXMLParser.STATE_ARG:
             if name == DBusXMLParser.STATE_ANNOTATION:
                 self.state = DBusXMLParser.STATE_ANNOTATION
@@ -260,7 +264,8 @@ class DBusXMLParser:
                 self._cur_object.annotations.append(anno)
                 self._cur_object = anno
             else:
-                raise RuntimeError('Cannot go from state "%s" to element with name "%s"'%(self.state, name))
+                self.state = DBusXMLParser.STATE_IGNORED
+
         elif self.state == DBusXMLParser.STATE_ANNOTATION:
             if name == DBusXMLParser.STATE_ANNOTATION:
                 self.state = DBusXMLParser.STATE_ANNOTATION
@@ -268,7 +273,8 @@ class DBusXMLParser:
                 self._cur_object.annotations.append(anno)
                 self._cur_object = anno
             else:
-                raise RuntimeError('Cannot go from state "%s" to element with name "%s"'%(self.state, name))
+                self.state = DBusXMLParser.STATE_IGNORED
+
         else:
             raise RuntimeError('Unhandled state "%s" while entering element with name "%s"'%(self.state, name))
 
diff --git a/gio/tests/test-codegen.xml b/gio/tests/test-codegen.xml
index 84518a3..8b8cac2 100644
--- a/gio/tests/test-codegen.xml
+++ b/gio/tests/test-codegen.xml
@@ -444,4 +444,27 @@
     </property>
   </interface>
 
+  <!-- ensure we don't choke on unknown/unexpected XML tags or unknown/unexpected attribyutes (#650874) -->
+  <interface name="UnknownXmlTags" unexpected="boo">
+    <someUnknownTag>
+      <anotherTagWeIgnore>yadaydaydaydayda</anotherTagWeIgnore>
+    </someUnknownTag>
+    <method name="CanSetTimezone" also_unexpected="1">
+      <annotation name="org.freedesktop.DBus.GLib.Async" value="" also_unexpected="1">
+        <unknownTag/>
+      </annotation>
+      <arg name="value" direction="out" type="i" also_unexpected="1">
+        <unknownTag/>
+      </arg>
+      <unknownTag/>
+    </method>
+    <signal name="SomeSignal" also_unexpected="1">
+      <unknownTag/>
+    </signal>
+    <property name="SomeProperty" type="s" access="readwrite" also_unexpected="1">
+      <unknownTag/>
+    </property>
+  </interface>
+  <unknownTag/>
+
 </node>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]