From 06545b25c09cae0478a98e15e77eba002e5437b5 Mon Sep 17 00:00:00 2001 From: Marvin Scholz Date: Sun, 1 Mar 2015 14:30:19 +0100 Subject: [PATCH] Fixed memory leaks in custom xslt loader This fixes some memory leaks in the xslt loader, additionally it now makes no copy of the URI if not needed. --- src/xslt.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/xslt.c b/src/xslt.c index fe7a8f84..99f3460a 100644 --- a/src/xslt.c +++ b/src/xslt.c @@ -203,27 +203,31 @@ static xmlDocPtr custom_loader(const xmlChar *URI, xsltLoadType type) { xmlDocPtr ret; - xmlChar *rel_path, *fn, *final_URI; + xmlChar *rel_path, *fn, *final_URI = NULL; xsltStylesheet *c; ice_config_t *config; - final_URI = xmlStrdup(URI); - struct stat file; switch (type) { /* In case an include is loaded */ case XSLT_LOAD_STYLESHEET: /* Not look in admindir if the include file exists */ - if (stat((char *)URI, &file) == 0) + if (access((char *)URI, F_OK) == 0) break; c = (xsltStylesheet *) ctxt; /* Check if we actually have context/path */ if (ctxt == NULL || c->doc->URL == NULL) break; + /* Construct the right path */ rel_path = xmlBuildRelativeURI(URI, c->doc->URL); if (rel_path != NULL && admin_path != NULL) { fn = xmlBuildURI(rel_path, admin_path); - if (fn != NULL && stat((char *)fn, &file) == 0) { - final_URI = fn; - } + final_URI = fn; + xmlFree(rel_path); + } + /* Fail if there was an error constructing the path */ + if (final_URI == NULL) { + if (rel_path) + xmlFree(rel_path); + return NULL; } break; /* In case a top stylesheet is loaded */ @@ -238,8 +242,12 @@ static xmlDocPtr custom_loader(const xmlChar *URI, break; } /* Get the actual xmlDoc */ - ret = xslt_loader(final_URI, dict, options, ctxt, type); - xmlFree(final_URI); + if (final_URI) { + ret = xslt_loader(final_URI, dict, options, ctxt, type); + xmlFree(final_URI); + } else { + ret = xslt_loader(URI, dict, options, ctxt, type); + } return ret; }