[librsvg] Plug mem leaks



commit 320cac5008a6b0db5900098404b98eed9073ad57
Author: Christian Persch <chpe gnome org>
Date:   Thu Dec 15 15:30:32 2011 +0100

    Plug mem leaks
    
    When parsing an SVG file with <title>, <desc>, or <metadata> elements
    on more then one element, the result was that the last element's data
    won. Instead, only parse these elements on the toplevel <svg> element,
    since that's what the API exposes in
    rsvg_handle_get_{desc,title,metadata}.
    
    ==673== 44 (12 direct, 32 indirect) bytes in 1 blocks are definitely lost in loss record 3,849 of 5,438
    ==673==    at 0x402AD89: malloc (vg_replace_malloc.c:236)
    ==673==    by 0x4C4EB5A: standard_malloc (gmem.c:85)
    ==673==    by 0x4C4EF00: g_malloc (gmem.c:159)
    ==673==    by 0x4C62C7D: g_slice_alloc (gslice.c:1003)
    ==673==    by 0x4C66AD9: g_string_sized_new (gstring.c:121)
    ==673==    by 0x4C67116: g_string_new (gstring.c:147)
    ==673==    by 0x4059306: rsvg_start_element (rsvg-base.c:403)
    
    ==673== 44 (12 direct, 32 indirect) bytes in 1 blocks are definitely lost in loss record 3,850 of 5,438
    ==673==    at 0x402AD89: malloc (vg_replace_malloc.c:236)
    ==673==    by 0x4C4EB5A: standard_malloc (gmem.c:85)
    ==673==    by 0x4C4EF00: g_malloc (gmem.c:159)
    ==673==    by 0x4C62C7D: g_slice_alloc (gslice.c:1003)
    ==673==    by 0x4C66AD9: g_string_sized_new (gstring.c:121)
    ==673==    by 0x4C67116: g_string_new (gstring.c:147)
    ==673==    by 0x4059306: rsvg_start_element (rsvg-base.c:403)
    
    https://bugzilla.gnome.org/show_bug.cgi?id=665824

 rsvg-base.c |   48 +++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 39 insertions(+), 9 deletions(-)
---
diff --git a/rsvg-base.c b/rsvg-base.c
index ee76a49..a20c501 100644
--- a/rsvg-base.c
+++ b/rsvg-base.c
@@ -294,6 +294,9 @@ rsvg_desc_handler_characters (RsvgSaxHandler * self, const char *ch, int len)
     if (!ch || !len)
         return;
 
+    if (ctx->priv->desc == NULL)
+        ctx->priv->desc = g_string_new (NULL);
+
     if (!g_utf8_validate ((char *) ch, len, NULL)) {
         char *utf8;
         utf8 = rsvg_make_valid_utf8 ((char *) ch, len);
@@ -327,14 +330,22 @@ static void
 rsvg_start_desc (RsvgHandle * ctx)
 {
     RsvgSaxHandlerDesc *handler = g_new0 (RsvgSaxHandlerDesc, 1);
+    RsvgNode *treebase = ctx->priv->treebase;
+    RsvgNode *currentnode = ctx->priv->currentnode;
+    gboolean do_care;
+
+    /* only parse <desc> for the <svg> node.
+     * This isn't quite the correct behavior - any graphics
+     * element may contain a <desc> element.
+     */
+    do_care = treebase != NULL && treebase == currentnode;
 
     handler->super.free = rsvg_desc_handler_free;
-    handler->super.characters = rsvg_desc_handler_characters;
+    handler->super.characters = do_care ? rsvg_desc_handler_characters : NULL;
     handler->super.start_element = rsvg_desc_handler_start;
     handler->super.end_element = rsvg_desc_handler_end;
     handler->ctx = ctx;
 
-    ctx->priv->desc = g_string_new (NULL);
     ctx->priv->handler = &handler->super;
 }
 
@@ -360,6 +371,9 @@ rsvg_title_handler_characters (RsvgSaxHandler * self, const char *ch, int len)
     if (!ch || !len)
         return;
 
+    if (ctx->priv->title == NULL)
+        ctx->priv->title = g_string_new (NULL);
+
     if (!g_utf8_validate ((char *) ch, len, NULL)) {
         char *utf8;
         utf8 = rsvg_make_valid_utf8 ((char *) ch, len);
@@ -393,14 +407,22 @@ static void
 rsvg_start_title (RsvgHandle * ctx)
 {
     RsvgSaxHandlerTitle *handler = g_new0 (RsvgSaxHandlerTitle, 1);
+    RsvgNode *treebase = ctx->priv->treebase;
+    RsvgNode *currentnode = ctx->priv->currentnode;
+    gboolean do_care;
+
+    /* only parse <title> for the <svg> node.
+     * This isn't quite the correct behavior - any graphics
+     * element may contain a <title> element.
+     */
+    do_care = treebase != NULL && treebase == currentnode;
 
     handler->super.free = rsvg_title_handler_free;
-    handler->super.characters = rsvg_title_handler_characters;
+    handler->super.characters = do_care ? rsvg_title_handler_characters : NULL;
     handler->super.start_element = rsvg_title_handler_start;
     handler->super.end_element = rsvg_title_handler_end;
     handler->ctx = ctx;
 
-    ctx->priv->title = g_string_new (NULL);
     ctx->priv->handler = &handler->super;
 }
 
@@ -420,12 +442,12 @@ rsvg_metadata_handler_characters (RsvgSaxHandler * self, const char *ch, int len
     RsvgSaxHandlerDesc *z = (RsvgSaxHandlerDesc *) self;
     RsvgHandle *ctx = z->ctx;
 
-    /* This isn't quite the correct behavior - in theory, any graphics
-       element may contain a metadata or desc element */
-
     if (!ch || !len)
         return;
 
+    if (ctx->priv->metadata == NULL)
+        ctx->priv->metadata = g_string_new (NULL);
+
     if (!g_utf8_validate ((char *) ch, len, NULL)) {
         char *utf8;
         utf8 = rsvg_make_valid_utf8 ((char *) ch, len);
@@ -473,14 +495,22 @@ static void
 rsvg_start_metadata (RsvgHandle * ctx)
 {
     RsvgSaxHandlerMetadata *handler = g_new0 (RsvgSaxHandlerMetadata, 1);
+    RsvgNode *treebase = ctx->priv->treebase;
+    RsvgNode *currentnode = ctx->priv->currentnode;
+    gboolean do_care;
+
+    /* only parse <metadata> for the <svg> node.
+     * This isn't quite the correct behavior - any graphics
+     * element may contain a <metadata> element.
+     */
+    do_care = treebase != NULL && treebase == currentnode;
 
     handler->super.free = rsvg_metadata_handler_free;
-    handler->super.characters = rsvg_metadata_handler_characters;
+    handler->super.characters = do_care ? rsvg_metadata_handler_characters : NULL;
     handler->super.start_element = rsvg_metadata_handler_start;
     handler->super.end_element = rsvg_metadata_handler_end;
     handler->ctx = ctx;
 
-    ctx->priv->metadata = g_string_new (NULL);
     ctx->priv->handler = &handler->super;
 }
 



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