1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-10-24 13:33:01 +03:00

io: Make xmlOutputBufferCreate* not free encoder on error

Revert a530ff12 which was an inadvertent API change.
This commit is contained in:
Nick Wellnhofer
2025-05-13 22:38:15 +02:00
parent 5b71dca613
commit fcb7a777ce
3 changed files with 26 additions and 20 deletions

View File

@@ -559,8 +559,10 @@ htmlNodeDumpFileFormat(FILE *out, xmlDocPtr doc ATTRIBUTE_UNUSED,
if (htmlFindOutputEncoder(encoding, &handler) != XML_ERR_OK) if (htmlFindOutputEncoder(encoding, &handler) != XML_ERR_OK)
return(-1); return(-1);
buf = xmlOutputBufferCreateFile(out, handler); buf = xmlOutputBufferCreateFile(out, handler);
if (buf == NULL) if (buf == NULL) {
xmlCharEncCloseFunc(handler);
return(-1); return(-1);
}
htmlNodeDumpInternal(buf, cur, NULL, format); htmlNodeDumpInternal(buf, cur, NULL, format);
@@ -613,8 +615,10 @@ htmlDocDumpMemoryFormat(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
if (htmlFindOutputEncoder((char *) cur->encoding, &handler) != XML_ERR_OK) if (htmlFindOutputEncoder((char *) cur->encoding, &handler) != XML_ERR_OK)
return; return;
buf = xmlAllocOutputBuffer(handler); buf = xmlAllocOutputBuffer(handler);
if (buf == NULL) if (buf == NULL) {
xmlCharEncCloseFunc(handler);
return; return;
}
htmlDocContentDumpFormatOutput(buf, cur, NULL, format); htmlDocContentDumpFormatOutput(buf, cur, NULL, format);
@@ -1202,8 +1206,10 @@ htmlDocDump(FILE *f, xmlDocPtr cur) {
if (htmlFindOutputEncoder((char *) cur->encoding, &handler) != XML_ERR_OK) if (htmlFindOutputEncoder((char *) cur->encoding, &handler) != XML_ERR_OK)
return(-1); return(-1);
buf = xmlOutputBufferCreateFile(f, handler); buf = xmlOutputBufferCreateFile(f, handler);
if (buf == NULL) if (buf == NULL) {
xmlCharEncCloseFunc(handler);
return(-1); return(-1);
}
htmlDocContentDumpOutput(buf, cur, NULL); htmlDocContentDumpOutput(buf, cur, NULL);
ret = xmlOutputBufferClose(buf); ret = xmlOutputBufferClose(buf);
@@ -1265,8 +1271,10 @@ htmlSaveFileFormat(const char *filename, xmlDocPtr cur,
* save the content to a temp buffer. * save the content to a temp buffer.
*/ */
buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression); buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
if (buf == NULL) if (buf == NULL) {
xmlCharEncCloseFunc(handler);
return(0); return(0);
}
htmlDocContentDumpFormatOutput(buf, cur, encoding, format); htmlDocContentDumpFormatOutput(buf, cur, encoding, format);

26
xmlIO.c
View File

@@ -1509,7 +1509,7 @@ xmlAllocParserInputBuffer(xmlCharEncoding enc) {
/** /**
* Create a buffered parser output * Create a buffered parser output
* *
* Consumes `encoder` even in error case. * Consumes `encoder` but not in error case.
* *
* @param encoder the encoding converter or NULL * @param encoder the encoding converter or NULL
* @returns the new parser output or NULL * @returns the new parser output or NULL
@@ -1520,13 +1520,11 @@ xmlAllocOutputBuffer(xmlCharEncodingHandlerPtr encoder) {
ret = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer)); ret = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
if (ret == NULL) { if (ret == NULL) {
xmlCharEncCloseFunc(encoder);
return(NULL); return(NULL);
} }
memset(ret, 0, sizeof(xmlOutputBuffer)); memset(ret, 0, sizeof(xmlOutputBuffer));
ret->buffer = xmlBufCreate(MINLEN); ret->buffer = xmlBufCreate(MINLEN);
if (ret->buffer == NULL) { if (ret->buffer == NULL) {
xmlCharEncCloseFunc(encoder);
xmlFree(ret); xmlFree(ret);
return(NULL); return(NULL);
} }
@@ -1535,7 +1533,8 @@ xmlAllocOutputBuffer(xmlCharEncodingHandlerPtr encoder) {
if (encoder != NULL) { if (encoder != NULL) {
ret->conv = xmlBufCreate(MINLEN); ret->conv = xmlBufCreate(MINLEN);
if (ret->conv == NULL) { if (ret->conv == NULL) {
xmlOutputBufferClose(ret); xmlBufFree(ret->buffer);
xmlFree(ret);
return(NULL); return(NULL);
} }
@@ -1778,7 +1777,6 @@ __xmlOutputBufferCreateFilename(const char *URI,
* Allocate the Output buffer front-end. * Allocate the Output buffer front-end.
*/ */
ret = xmlAllocOutputBuffer(encoder); ret = xmlAllocOutputBuffer(encoder);
encoder = NULL;
if (ret == NULL) if (ret == NULL)
goto error; goto error;
@@ -1807,14 +1805,14 @@ __xmlOutputBufferCreateFilename(const char *URI,
} }
if (ret->context == NULL) { if (ret->context == NULL) {
/* Don't free encoder */
ret->encoder = NULL;
xmlOutputBufferClose(ret); xmlOutputBufferClose(ret);
ret = NULL; ret = NULL;
} }
error: error:
xmlFree(unescaped); xmlFree(unescaped);
if (encoder != NULL)
xmlCharEncCloseFunc(encoder);
return(ret); return(ret);
} }
@@ -1824,7 +1822,7 @@ error:
* Automatic support for ZLIB/Compress compressed document is provided * Automatic support for ZLIB/Compress compressed document is provided
* by default if found at compile-time. * by default if found at compile-time.
* *
* Consumes `encoder` even in error case. * Consumes `encoder` but not in error case.
* *
* @param URI a C string containing the URI or filename * @param URI a C string containing the URI or filename
* @param encoder the encoding converter or NULL * @param encoder the encoding converter or NULL
@@ -1877,7 +1875,7 @@ xmlParserInputBufferCreateFile(FILE *file, xmlCharEncoding enc) {
* Create a buffered output for the progressive saving to a `FILE *` * Create a buffered output for the progressive saving to a `FILE *`
* buffered C I/O. * buffered C I/O.
* *
* Consumes `encoder` even in error case. * Consumes `encoder` but not in error case.
* *
* @param file a `FILE *` * @param file a `FILE *`
* @param encoder the encoding converter or NULL * @param encoder the encoding converter or NULL
@@ -1888,7 +1886,6 @@ xmlOutputBufferCreateFile(FILE *file, xmlCharEncodingHandlerPtr encoder) {
xmlOutputBufferPtr ret; xmlOutputBufferPtr ret;
if (file == NULL) { if (file == NULL) {
xmlCharEncCloseFunc(encoder);
return(NULL); return(NULL);
} }
@@ -1905,7 +1902,7 @@ xmlOutputBufferCreateFile(FILE *file, xmlCharEncodingHandlerPtr encoder) {
/** /**
* Create a buffered output for the progressive saving to a xmlBuffer * Create a buffered output for the progressive saving to a xmlBuffer
* *
* Consumes `encoder` even in error case. * Consumes `encoder` but not in error case.
* *
* @param buffer a xmlBufferPtr * @param buffer a xmlBufferPtr
* @param encoder the encoding converter or NULL * @param encoder the encoding converter or NULL
@@ -1917,7 +1914,6 @@ xmlOutputBufferCreateBuffer(xmlBufferPtr buffer,
xmlOutputBufferPtr ret; xmlOutputBufferPtr ret;
if (buffer == NULL) { if (buffer == NULL) {
xmlCharEncCloseFunc(encoder);
return(NULL); return(NULL);
} }
@@ -2167,7 +2163,7 @@ xmlNewInputBufferString(const char *str, xmlParserInputFlags flags) {
* Create a buffered output for the progressive saving * Create a buffered output for the progressive saving
* to a file descriptor * to a file descriptor
* *
* Consumes `encoder` even in error case. * Consumes `encoder` but not in error case.
* *
* @param fd a file descriptor number * @param fd a file descriptor number
* @param encoder the encoding converter or NULL * @param encoder the encoding converter or NULL
@@ -2178,7 +2174,6 @@ xmlOutputBufferCreateFd(int fd, xmlCharEncodingHandlerPtr encoder) {
xmlOutputBufferPtr ret; xmlOutputBufferPtr ret;
if (fd < 0) { if (fd < 0) {
xmlCharEncCloseFunc(encoder);
return(NULL); return(NULL);
} }
@@ -2239,7 +2234,7 @@ xmlParserInputBufferCreateIO(xmlInputReadCallback ioread,
* Create a buffered output for the progressive saving * Create a buffered output for the progressive saving
* to an I/O handler * to an I/O handler
* *
* Consumes `encoder` even in error case. * Consumes `encoder` but not in error case.
* *
* @param iowrite an I/O write function * @param iowrite an I/O write function
* @param ioclose an I/O close function * @param ioclose an I/O close function
@@ -2254,7 +2249,6 @@ xmlOutputBufferCreateIO(xmlOutputWriteCallback iowrite,
xmlOutputBufferPtr ret; xmlOutputBufferPtr ret;
if (iowrite == NULL) { if (iowrite == NULL) {
xmlCharEncCloseFunc(encoder);
return(NULL); return(NULL);
} }

View File

@@ -1836,6 +1836,7 @@ xmlSaveToFd(int fd, const char *encoding, int options)
if (ret == NULL) return(NULL); if (ret == NULL) return(NULL);
ret->buf = xmlOutputBufferCreateFd(fd, ret->handler); ret->buf = xmlOutputBufferCreateFd(fd, ret->handler);
if (ret->buf == NULL) { if (ret->buf == NULL) {
xmlCharEncCloseFunc(ret->handler);
xmlFreeSaveCtxt(ret); xmlFreeSaveCtxt(ret);
return(NULL); return(NULL);
} }
@@ -1868,6 +1869,7 @@ xmlSaveToFilename(const char *filename, const char *encoding, int options)
ret->buf = xmlOutputBufferCreateFilename(filename, ret->handler, ret->buf = xmlOutputBufferCreateFilename(filename, ret->handler,
compression); compression);
if (ret->buf == NULL) { if (ret->buf == NULL) {
xmlCharEncCloseFunc(ret->handler);
xmlFreeSaveCtxt(ret); xmlFreeSaveCtxt(ret);
return(NULL); return(NULL);
} }
@@ -1899,6 +1901,7 @@ xmlSaveToBuffer(xmlBufferPtr buffer, const char *encoding, int options)
if (ret == NULL) return(NULL); if (ret == NULL) return(NULL);
ret->buf = xmlOutputBufferCreateBuffer(buffer, ret->handler); ret->buf = xmlOutputBufferCreateBuffer(buffer, ret->handler);
if (ret->buf == NULL) { if (ret->buf == NULL) {
xmlCharEncCloseFunc(ret->handler);
xmlFreeSaveCtxt(ret); xmlFreeSaveCtxt(ret);
return(NULL); return(NULL);
} }
@@ -1933,6 +1936,7 @@ xmlSaveToIO(xmlOutputWriteCallback iowrite,
if (ret == NULL) return(NULL); if (ret == NULL) return(NULL);
ret->buf = xmlOutputBufferCreateIO(iowrite, ioclose, ioctx, ret->handler); ret->buf = xmlOutputBufferCreateIO(iowrite, ioclose, ioctx, ret->handler);
if (ret->buf == NULL) { if (ret->buf == NULL) {
xmlCharEncCloseFunc(ret->handler);
xmlFreeSaveCtxt(ret); xmlFreeSaveCtxt(ret);
return(NULL); return(NULL);
} }