ping //Re: [PATCH] encoding: fix memleak in xmlRegisterCharEncodingHandler()

Xiaoming Ni <[email protected]> Tue, 1 Dec 2020 15:05:55 +0800
Newsgroups gmane.comp.gnome.lib.xml.general
Message-ID <[email protected]>
ping

On 2020/11/24 14:55, Xiaoming Ni wrote:
> The return type of xmlRegisterCharEncodingHandler() is void. The invoker
> cannot determine whether xmlRegisterCharEncodingHandler() is executed
> successfully. when nbCharEncodingHandler >= MAX_ENCODING_HANDLERS, the
> "handler" is not added to the array "handlers". As a result, the memory
> of "handler" cannot be managed and released: memory leakage.
> 
> so add "xmlfree(handler)" to fix memory leakage on the failure branch of
> xmlRegisterCharEncodingHandler().
> 
> Reported-by: wuqing <[email protected]>
> Signed-off-by: Xiaoming Ni <[email protected]>
> ---
>   encoding.c | 13 +++++++++++--
>   1 file changed, 11 insertions(+), 2 deletions(-)
> 
> diff --git a/encoding.c b/encoding.c
> index c34aca44..841540ed 100644
> --- a/encoding.c
> +++ b/encoding.c
> @@ -1488,16 +1488,25 @@ xmlRegisterCharEncodingHandler(xmlCharEncodingHandlerPtr handler) {
>       if ((handler == NULL) || (handlers == NULL)) {
>           xmlEncodingErr(XML_I18N_NO_HANDLER,
>   		"xmlRegisterCharEncodingHandler: NULL handler !\n", NULL);
> -	return;
> +        goto free_handler;
>       }
>   
>       if (nbCharEncodingHandler >= MAX_ENCODING_HANDLERS) {
>           xmlEncodingErr(XML_I18N_EXCESS_HANDLER,
>   	"xmlRegisterCharEncodingHandler: Too many handler registered, see %s\n",
>   	               "MAX_ENCODING_HANDLERS");
> -	return;
> +        goto free_handler;
>       }
>       handlers[nbCharEncodingHandler++] = handler;
> +    return;
> +
> +free_handler:
> +    if (handler != NULL) {
> +        if (handler->name != NULL) {
> +            xmlFree(handler->name);
> +        }
> +        xmlFree(handler);
> +    }
>   }
>   
>   /**
>