From ecb4c9fb2802a7963aa6e1b82ec4210badf1b7e1 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Mon, 18 Dec 2023 21:32:49 +0100 Subject: [PATCH] misc: Improve error handling Remove calls to generic error handler or use stderr for - legacy deprecation warnings - nanohttp, nanoftp in standalone mode - memory debug messages Use xmlRaiseMemoryError. Remove TODO macro. Don't raise errors in xmlmodule.c. --- legacy.c | 31 ++++++++++++++------------- list.c | 27 +++++------------------- nanoftp.c | 14 ++++++------ nanohttp.c | 18 ++++++++-------- pattern.c | 4 ---- threads.c | 5 ++--- xmlmemory.c | 30 +++++++++++++------------- xmlmodule.c | 61 ++++++----------------------------------------------- 8 files changed, 60 insertions(+), 130 deletions(-) diff --git a/legacy.c b/legacy.c index 4bd25fbf..65586541 100644 --- a/legacy.c +++ b/legacy.c @@ -11,6 +11,7 @@ #include "libxml.h" #ifdef LIBXML_LEGACY_ENABLED +#include #include #include @@ -55,7 +56,7 @@ htmlDecodeEntities(htmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED, static int deprecated = 0; if (!deprecated) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "htmlDecodeEntities() deprecated function reached\n"); deprecated = 1; } @@ -416,7 +417,7 @@ xmlDecodeEntities(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED, static int deprecated = 0; if (!deprecated) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlDecodeEntities() deprecated function reached\n"); deprecated = 1; } @@ -446,7 +447,7 @@ xmlNamespaceParseNCName(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED) static int deprecated = 0; if (!deprecated) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlNamespaceParseNCName() deprecated function reached\n"); deprecated = 1; } @@ -481,7 +482,7 @@ xmlNamespaceParseQName(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED, static int deprecated = 0; if (!deprecated) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlNamespaceParseQName() deprecated function reached\n"); deprecated = 1; } @@ -510,7 +511,7 @@ xmlNamespaceParseNSDef(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED) static int deprecated = 0; if (!deprecated) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlNamespaceParseNSDef() deprecated function reached\n"); deprecated = 1; } @@ -533,7 +534,7 @@ xmlParseQuotedString(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED) static int deprecated = 0; if (!deprecated) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlParseQuotedString() deprecated function reached\n"); deprecated = 1; } @@ -561,7 +562,7 @@ xmlParseNamespace(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED) static int deprecated = 0; if (!deprecated) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlParseNamespace() deprecated function reached\n"); deprecated = 1; } @@ -593,7 +594,7 @@ xmlScanName(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED) static int deprecated = 0; if (!deprecated) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlScanName() deprecated function reached\n"); deprecated = 1; } @@ -633,7 +634,7 @@ xmlParserHandleReference(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED) static int deprecated = 0; if (!deprecated) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlParserHandleReference() deprecated function reached\n"); deprecated = 1; } @@ -659,7 +660,7 @@ xmlHandleEntity(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED, static int deprecated = 0; if (!deprecated) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlHandleEntity() deprecated function reached\n"); deprecated = 1; } @@ -683,7 +684,7 @@ xmlNewGlobalNs(xmlDocPtr doc ATTRIBUTE_UNUSED, static int deprecated = 0; if (!deprecated) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlNewGlobalNs() deprecated function reached\n"); deprecated = 1; } @@ -703,7 +704,7 @@ xmlUpgradeOldNs(xmlDocPtr doc ATTRIBUTE_UNUSED) static int deprecated = 0; if (!deprecated) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlUpgradeOldNs() deprecated function reached\n"); deprecated = 1; } @@ -729,9 +730,9 @@ xmlEncodeEntities(xmlDocPtr doc ATTRIBUTE_UNUSED, static int warning = 1; if (warning) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "Deprecated API xmlEncodeEntities() used\n"); - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, " change code to use xmlEncodeEntitiesReentrant()\n"); warning = 0; } @@ -747,7 +748,7 @@ static int deprecated_v1_msg = 0; #define DEPRECATED(n) \ if (deprecated_v1_msg == 0) \ - xmlGenericError(xmlGenericErrorContext, \ + fprintf(stderr, \ "Use of deprecated SAXv1 function %s\n", n); \ deprecated_v1_msg++; diff --git a/list.c b/list.c index 3eaf67f9..20df26c8 100644 --- a/list.c +++ b/list.c @@ -188,18 +188,13 @@ xmlListPtr xmlListCreate(xmlListDeallocator deallocator, xmlListDataCompare compare) { xmlListPtr l; - if (NULL == (l = (xmlListPtr )xmlMalloc( sizeof(xmlList)))) { - xmlGenericError(xmlGenericErrorContext, - "Cannot initialize memory for list"); + if (NULL == (l = (xmlListPtr )xmlMalloc( sizeof(xmlList)))) return (NULL); - } /* Initialize the list to NULL */ memset(l, 0, sizeof(xmlList)); /* Add the sentinel */ if (NULL ==(l->sentinel = (xmlLinkPtr )xmlMalloc(sizeof(xmlLink)))) { - xmlGenericError(xmlGenericErrorContext, - "Cannot initialize memory for sentinel"); xmlFree(l); return (NULL); } @@ -279,11 +274,8 @@ xmlListInsert(xmlListPtr l, void *data) lkPlace = xmlListLowerSearch(l, data); /* Add the new link */ lkNew = (xmlLinkPtr) xmlMalloc(sizeof(xmlLink)); - if (lkNew == NULL) { - xmlGenericError(xmlGenericErrorContext, - "Cannot initialize memory for new link"); + if (lkNew == NULL) return (1); - } lkNew->data = data; lkPlace = lkPlace->prev; lkNew->next = lkPlace->next; @@ -311,11 +303,8 @@ int xmlListAppend(xmlListPtr l, void *data) lkPlace = xmlListHigherSearch(l, data); /* Add the new link */ lkNew = (xmlLinkPtr) xmlMalloc(sizeof(xmlLink)); - if (lkNew == NULL) { - xmlGenericError(xmlGenericErrorContext, - "Cannot initialize memory for new link"); + if (lkNew == NULL) return (1); - } lkNew->data = data; lkNew->next = lkPlace->next; (lkPlace->next)->prev = lkNew; @@ -548,11 +537,8 @@ xmlListPushFront(xmlListPtr l, void *data) lkPlace = l->sentinel; /* Add the new link */ lkNew = (xmlLinkPtr) xmlMalloc(sizeof(xmlLink)); - if (lkNew == NULL) { - xmlGenericError(xmlGenericErrorContext, - "Cannot initialize memory for new link"); + if (lkNew == NULL) return (0); - } lkNew->data = data; lkNew->next = lkPlace->next; (lkPlace->next)->prev = lkNew; @@ -579,11 +565,8 @@ xmlListPushBack(xmlListPtr l, void *data) return(0); lkPlace = l->sentinel->prev; /* Add the new link */ - if (NULL ==(lkNew = (xmlLinkPtr )xmlMalloc(sizeof(xmlLink)))) { - xmlGenericError(xmlGenericErrorContext, - "Cannot initialize memory for new link"); + if (NULL ==(lkNew = (xmlLinkPtr )xmlMalloc(sizeof(xmlLink)))) return (0); - } lkNew->data = data; lkNew->next = lkPlace->next; (lkPlace->next)->prev = lkNew; diff --git a/nanoftp.c b/nanoftp.c index 8fbe7aac..fc8f8d75 100644 --- a/nanoftp.c +++ b/nanoftp.c @@ -137,9 +137,9 @@ int have_ipv6(void) { * Handle an out of memory condition */ static void -xmlFTPErrMemory(const char *extra) +xmlFTPErrMemory(const char *extra ATTRIBUTE_UNUSED) { - __xmlSimpleError(XML_FROM_FTP, XML_ERR_NO_MEMORY, NULL, NULL, extra); + xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_FTP, NULL); } /** @@ -1923,7 +1923,7 @@ static void ftpList(void *userData, const char *filename, const char* attrib, const char *owner, const char *group, unsigned long size, int links, int year, const char *month, int day, int hour, int minute) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "%s %s %s %ld %s\n", attrib, owner, group, size, filename); } static @@ -1945,7 +1945,7 @@ int main(int argc, char **argv) { if (argc > 1) { ctxt = xmlNanoFTPNewCtxt(argv[1]); if (xmlNanoFTPConnect(ctxt) < 0) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "Couldn't connect to %s\n", argv[1]); exit(1); } @@ -1954,7 +1954,7 @@ int main(int argc, char **argv) { } else ctxt = xmlNanoFTPConnectTo("localhost", 0); if (ctxt == NULL) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "Couldn't connect to localhost\n"); exit(1); } @@ -1962,7 +1962,7 @@ int main(int argc, char **argv) { output = fopen("/tmp/tstdata", "w"); if (output != NULL) { if (xmlNanoFTPGet(ctxt, ftpData, (void *) output, tstfile) < 0) - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "Failed to get file\n"); } @@ -1974,7 +1974,7 @@ int main(int argc, char **argv) { #ifdef STANDALONE #include int main(int argc, char **argv) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "%s : FTP support not compiled in\n", argv[0]); return(0); } diff --git a/nanohttp.c b/nanohttp.c index 0d7af489..81da585c 100644 --- a/nanohttp.c +++ b/nanohttp.c @@ -147,9 +147,9 @@ static int xmlNanoHTTPFetchContent( void * ctx, char ** ptr, int * len ); * Handle an out of memory condition */ static void -xmlHTTPErrMemory(const char *extra) +xmlHTTPErrMemory(void) { - __xmlSimpleError(XML_FROM_HTTP, XML_ERR_NO_MEMORY, NULL, NULL, extra); + xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_HTTP, NULL); } /** @@ -358,7 +358,7 @@ xmlNanoHTTPNewCtxt(const char *URL) { ret = (xmlNanoHTTPCtxtPtr) xmlMalloc(sizeof(xmlNanoHTTPCtxt)); if (ret == NULL) { - xmlHTTPErrMemory("allocating context"); + xmlHTTPErrMemory(); return(NULL); } @@ -504,7 +504,7 @@ xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) if (ctxt->in == NULL) { ctxt->in = (char *) xmlMallocAtomic(65000); if (ctxt->in == NULL) { - xmlHTTPErrMemory("allocating input"); + xmlHTTPErrMemory(); ctxt->last = -1; return (-1); } @@ -529,7 +529,7 @@ xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt) ctxt->inlen *= 2; ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen); if (ctxt->in == NULL) { - xmlHTTPErrMemory("allocating input buffer"); + xmlHTTPErrMemory(); xmlFree(tmp_ptr); ctxt->last = -1; return (-1); @@ -1444,7 +1444,7 @@ retry: bp = (char*)xmlMallocAtomic(blen); if ( bp == NULL ) { xmlNanoHTTPFreeCtxt( ctxt ); - xmlHTTPErrMemory("allocating header buffer"); + xmlHTTPErrMemory(); return ( NULL ); } @@ -1831,9 +1831,9 @@ int main(int argc, char **argv) { xmlNanoHTTPFetch(argv[1], "-", &contentType); if (contentType != NULL) xmlFree(contentType); } else { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "%s: minimal HTTP GET implementation\n", argv[0]); - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "\tusage %s [ URL [ filename ] ]\n", argv[0]); } xmlNanoHTTPCleanup(); @@ -1844,7 +1844,7 @@ int main(int argc, char **argv) { #ifdef STANDALONE #include int main(int argc, char **argv) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "%s : HTTP support not compiled in\n", argv[0]); return(0); } diff --git a/pattern.c b/pattern.c index 3a94acab..6cb48987 100644 --- a/pattern.c +++ b/pattern.c @@ -697,10 +697,6 @@ rollback: * * ************************************************************************/ -#define TODO \ - xmlGenericError(xmlGenericErrorContext, \ - "Unimplemented block at %s:%d\n", \ - __FILE__, __LINE__); #define CUR (*ctxt->cur) #define SKIP(val) ctxt->cur += (val) #define NXT(val) ctxt->cur[(val)] diff --git a/threads.c b/threads.c index 461f4a51..8235d395 100644 --- a/threads.c +++ b/threads.c @@ -498,9 +498,8 @@ xmlGlobalInitMutexLock(void) { if (global_init_lock == NULL) { cs = malloc(sizeof(CRITICAL_SECTION)); if (cs == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlGlobalInitMutexLock: out of memory\n"); - return; + fprintf(stderr, "libxml2: xmlInitParser: out of memory\n"); + abort(); } InitializeCriticalSection(cs); diff --git a/xmlmemory.c b/xmlmemory.c index 1e999b11..b515f1cc 100644 --- a/xmlmemory.c +++ b/xmlmemory.c @@ -126,7 +126,7 @@ static void debugmem_list_delete(MEMHDR *); void xmlMallocBreakpoint(void) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlMallocBreakpoint reached on block %d\n", xmlMemStopAtBlock); } @@ -152,7 +152,7 @@ xmlMallocLoc(size_t size, const char * file, int line) TEST_POINT if (size > (MAX_SIZE_T - RESERVE_SIZE)) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlMallocLoc : Unsigned overflow\n"); return(NULL); } @@ -160,7 +160,7 @@ xmlMallocLoc(size_t size, const char * file, int line) p = (MEMHDR *) malloc(RESERVE_SIZE+size); if (!p) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlMallocLoc : Out of free space\n"); return(NULL); } @@ -184,7 +184,7 @@ xmlMallocLoc(size_t size, const char * file, int line) ret = HDR_2_CLIENT(p); if (xmlMemTraceBlockAt == ret) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt, (long unsigned)size); xmlMallocBreakpoint(); @@ -217,7 +217,7 @@ xmlMallocAtomicLoc(size_t size, const char * file, int line) TEST_POINT if (size > (MAX_SIZE_T - RESERVE_SIZE)) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlMallocAtomicLoc : Unsigned overflow\n"); return(NULL); } @@ -225,7 +225,7 @@ xmlMallocAtomicLoc(size_t size, const char * file, int line) p = (MEMHDR *) malloc(RESERVE_SIZE+size); if (!p) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlMallocAtomicLoc : Out of free space\n"); return(NULL); } @@ -249,7 +249,7 @@ xmlMallocAtomicLoc(size_t size, const char * file, int line) ret = HDR_2_CLIENT(p); if (xmlMemTraceBlockAt == ret) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt, (long unsigned)size); xmlMallocBreakpoint(); @@ -315,7 +315,7 @@ xmlReallocLoc(void *ptr,size_t size, const char * file, int line) xmlMutexUnlock(&xmlMemMutex); if (size > (MAX_SIZE_T - RESERVE_SIZE)) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlReallocLoc : Unsigned overflow\n"); return(NULL); } @@ -327,7 +327,7 @@ xmlReallocLoc(void *ptr,size_t size, const char * file, int line) } p = tmp; if (xmlMemTraceBlockAt == ptr) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "%p : Realloced(%lu -> %lu) Ok\n", xmlMemTraceBlockAt, (long unsigned)p->mh_size, (long unsigned)size); @@ -387,13 +387,13 @@ xmlMemFree(void *ptr) return; if (ptr == (void *) -1) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "trying to free pointer from freed area\n"); goto error; } if (xmlMemTraceBlockAt == ptr) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "%p : Freed()\n", xmlMemTraceBlockAt); xmlMallocBreakpoint(); } @@ -425,7 +425,7 @@ xmlMemFree(void *ptr) return; error: - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlMemFree(%p) error\n", ptr); xmlMallocBreakpoint(); return; @@ -453,7 +453,7 @@ xmlMemStrdupLoc(const char *str, const char *file, int line) TEST_POINT if (size > (MAX_SIZE_T - RESERVE_SIZE)) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "xmlMemStrdupLoc : Unsigned overflow\n"); return(NULL); } @@ -486,7 +486,7 @@ xmlMemStrdupLoc(const char *str, const char *file, int line) TEST_POINT if (xmlMemTraceBlockAt == s) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "%p : Strdup() Ok\n", xmlMemTraceBlockAt); xmlMallocBreakpoint(); } @@ -733,7 +733,7 @@ static void debugmem_list_delete(MEMHDR *p) static void debugmem_tag_error(void *p) { - xmlGenericError(xmlGenericErrorContext, + fprintf(stderr, "Memory tag error occurs :%p \n\t bye\n", p); #ifdef MEM_LIST if (stderr) diff --git a/xmlmodule.c b/xmlmodule.c index d8bce329..dd2fd7a5 100644 --- a/xmlmodule.c +++ b/xmlmodule.c @@ -41,27 +41,6 @@ static int xmlModulePlatformSymbol(void *handle, const char *name, void **result * * ************************************************************************/ -/** - * xmlModuleErrMemory: - * @extra: extra information - * - * Handle an out of memory condition - */ -static void -xmlModuleErrMemory(xmlModulePtr module, const char *extra) -{ - const char *name = NULL; - - if (module != NULL) { - name = (const char *) module->name; - } - - __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE, - XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, - name, NULL, 0, 0, - "Memory allocation failed : %s\n", extra); -} - /** * xmlModuleOpen: * @name: the module name @@ -82,10 +61,8 @@ xmlModuleOpen(const char *name, int options ATTRIBUTE_UNUSED) xmlModulePtr module; module = (xmlModulePtr) xmlMalloc(sizeof(xmlModule)); - if (module == NULL) { - xmlModuleErrMemory(NULL, "creating module"); + if (module == NULL) return (NULL); - } memset(module, 0, sizeof(xmlModule)); @@ -93,9 +70,6 @@ xmlModuleOpen(const char *name, int options ATTRIBUTE_UNUSED) if (module->handle == NULL) { xmlFree(module); - __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE, - XML_MODULE_OPEN, XML_ERR_FATAL, NULL, 0, 0, - name, NULL, 0, 0, "failed to open %s\n", name); return(NULL); } @@ -122,23 +96,13 @@ xmlModuleSymbol(xmlModulePtr module, const char *name, void **symbol) { int rc = -1; - if ((NULL == module) || (symbol == NULL) || (name == NULL)) { - __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE, - XML_MODULE_OPEN, XML_ERR_FATAL, NULL, 0, 0, - NULL, NULL, 0, 0, "null parameter\n"); + if ((NULL == module) || (symbol == NULL) || (name == NULL)) return rc; - } rc = xmlModulePlatformSymbol(module->handle, name, symbol); - if (rc == -1) { - __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE, - XML_MODULE_OPEN, XML_ERR_FATAL, NULL, 0, 0, - name, NULL, 0, 0, - "failed to find symbol: %s\n", - (name == NULL ? "NULL" : name)); + if (rc == -1) return rc; - } return rc; } @@ -158,22 +122,13 @@ xmlModuleClose(xmlModulePtr module) { int rc; - if (NULL == module) { - __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE, - XML_MODULE_CLOSE, XML_ERR_FATAL, NULL, 0, 0, - NULL, NULL, 0, 0, "null module pointer\n"); + if (NULL == module) return -1; - } rc = xmlModulePlatformClose(module->handle); - if (rc != 0) { - __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE, - XML_MODULE_CLOSE, XML_ERR_FATAL, NULL, 0, 0, - (const char *) module->name, NULL, 0, 0, - "failed to close: %s\n", module->name); + if (rc != 0) return -2; - } rc = xmlModuleFree(module); return (rc); @@ -192,12 +147,8 @@ xmlModuleClose(xmlModulePtr module) int xmlModuleFree(xmlModulePtr module) { - if (NULL == module) { - __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_MODULE, - XML_MODULE_CLOSE, XML_ERR_FATAL, NULL, 0, NULL, - NULL, NULL, 0, 0, "null module pointer\n"); + if (NULL == module) return -1; - } xmlFree(module->name); xmlFree(module);