1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-02-05 05:47:00 +03:00

catalog: Improve error handling

Handle malloc failures from xmlRaiseError.

Remove arguments from memor error handler.

Remove TODO macro.

Make debugging code print to stderr instead of xmlGenericError.
This commit is contained in:
Nick Wellnhofer 2023-12-18 20:54:26 +01:00
parent 06c00f6582
commit 6cb8420a9e

149
catalog.c
View File

@ -16,6 +16,7 @@
#include "libxml.h"
#ifdef LIBXML_CATALOG_ENABLED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_SYS_STAT_H
@ -49,21 +50,6 @@
# define PATH_SEPARATOR ':'
#endif
/**
* TODO:
*
* macro to flag unimplemented blocks
* XML_CATALOG_PREFER user env to select between system/public preferred
* option. C.f. Richard Tobin <richard@cogsci.ed.ac.uk>
*> Just FYI, I am using an environment variable XML_CATALOG_PREFER with
*> values "system" and "public". I have made the default be "system" to
*> match yours.
*/
#define TODO \
xmlGenericError(xmlGenericErrorContext, \
"Unimplemented block at %s:%d\n", \
__FILE__, __LINE__);
#define XML_URN_PUBID "urn:publicid:"
#define XML_CATAL_BREAK ((xmlChar *) -1)
#ifndef XML_XML_DEFAULT_CATALOG
@ -212,12 +198,9 @@ static int xmlCatalogInitialized = 0;
* Handle an out of memory condition
*/
static void
xmlCatalogErrMemory(const char *extra)
xmlCatalogErrMemory(void)
{
__xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_CATALOG,
XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0,
extra, NULL, NULL, 0, 0,
"Memory allocation failed : %s\n", extra);
xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_CATALOG, NULL);
}
/**
@ -234,11 +217,15 @@ xmlCatalogErr(xmlCatalogEntryPtr catal, xmlNodePtr node, int error,
const char *msg, const xmlChar *str1, const xmlChar *str2,
const xmlChar *str3)
{
__xmlRaiseError(NULL, NULL, NULL, catal, node, XML_FROM_CATALOG,
error, XML_ERR_ERROR, NULL, 0,
(const char *) str1, (const char *) str2,
(const char *) str3, 0, 0,
msg, str1, str2, str3);
int res;
res = __xmlRaiseError(NULL, NULL, NULL, catal, node,
XML_FROM_CATALOG, error, XML_ERR_ERROR, NULL, 0,
(const char *) str1, (const char *) str2,
(const char *) str3, 0, 0,
msg, str1, str2, str3);
if (res < 0)
xmlCatalogErrMemory();
}
@ -270,7 +257,7 @@ xmlNewCatalogEntry(xmlCatalogEntryType type, const xmlChar *name,
ret = (xmlCatalogEntryPtr) xmlMalloc(sizeof(xmlCatalogEntry));
if (ret == NULL) {
xmlCatalogErrMemory("allocating catalog entry");
xmlCatalogErrMemory();
return(NULL);
}
ret->next = NULL;
@ -328,13 +315,13 @@ xmlFreeCatalogEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
if (xmlDebugCatalogs) {
if (ret->name != NULL)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Free catalog entry %s\n", ret->name);
else if (ret->value != NULL)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Free catalog entry %s\n", ret->value);
else
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Free catalog entry\n");
}
@ -408,7 +395,7 @@ xmlCreateNewCatalog(xmlCatalogType type, xmlCatalogPrefer prefer) {
ret = (xmlCatalogPtr) xmlMalloc(sizeof(xmlCatalog));
if (ret == NULL) {
xmlCatalogErrMemory("allocating catalog");
xmlCatalogErrMemory();
return(NULL);
}
memset(ret, 0, sizeof(xmlCatalog));
@ -792,7 +779,7 @@ xmlConvertSGMLCatalog(xmlCatalogPtr catal) {
return(-1);
if (xmlDebugCatalogs) {
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Converting SGML catalog to XML\n");
}
xmlHashScan(catal->sgml, xmlCatalogConvertEntry, &catal);
@ -890,7 +877,7 @@ xmlParseCatalogFile(const char *filename) {
ctxt = xmlNewParserCtxt();
if (ctxt == NULL) {
xmlCatalogErrMemory("allocating parser context");
xmlCatalogErrMemory();
return(NULL);
}
@ -986,7 +973,7 @@ xmlLoadFileContent(const char *filename)
#endif
content = (xmlChar*)xmlMallocAtomic(size + 10);
if (content == NULL) {
xmlCatalogErrMemory("allocating catalog data");
xmlCatalogErrMemory();
#ifdef HAVE_STAT
close(fd);
#else
@ -1171,10 +1158,10 @@ xmlParseXMLCatalogOneNode(xmlNodePtr cur, xmlCatalogEntryType type,
if (URL != NULL) {
if (xmlDebugCatalogs > 1) {
if (nameValue != NULL)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Found %s: '%s' '%s'\n", name, nameValue, URL);
else
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Found %s: '%s'\n", name, URL);
}
ret = xmlNewCatalogEntry(type, nameValue, uriValue, URL, prefer, cgroup);
@ -1343,13 +1330,13 @@ xmlParseXMLCatalogFile(xmlCatalogPrefer prefer, const xmlChar *filename) {
doc = xmlParseCatalogFile((const char *) filename);
if (doc == NULL) {
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Failed to parse catalog %s\n", filename);
return(NULL);
}
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"%d Parsing catalog %s\n", xmlGetThreadId(), filename);
cur = xmlDocGetRootElement(doc);
@ -1422,7 +1409,7 @@ xmlFetchXMLCatalogFile(xmlCatalogEntryPtr catal) {
xmlHashLookup(xmlCatalogXMLFiles, catal->URL);
if (doc != NULL) {
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Found %s in file hash\n", catal->URL);
if (catal->type == XML_CATA_CATALOG)
@ -1434,7 +1421,7 @@ xmlFetchXMLCatalogFile(xmlCatalogEntryPtr catal) {
return(0);
}
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"%s not found in file hash\n", catal->URL);
}
@ -1461,7 +1448,7 @@ xmlFetchXMLCatalogFile(xmlCatalogEntryPtr catal) {
xmlCatalogXMLFiles = xmlHashCreate(10);
if (xmlCatalogXMLFiles != NULL) {
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"%s added to file hash\n", catal->URL);
xmlHashAddEntry(xmlCatalogXMLFiles, catal->URL, doc);
}
@ -1507,7 +1494,7 @@ xmlAddXMLCatalog(xmlCatalogEntryPtr catal, const xmlChar *type,
typ = xmlGetXMLCatalogEntryType(type);
if (typ == XML_CATA_NONE) {
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Failed to add unknown element %s to catalog\n", type);
return(-1);
}
@ -1521,7 +1508,7 @@ xmlAddXMLCatalog(xmlCatalogEntryPtr catal, const xmlChar *type,
if ((orig != NULL) && (cur->type == typ) &&
(xmlStrEqual(orig, cur->name))) {
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Updating element %s to catalog\n", type);
if (cur->value != NULL)
xmlFree(cur->value);
@ -1537,7 +1524,7 @@ xmlAddXMLCatalog(xmlCatalogEntryPtr catal, const xmlChar *type,
}
}
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Adding element %s to catalog\n", type);
if (cur == NULL)
catal->children = xmlNewCatalogEntry(typ, orig, replace,
@ -1589,10 +1576,10 @@ xmlDelXMLCatalog(xmlCatalogEntryPtr catal, const xmlChar *value) {
(xmlStrEqual(value, cur->value))) {
if (xmlDebugCatalogs) {
if (cur->name != NULL)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Removing element %s from catalog\n", cur->name);
else
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Removing element %s from catalog\n", cur->value);
}
cur->type = XML_CATA_REMOVED;
@ -1648,7 +1635,7 @@ xmlCatalogXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
case XML_CATA_SYSTEM:
if (xmlStrEqual(sysID, cur->name)) {
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Found system match %s, using %s\n",
cur->name, cur->URL);
catal->depth--;
@ -1677,7 +1664,7 @@ xmlCatalogXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
}
if (rewrite != NULL) {
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Using rewriting rule %s\n", rewrite->name);
ret = xmlStrdup(rewrite->URL);
if (ret != NULL)
@ -1712,7 +1699,7 @@ xmlCatalogXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
}
if (cur->children != NULL) {
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Trying system delegate %s\n", cur->URL);
ret = xmlCatalogListXMLResolve(
cur->children, NULL, sysID);
@ -1742,7 +1729,7 @@ xmlCatalogXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
case XML_CATA_PUBLIC:
if (xmlStrEqual(pubID, cur->name)) {
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Found public match %s\n", cur->name);
catal->depth--;
return(xmlStrdup(cur->URL));
@ -1791,7 +1778,7 @@ xmlCatalogXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
}
if (cur->children != NULL) {
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Trying public delegate %s\n", cur->URL);
ret = xmlCatalogListXMLResolve(
cur->children, pubID, NULL);
@ -1881,7 +1868,7 @@ xmlCatalogXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI) {
case XML_CATA_URI:
if (xmlStrEqual(URI, cur->name)) {
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Found URI match %s\n", cur->name);
return(xmlStrdup(cur->URL));
}
@ -1908,7 +1895,7 @@ xmlCatalogXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI) {
}
if (rewrite != NULL) {
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Using rewriting rule %s\n", rewrite->name);
ret = xmlStrdup(rewrite->URL);
if (ret != NULL)
@ -1943,7 +1930,7 @@ xmlCatalogXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI) {
}
if (cur->children != NULL) {
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Trying URI delegate %s\n", cur->URL);
ret = xmlCatalogListXMLResolveURI(
cur->children, URI);
@ -2012,10 +1999,10 @@ xmlCatalogListXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
urnID = xmlCatalogUnWrapURN(pubID);
if (xmlDebugCatalogs) {
if (urnID == NULL)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Public URN ID %s expanded to NULL\n", pubID);
else
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Public URN ID expanded to %s\n", urnID);
}
ret = xmlCatalogListXMLResolve(catal, urnID, sysID);
@ -2029,10 +2016,10 @@ xmlCatalogListXMLResolve(xmlCatalogEntryPtr catal, const xmlChar *pubID,
urnID = xmlCatalogUnWrapURN(sysID);
if (xmlDebugCatalogs) {
if (urnID == NULL)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"System URN ID %s expanded to NULL\n", sysID);
else
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"System URN ID expanded to %s\n", urnID);
}
if (pubID == NULL)
@ -2096,10 +2083,10 @@ xmlCatalogListXMLResolveURI(xmlCatalogEntryPtr catal, const xmlChar *URI) {
urnID = xmlCatalogUnWrapURN(URI);
if (xmlDebugCatalogs) {
if (urnID == NULL)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"URN ID %s expanded to NULL\n", URI);
else
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"URN ID expanded to %s\n", urnID);
}
ret = xmlCatalogListXMLResolve(catal, urnID, NULL);
@ -2186,7 +2173,7 @@ xmlParseSGMLCatalogPubid(const xmlChar *cur, xmlChar **id) {
}
buf = (xmlChar *) xmlMallocAtomic(size);
if (buf == NULL) {
xmlCatalogErrMemory("allocating public ID");
xmlCatalogErrMemory();
return(NULL);
}
while (IS_PUBIDCHAR_CH(*cur) || (*cur == '?')) {
@ -2198,7 +2185,7 @@ xmlParseSGMLCatalogPubid(const xmlChar *cur, xmlChar **id) {
size *= 2;
tmp = (xmlChar *) xmlRealloc(buf, size);
if (tmp == NULL) {
xmlCatalogErrMemory("allocating public ID");
xmlCatalogErrMemory();
xmlFree(buf);
return(NULL);
}
@ -2778,7 +2765,7 @@ xmlACatalogResolveSystem(xmlCatalogPtr catal, const xmlChar *sysID) {
return(NULL);
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Resolve sysID %s\n", sysID);
if (catal->type == XML_XML_CATALOG_TYPE) {
@ -2813,7 +2800,7 @@ xmlACatalogResolvePublic(xmlCatalogPtr catal, const xmlChar *pubID) {
return(NULL);
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Resolve pubID %s\n", pubID);
if (catal->type == XML_XML_CATALOG_TYPE) {
@ -2852,13 +2839,13 @@ xmlACatalogResolve(xmlCatalogPtr catal, const xmlChar * pubID,
if (xmlDebugCatalogs) {
if ((pubID != NULL) && (sysID != NULL)) {
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Resolve: pubID %s sysID %s\n", pubID, sysID);
} else if (pubID != NULL) {
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Resolve: pubID %s\n", pubID);
} else {
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Resolve: sysID %s\n", sysID);
}
}
@ -2895,7 +2882,7 @@ xmlACatalogResolveURI(xmlCatalogPtr catal, const xmlChar *URI) {
return(NULL);
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Resolve URI %s\n", URI);
if (catal->type == XML_XML_CATALOG_TYPE) {
@ -3272,7 +3259,7 @@ xmlCatalogCleanup(void) {
xmlRMutexLock(xmlCatalogMutex);
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Catalogs cleanup\n");
if (xmlCatalogXMLFiles != NULL)
xmlHashFree(xmlCatalogXMLFiles, xmlFreeCatalogHashEntryList);
@ -3500,19 +3487,19 @@ xmlCatalogSetDefaults(xmlCatalogAllow allow) {
if (xmlDebugCatalogs) {
switch (allow) {
case XML_CATA_ALLOW_NONE:
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Disabling catalog usage\n");
break;
case XML_CATA_ALLOW_GLOBAL:
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Allowing only global catalogs\n");
break;
case XML_CATA_ALLOW_DOCUMENT:
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Allowing only catalogs from the document\n");
break;
case XML_CATA_ALLOW_ALL:
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Allowing all catalogs\n");
break;
}
@ -3540,11 +3527,11 @@ xmlCatalogSetDefaultPrefer(xmlCatalogPrefer prefer) {
if (xmlDebugCatalogs) {
switch (prefer) {
case XML_CATA_PREFER_PUBLIC:
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Setting catalog preference to PUBLIC\n");
break;
case XML_CATA_PREFER_SYSTEM:
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Setting catalog preference to SYSTEM\n");
break;
default:
@ -3620,7 +3607,7 @@ xmlCatalogAddLocal(void *catalogs, const xmlChar *URL) {
return(catalogs);
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Adding document catalog %s\n", URL);
add = xmlNewCatalogEntry(XML_CATA_CATALOG, NULL, URL, NULL,
@ -3664,13 +3651,13 @@ xmlCatalogLocalResolve(void *catalogs, const xmlChar *pubID,
if (xmlDebugCatalogs) {
if ((pubID != NULL) && (sysID != NULL)) {
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Local Resolve: pubID %s sysID %s\n", pubID, sysID);
} else if (pubID != NULL) {
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Local Resolve: pubID %s\n", pubID);
} else {
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Local Resolve: sysID %s\n", sysID);
}
}
@ -3707,7 +3694,7 @@ xmlCatalogLocalResolveURI(void *catalogs, const xmlChar *URI) {
return(NULL);
if (xmlDebugCatalogs)
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Resolve URI %s\n", URI);
catal = (xmlCatalogEntryPtr) catalogs;
@ -3743,7 +3730,7 @@ xmlCatalogGetSystem(const xmlChar *sysID) {
xmlInitializeCatalog();
if (msg == 0) {
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Use of deprecated xmlCatalogGetSystem() call\n");
msg++;
}
@ -3787,7 +3774,7 @@ xmlCatalogGetPublic(const xmlChar *pubID) {
xmlInitializeCatalog();
if (msg == 0) {
xmlGenericError(xmlGenericErrorContext,
fprintf(stderr,
"Use of deprecated xmlCatalogGetPublic() call\n");
msg++;
}