From ef44c240f5cc5dd41c8dfa76104eed73b075924b Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Mon, 10 Mar 2025 14:15:35 +0100 Subject: [PATCH] encoding: Fix memory leak in xmlCharEncNewCustomHandler Short-lived regression. --- encoding.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/encoding.c b/encoding.c index 3ac9e427..1996ef3f 100644 --- a/encoding.c +++ b/encoding.c @@ -668,15 +668,13 @@ xmlCharEncNewCustomHandler(const char *name, handler = xmlMalloc(sizeof(*handler)); if (handler == NULL) - return(XML_ERR_NO_MEMORY); + goto error; memset(handler, 0, sizeof(*handler)); if (name != NULL) { handler->name = xmlMemStrdup(name); - if (handler->name == NULL) { - xmlFree(handler); - return(XML_ERR_NO_MEMORY); - } + if (handler->name == NULL) + goto error; } handler->input.func = input; @@ -687,6 +685,18 @@ xmlCharEncNewCustomHandler(const char *name, *out = handler; return(XML_ERR_OK); + +error: + xmlFree(handler); + + if (ctxtDtor != NULL) { + if (inputCtxt != NULL) + ctxtDtor(inputCtxt); + if (outputCtxt != NULL) + ctxtDtor(outputCtxt); + } + + return(XML_ERR_NO_MEMORY); } /**