From 3c69533a4e7e109389b9f74649516a25eeb8ee11 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sat, 19 Oct 2024 19:16:20 -0400 Subject: [PATCH] src/svgtiny.c: cleanup dom nodes and select ctx after svgtiny_parse() 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 | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/svgtiny.c b/src/svgtiny.c index 3d5cd2c..193164b 100644 --- a/src/svgtiny.c +++ b/src/svgtiny.c @@ -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) \ -- 2.44.2