[librsvg] NodeSvg: Only access self.atts if it is non-null



commit 6c3ea8ee997b5e2ea3d40387c45b9a1ef2b3e847
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Mar 23 08:25:54 2017 -0600

    NodeSvg: Only access self.atts if it is non-null
    
    The contents of self.atts will be null if an <svg> element has no
    attributes.  This is because node_set_atts() in the C code will not call
    a Node.set_atts() method if the property bag is empty.
    
    While that is an optimization to avoid a bunch of hash lookups on empty
    hash tables, it does mean that node implementations cannot assume that
    their set_atts() method is always called.  I'm not sure that I like
    this.  Maybe we should just profile things with/without the extra hash
    lookups and see if they make a difference.

 rust/src/structure.rs |   28 +++++++++++++++++-----------
 1 files changed, 17 insertions(+), 11 deletions(-)
---
diff --git a/rust/src/structure.rs b/rust/src/structure.rs
index a06d767..e9999b9 100644
--- a/rust/src/structure.rs
+++ b/rust/src/structure.rs
@@ -226,8 +226,11 @@ impl NodeTrait for NodeSvg {
 
 impl Drop for NodeSvg {
     fn drop (&mut self) {
-        property_bag::free (self.atts.get ());
-        self.atts.set (ptr::null_mut ());
+        let pbag = self.atts.get ();
+
+        if !pbag.is_null () {
+            property_bag::free (pbag);
+        }
     }
 }
 
@@ -307,14 +310,17 @@ pub extern fn rsvg_node_svg_apply_atts (raw_node: *const RsvgNode, handle: *cons
 
     node.with_impl (|svg: &NodeSvg| {
         let pbag = svg.atts.get ();
-        let class = property_bag::lookup (pbag, "class");
-        let id = property_bag::lookup (pbag, "id");
-
-        unsafe { rsvg_parse_style_attrs (handle,
-                                         raw_node,
-                                         str::to_glib_none ("svg").0,
-                                         class.map_or (ptr::null (), |s| String::to_glib_none (&s).0),
-                                         id.map_or (ptr::null (), |s| String::to_glib_none (&s).0),
-                                         pbag); }
+
+        if !pbag.is_null () {
+            let class = property_bag::lookup (pbag, "class");
+            let id = property_bag::lookup (pbag, "id");
+
+            unsafe { rsvg_parse_style_attrs (handle,
+                                             raw_node,
+                                             str::to_glib_none ("svg").0,
+                                             class.map_or (ptr::null (), |s| String::to_glib_none (&s).0),
+                                             id.map_or (ptr::null (), |s| String::to_glib_none (&s).0),
+                                             pbag); }
+        }
     });
 }


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