save: Improve error handling

Handle malloc failrue from xmlRaiseError.

Use xmlRaiseMemoryError.

Stop using xmlGenericError.

Remove argument from memory error handler.

Remove TODO macro.
This commit is contained in:
Nick Wellnhofer 2023-12-18 21:30:22 +01:00
parent 664db89e0e
commit bc1e030664
2 changed files with 30 additions and 30 deletions

View File

@ -334,17 +334,6 @@ htmlIsBooleanAttr(const xmlChar *name)
* Output error handlers * * Output error handlers *
* * * *
************************************************************************/ ************************************************************************/
/**
* htmlSaveErrMemory:
* @extra: extra information
*
* Handle an out of memory condition
*/
static void
htmlSaveErrMemory(const char *extra)
{
__xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra);
}
/** /**
* htmlSaveErr: * htmlSaveErr:
@ -358,6 +347,7 @@ static void
htmlSaveErr(int code, xmlNodePtr node, const char *extra) htmlSaveErr(int code, xmlNodePtr node, const char *extra)
{ {
const char *msg = NULL; const char *msg = NULL;
int res;
switch(code) { switch(code) {
case XML_SAVE_NOT_UTF8: case XML_SAVE_NOT_UTF8:
@ -375,7 +365,13 @@ htmlSaveErr(int code, xmlNodePtr node, const char *extra)
default: default:
msg = "unexpected error number\n"; msg = "unexpected error number\n";
} }
__xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra);
res = __xmlRaiseError(NULL, NULL, NULL, NULL, node,
XML_FROM_OUTPUT, code, XML_ERR_ERROR, NULL, 0,
extra, NULL, NULL, 0, 0,
msg, extra);
if (res < 0)
xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_OUTPUT, NULL);
} }
/************************************************************************ /************************************************************************
@ -435,10 +431,8 @@ htmlBufNodeDumpFormat(xmlBufPtr buf, xmlDocPtr doc, xmlNodePtr cur,
return (-1); return (-1);
} }
outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer)); outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
if (outbuf == NULL) { if (outbuf == NULL)
htmlSaveErrMemory("allocating HTML output buffer");
return (-1); return (-1);
}
memset(outbuf, 0, sizeof(xmlOutputBuffer)); memset(outbuf, 0, sizeof(xmlOutputBuffer));
outbuf->buffer = buf; outbuf->buffer = buf;
outbuf->encoder = NULL; outbuf->encoder = NULL;

View File

@ -28,11 +28,6 @@
#define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml" #define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml"
#define TODO \
xmlGenericError(xmlGenericErrorContext, \
"Unimplemented block at %s:%d\n", \
__FILE__, __LINE__);
struct _xmlSaveCtxt { struct _xmlSaveCtxt {
void *_private; void *_private;
int type; int type;
@ -63,9 +58,9 @@ struct _xmlSaveCtxt {
* Handle an out of memory condition * Handle an out of memory condition
*/ */
static void static void
xmlSaveErrMemory(const char *extra) xmlSaveErrMemory(void)
{ {
__xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra); xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_OUTPUT, NULL);
} }
/** /**
@ -80,6 +75,7 @@ static void
xmlSaveErr(int code, xmlNodePtr node, const char *extra) xmlSaveErr(int code, xmlNodePtr node, const char *extra)
{ {
const char *msg = NULL; const char *msg = NULL;
int res;
switch(code) { switch(code) {
case XML_SAVE_NOT_UTF8: case XML_SAVE_NOT_UTF8:
@ -97,7 +93,13 @@ xmlSaveErr(int code, xmlNodePtr node, const char *extra)
default: default:
msg = "unexpected error number\n"; msg = "unexpected error number\n";
} }
__xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra);
res = __xmlRaiseError(NULL, NULL, NULL, NULL, node,
XML_FROM_OUTPUT, code, XML_ERR_ERROR, NULL, 0,
extra, NULL, NULL, 0, 0,
msg, extra);
if (res < 0)
xmlSaveErrMemory();
} }
/************************************************************************ /************************************************************************
@ -258,8 +260,7 @@ xmlEscapeEntities(unsigned char* out, int *outlen,
if (outend - out < 6) break; if (outend - out < 6) break;
out = xmlSerializeHexCharRef(out, *in++); out = xmlSerializeHexCharRef(out, *in++);
} else { } else {
xmlGenericError(xmlGenericErrorContext, xmlSaveErr(XML_SAVE_CHAR_INVALID, NULL, NULL);
"xmlEscapeEntities : char out of range\n");
in++; in++;
goto error; goto error;
} }
@ -340,7 +341,7 @@ xmlNewSaveCtxt(const char *encoding, int options)
ret = (xmlSaveCtxtPtr) xmlMalloc(sizeof(xmlSaveCtxt)); ret = (xmlSaveCtxtPtr) xmlMalloc(sizeof(xmlSaveCtxt));
if (ret == NULL) { if (ret == NULL) {
xmlSaveErrMemory("creating saving context"); xmlSaveErrMemory();
return ( NULL ); return ( NULL );
} }
memset(ret, 0, sizeof(xmlSaveCtxt)); memset(ret, 0, sizeof(xmlSaveCtxt));
@ -513,14 +514,19 @@ static int xmlSaveSwitchEncoding(xmlSaveCtxtPtr ctxt, const char *encoding) {
xmlCharEncodingHandler *handler; xmlCharEncodingHandler *handler;
int res; int res;
res = xmlOpenCharEncodingHandler((const char *) encoding, &handler); res = xmlOpenCharEncodingHandler(encoding, &handler);
if (res != 0) { if (res != 0) {
if (res < 0)
xmlSaveErrMemory();
else
xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding);
buf->error = res; buf->error = res;
return(-1); return(-1);
} }
buf->conv = xmlBufCreate(); buf->conv = xmlBufCreate();
if (buf->conv == NULL) { if (buf->conv == NULL) {
xmlCharEncCloseFunc(handler); xmlCharEncCloseFunc(handler);
xmlSaveErrMemory();
buf->error = XML_ERR_NO_MEMORY; buf->error = XML_ERR_NO_MEMORY;
return(-1); return(-1);
} }
@ -2195,7 +2201,7 @@ xmlBufNodeDump(xmlBufPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
} }
outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer)); outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
if (outbuf == NULL) { if (outbuf == NULL) {
xmlSaveErrMemory("creating buffer"); xmlSaveErrMemory();
return (-1); return (-1);
} }
memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer)); memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
@ -2370,7 +2376,7 @@ xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
} }
if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) { if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
xmlSaveErrMemory("creating buffer"); xmlSaveErrMemory();
xmlCharEncCloseFunc(conv_hdlr); xmlCharEncCloseFunc(conv_hdlr);
return; return;
} }
@ -2404,7 +2410,7 @@ xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
return; return;
error: error:
xmlSaveErrMemory("creating output"); xmlSaveErrMemory();
xmlOutputBufferClose(out_buff); xmlOutputBufferClose(out_buff);
return; return;
} }