]> gitweb.michael.orlitzky.com - libsvgtiny.git/commitdiff
src/svgtiny.c: cleanup dom nodes and select ctx after svgtiny_parse()
authorMichael Orlitzky <michael@orlitzky.com>
Sat, 19 Oct 2024 23:16:20 +0000 (19:16 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Sun, 20 Oct 2024 21:46:58 +0000 (17:46 -0400)
The svgtiny_parse() function has a "cleanup:" label at the end
that... cleans up, and then returns whatever result is set. Looking
back through this function, though, we see that none of "svg",
"document", or "state.select_ctx" are cleaned up separately in the two
places where we jump to the "cleanup:" label. Thus those resources may
be leaked.

To prevent that, we just have to move the "cleanup:" label up a bit,
so that it includes the clean-up for "svg", "document", and
"state.select_ctx". We also add a new guard that prevents us from
clobbering a real error code with an error from the context destructor
in the cleanup routine.

src/svgtiny.c

index 3d5cd2cf5c92f82118fad83c24bf8f1eaa56a6de..193164beeb9833a0f549d89c7755879b6ca7a0de 100644 (file)
@@ -782,14 +782,18 @@ svgtiny_code svgtiny_parse(struct svgtiny_diagram *diagram,
                code = svgtiny_parse_svg(svg, state);
        }
 
+cleanup:
        dom_node_unref(svg);
        dom_node_unref(document);
+
+       /* Only override the true exit code with a failure from this
+        * "destroy" if a more meaningful error code is not already
+        * set. */
        css_code = css_select_ctx_destroy(state.select_ctx);
-       if (css_code != CSS_OK) {
+       if (css_code != CSS_OK && code == svgtiny_OK) {
                code = svgtiny_LIBCSS_ERROR;
        }
 
-cleanup:
        svgtiny_cleanup_state_local(&state);
 #define SVGTINY_STRING_ACTION2(s,n)                    \
        if (state.interned_##s != NULL)                 \