From ef9e5dcdd29b794de438155f43a0a3374b4d33a3 Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Sat, 30 Jul 2011 22:21:21 -0700 Subject: Refactored createnewDocFromMem() to be C++ instead of C, removing potential for memory leaks. (bzr r10523) --- src/document.cpp | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'src/document.cpp') diff --git a/src/document.cpp b/src/document.cpp index 4d1d8780a..b699f8afa 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -497,25 +497,21 @@ SPDocument *SPDocument::createNewDoc(gchar const *uri, unsigned int keepalive, b SPDocument *SPDocument::createNewDocFromMem(gchar const *buffer, gint length, unsigned int keepalive) { - SPDocument *doc; - Inkscape::XML::Document *rdoc; - Inkscape::XML::Node *rroot; - gchar *name; - - rdoc = sp_repr_read_mem(buffer, length, SP_SVG_NS_URI); - - /* If it cannot be loaded, return NULL without warning */ - if (rdoc == NULL) return NULL; - - rroot = rdoc->root(); - /* If xml file is not svg, return NULL without warning */ - /* fixme: destroy document */ - if (strcmp(rroot->name(), "svg:svg") != 0) return NULL; - - name = g_strdup_printf(_("Memory document %d"), ++doc_count); + SPDocument *doc = 0; + + Inkscape::XML::Document *rdoc = sp_repr_read_mem(buffer, length, SP_SVG_NS_URI); + if ( rdoc ) { + // Only continue to create a non-null doc if it could be loaded + Inkscape::XML::Node *rroot = rdoc->root(); + if ( strcmp(rroot->name(), "svg:svg") != 0 ) { + // If xml file is not svg, return NULL without warning + // TODO fixme: destroy document + } else { + Glib::ustring name = Glib::ustring::compose( _("Memory document %1"), ++doc_count ); + doc = createDoc(rdoc, NULL, NULL, name.c_str(), keepalive); + } + } - doc = createDoc(rdoc, NULL, NULL, name, keepalive); - g_free(name); return doc; } -- cgit v1.2.3