[librsvg: 13/16] Call xmlStopParser() when the start_element callback returns an error



commit 74f2ca0d8768d4a91344f6135536be40085fcb5f
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri Oct 11 15:59:11 2019 -0500

    Call xmlStopParser() when the start_element callback returns an error
    
    This adds the ability for start_element() to return a Result.
    
    We'll use this to implement limits on loaded elements.

 rsvg_internals/src/xml.rs       | 6 ++++--
 rsvg_internals/src/xml2.rs      | 2 ++
 rsvg_internals/src/xml2_load.rs | 7 ++++++-
 3 files changed, 12 insertions(+), 3 deletions(-)
---
diff --git a/rsvg_internals/src/xml.rs b/rsvg_internals/src/xml.rs
index 1853d677..b7d6b80e 100644
--- a/rsvg_internals/src/xml.rs
+++ b/rsvg_internals/src/xml.rs
@@ -142,11 +142,11 @@ impl XmlState {
         }
     }
 
-    pub fn start_element(&self, name: &str, pbag: &PropertyBag) {
+    pub fn start_element(&self, name: &str, pbag: &PropertyBag) -> Result<(), ()> {
         let context = self.inner.borrow().context();
 
         if let Context::FatalError = context {
-            return;
+            return Err(());
         }
 
         // FIXME: we should deal with namespaces at some point
@@ -165,6 +165,8 @@ impl XmlState {
         };
 
         self.inner.borrow_mut().context_stack.push(new_context);
+
+        Ok(())
     }
 
     pub fn end_element(&self, _name: &str) {
diff --git a/rsvg_internals/src/xml2.rs b/rsvg_internals/src/xml2.rs
index 4ad7a652..a2afb7dd 100644
--- a/rsvg_internals/src/xml2.rs
+++ b/rsvg_internals/src/xml2.rs
@@ -174,6 +174,8 @@ extern "C" {
         enc: xmlCharEncoding,
     ) -> xmlParserCtxtPtr;
 
+    pub fn xmlStopParser(ctxt: xmlParserCtxtPtr);
+
     pub fn xmlParseDocument(ctxt: xmlParserCtxtPtr) -> libc::c_int;
 
     pub fn xmlFreeDoc(doc: xmlDocPtr);
diff --git a/rsvg_internals/src/xml2_load.rs b/rsvg_internals/src/xml2_load.rs
index bb3b98d7..72e167f1 100644
--- a/rsvg_internals/src/xml2_load.rs
+++ b/rsvg_internals/src/xml2_load.rs
@@ -160,7 +160,12 @@ unsafe extern "C" fn sax_start_element_cb(
 
     let pbag = PropertyBag::new_from_key_value_pairs(atts);
 
-    xml2_parser.state.start_element(name, &pbag);
+    if let Err(e) = xml2_parser.state.start_element(name, &pbag) {
+        let _: () = e; // guard in case we change the error type later
+
+        let parser = xml2_parser.parser.get();
+        xmlStopParser(parser);
+    }
 }
 
 unsafe extern "C" fn sax_end_element_cb(user_data: *mut libc::c_void, name: *const libc::c_char) {


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