From 42942066e1f6422e26cd162a6014b19ac215083f Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Mon, 11 Nov 2019 13:49:11 +0100 Subject: [PATCH] Fix memory leaks of encoding handlers in xmlsave.c Fix leak of iconv/ICU encoding handler in xmlSaveToBuffer. Fix leaks of iconv/ICU encoding handlers in xmlSaveTo* error paths. Closes #127. --- xmlsave.c | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/xmlsave.c b/xmlsave.c index fa009153..7158c26b 100644 --- a/xmlsave.c +++ b/xmlsave.c @@ -1802,6 +1802,7 @@ xmlSaveToFd(int fd, const char *encoding, int options) if (ret == NULL) return(NULL); ret->buf = xmlOutputBufferCreateFd(fd, ret->handler); if (ret->buf == NULL) { + xmlCharEncCloseFunc(ret->handler); xmlFreeSaveCtxt(ret); return(NULL); } @@ -1831,6 +1832,7 @@ xmlSaveToFilename(const char *filename, const char *encoding, int options) ret->buf = xmlOutputBufferCreateFilename(filename, ret->handler, compression); if (ret->buf == NULL) { + xmlCharEncCloseFunc(ret->handler); xmlFreeSaveCtxt(ret); return(NULL); } @@ -1853,28 +1855,15 @@ xmlSaveCtxtPtr xmlSaveToBuffer(xmlBufferPtr buffer, const char *encoding, int options) { xmlSaveCtxtPtr ret; - xmlOutputBufferPtr out_buff; - xmlCharEncodingHandlerPtr handler; ret = xmlNewSaveCtxt(encoding, options); if (ret == NULL) return(NULL); - - if (encoding != NULL) { - handler = xmlFindCharEncodingHandler(encoding); - if (handler == NULL) { - xmlFree(ret); - return(NULL); - } - } else - handler = NULL; - out_buff = xmlOutputBufferCreateBuffer(buffer, handler); - if (out_buff == NULL) { - xmlFree(ret); - if (handler) xmlCharEncCloseFunc(handler); - return(NULL); + ret->buf = xmlOutputBufferCreateBuffer(buffer, ret->handler); + if (ret->buf == NULL) { + xmlCharEncCloseFunc(ret->handler); + xmlFreeSaveCtxt(ret); + return(NULL); } - - ret->buf = out_buff; return(ret); } @@ -1902,6 +1891,7 @@ xmlSaveToIO(xmlOutputWriteCallback iowrite, if (ret == NULL) return(NULL); ret->buf = xmlOutputBufferCreateIO(iowrite, ioclose, ioctx, ret->handler); if (ret->buf == NULL) { + xmlCharEncCloseFunc(ret->handler); xmlFreeSaveCtxt(ret); return(NULL); }