From 79e119954c949754e2d9c243c084af1f9c9bc989 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Mon, 15 Jul 2024 19:43:28 +0200 Subject: [PATCH] error: Make xmlLastError const --- error.c | 20 ++++++++++++-------- globals.c | 31 +++++++++++++++++++++++++------ include/libxml/xmlerror.h | 10 +++++++++- include/private/globals.h | 3 +++ 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/error.c b/error.c index e70c0d37..fc28bb69 100644 --- a/error.c +++ b/error.c @@ -17,6 +17,7 @@ #include #include "private/error.h" +#include "private/globals.h" #include "private/string.h" /************************************************************************ @@ -635,7 +636,7 @@ void xmlRaiseMemoryError(xmlStructuredErrorFunc schannel, xmlGenericErrorFunc channel, void *data, int domain, xmlError *error) { - xmlError *lastError = &xmlLastError; + xmlError *lastError = xmlGetLastErrorInternal(); xmlResetLastError(); lastError->domain = domain; @@ -694,7 +695,7 @@ xmlVRaiseError(xmlStructuredErrorFunc schannel, { xmlParserCtxtPtr ctxt = NULL; /* xmlLastError is a macro retrieving the per-thread global. */ - xmlErrorPtr lastError = &xmlLastError; + xmlErrorPtr lastError = xmlGetLastErrorInternal(); xmlErrorPtr to = lastError; if (code == XML_ERR_OK) @@ -919,9 +920,11 @@ xmlParserValidityWarning(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) const xmlError * xmlGetLastError(void) { - if (xmlLastError.code == XML_ERR_OK) - return (NULL); - return (&xmlLastError); + const xmlError *error = xmlGetLastErrorInternal(); + + if (error->code == XML_ERR_OK) + return(NULL); + return(error); } /** @@ -960,9 +963,10 @@ xmlResetError(xmlErrorPtr err) void xmlResetLastError(void) { - if (xmlLastError.code == XML_ERR_OK) - return; - xmlResetError(&xmlLastError); + xmlError *error = xmlGetLastErrorInternal(); + + if (error->code != XML_ERR_OK) + xmlResetError(error); } /** diff --git a/globals.c b/globals.c index 13daefec..1dfdfb56 100644 --- a/globals.c +++ b/globals.c @@ -79,6 +79,8 @@ struct _xmlGlobalState { unsigned localRngState[2]; #endif + xmlError lastError; + #define XML_OP XML_DECLARE_MEMBER XML_GLOBALS_ALLOC XML_GLOBALS_ERROR @@ -679,7 +681,7 @@ xmlFreeGlobalState(void *state) * But the xmlError struct is fully public and widely used, * so changes are dangerous. */ - xmlResetError(&(gs->gs_xmlLastError)); + xmlResetError(&gs->lastError); #ifndef USE_TLS free(state); #endif @@ -723,10 +725,10 @@ static void xmlInitGlobalState(xmlGlobalStatePtr gs) { xmlMutexLock(&xmlThrDefMutex); -#ifdef LIBXML_THREAD_ENABLED gs->localRngState[0] = xmlGlobalRandom(); gs->localRngState[1] = xmlGlobalRandom(); -#endif + + memset(&gs->lastError, 0, sizeof(xmlError)); gs->gs_xmlDoValidityCheckingDefaultValue = xmlDoValidityCheckingDefaultValueThrDef; @@ -761,7 +763,6 @@ xmlInitGlobalState(xmlGlobalStatePtr gs) { xmlParserInputBufferCreateFilenameValueThrDef; gs->gs_xmlOutputBufferCreateFilenameValue = xmlOutputBufferCreateFilenameValueThrDef; - memset(&gs->gs_xmlLastError, 0, sizeof(xmlError)); xmlMutexUnlock(&xmlThrDefMutex); @@ -857,7 +858,14 @@ XML_GLOBALS_PARSER XML_GLOBALS_TREE #undef XML_OP -#ifdef LIBXML_THREAD_ENABLED +const xmlError * +__xmlLastError(void) { + if (IS_MAIN_THREAD) + return(&xmlLastError); + else + return(&xmlGetThreadLocalStorage(0)->lastError); +} + /** * xmlGetLocalRngState: * @@ -870,7 +878,6 @@ xmlGetLocalRngState(void) { else return(xmlGetThreadLocalStorage(0)->localRngState); } -#endif /* For backward compatibility */ @@ -941,6 +948,18 @@ xmlCheckThreadLocalStorage(void) { return(0); } +xmlError * +xmlGetLastErrorInternal(void) { +#ifdef LIBXML_THREAD_ENABLED + if (IS_MAIN_THREAD) + return(&xmlLastError); + else + return(&xmlGetThreadLocalStorage(0)->lastError); +#else + return(&xmlLastError); +#endif +} + /** DOC_DISABLE */ /** diff --git a/include/libxml/xmlerror.h b/include/libxml/xmlerror.h index 36381bec..d847bfde 100644 --- a/include/libxml/xmlerror.h +++ b/include/libxml/xmlerror.h @@ -865,8 +865,16 @@ typedef void (*xmlGenericErrorFunc) (void *ctx, typedef void (*xmlStructuredErrorFunc) (void *userData, const xmlError *error); /** DOC_DISABLE */ +#if defined(LIBXML_THREAD_ENABLED) +XML_DEPRECATED +XMLPUBFUN const xmlError * +__xmlLastError(void); +#elif !defined(IN_LIBXML) +XML_DEPRECATED +XMLPUBVAR const xmlError xmlLastError; +#endif + #define XML_GLOBALS_ERROR \ - XML_OP(xmlLastError, xmlError, XML_DEPRECATED) \ XML_OP(xmlGenericError, xmlGenericErrorFunc, XML_NO_ATTR) \ XML_OP(xmlGenericErrorContext, void *, XML_NO_ATTR) \ XML_OP(xmlStructuredError, xmlStructuredErrorFunc, XML_NO_ATTR) \ diff --git a/include/private/globals.h b/include/private/globals.h index 828b6d50..d111701e 100644 --- a/include/private/globals.h +++ b/include/private/globals.h @@ -6,6 +6,9 @@ xmlInitGlobalsInternal(void); XML_HIDDEN void xmlCleanupGlobalsInternal(void); +XML_HIDDEN xmlError * +xmlGetLastErrorInternal(void); + #ifdef LIBXML_THREAD_ENABLED XML_HIDDEN unsigned * xmlGetLocalRngState(void);