1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2026-01-26 21:41:34 +03:00

error: Make xmlGetLastError return a const error

This is a slight break of the API, but users really shouldn't modify the
global error struct. The goal is to make xmlLastError use static buffers
for its strings eventually. This should warn people if they're abusing
the struct.
This commit is contained in:
Nick Wellnhofer
2023-09-21 23:52:52 +02:00
parent fc26934eb0
commit 45470611b0
8 changed files with 29 additions and 11 deletions

View File

@@ -10144,7 +10144,7 @@ Could we use @subtypes for this?'/>
</function> </function>
<function name='xmlGetLastError' file='xmlerror' module='error'> <function name='xmlGetLastError' file='xmlerror' module='error'>
<info>Get the last global error registered. This is per thread if compiled with thread support.</info> <info>Get the last global error registered. This is per thread if compiled with thread support.</info>
<return type='xmlErrorPtr' info='NULL if no error occurred or a pointer to the error'/> <return type='const xmlError *' info='NULL if no error occurred or a pointer to the error'/>
</function> </function>
<function name='xmlGetLineNo' file='tree' module='tree'> <function name='xmlGetLineNo' file='tree' module='tree'>
<info>Get line number of @node. Try to override the limitation of lines being store in 16 bits ints if XML_PARSE_BIG_LINES parser option was used</info> <info>Get line number of @node. Try to override the limitation of lines being store in 16 bits ints if XML_PARSE_BIG_LINES parser option was used</info>

View File

@@ -880,9 +880,9 @@ xmlParserValidityWarning(void *ctx, const char *msg, ...)
* Get the last global error registered. This is per thread if compiled * Get the last global error registered. This is per thread if compiled
* with thread support. * with thread support.
* *
* Returns NULL if no error occurred or a pointer to the error * Returns a pointer to the error
*/ */
xmlErrorPtr const xmlError *
xmlGetLastError(void) xmlGetLastError(void)
{ {
if (xmlLastError.code == XML_ERR_OK) if (xmlLastError.code == XML_ERR_OK)
@@ -981,7 +981,7 @@ xmlCtxtResetLastError(void *ctx)
* Returns 0 in case of success and -1 in case of error. * Returns 0 in case of success and -1 in case of error.
*/ */
int int
xmlCopyError(xmlErrorPtr from, xmlErrorPtr to) { xmlCopyError(const xmlError *from, xmlErrorPtr to) {
char *message, *file, *str1, *str2, *str3; char *message, *file, *str1, *str2, *str3;
if ((from == NULL) || (to == NULL)) if ((from == NULL) || (to == NULL))

View File

@@ -928,7 +928,7 @@ XMLPUBFUN void
/* /*
* Extended error information routines * Extended error information routines
*/ */
XMLPUBFUN xmlErrorPtr XMLPUBFUN const xmlError *
xmlGetLastError (void); xmlGetLastError (void);
XMLPUBFUN void XMLPUBFUN void
xmlResetLastError (void); xmlResetLastError (void);
@@ -939,7 +939,7 @@ XMLPUBFUN void
XMLPUBFUN void XMLPUBFUN void
xmlResetError (xmlErrorPtr err); xmlResetError (xmlErrorPtr err);
XMLPUBFUN int XMLPUBFUN int
xmlCopyError (xmlErrorPtr from, xmlCopyError (const xmlError *from,
xmlErrorPtr to); xmlErrorPtr to);
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -240,6 +240,7 @@ py_types = {
'xmlCatalogPtr': ('O', "catalog", "xmlCatalogPtr", "xmlCatalogPtr"), 'xmlCatalogPtr': ('O', "catalog", "xmlCatalogPtr", "xmlCatalogPtr"),
'FILE *': ('O', "File", "FILEPtr", "FILE *"), 'FILE *': ('O', "File", "FILEPtr", "FILE *"),
'xmlURIPtr': ('O', "URI", "xmlURIPtr", "xmlURIPtr"), 'xmlURIPtr': ('O', "URI", "xmlURIPtr", "xmlURIPtr"),
'const xmlError *': ('O', "Error", "xmlErrorPtr", "const xmlError *"),
'xmlErrorPtr': ('O', "Error", "xmlErrorPtr", "xmlErrorPtr"), 'xmlErrorPtr': ('O', "Error", "xmlErrorPtr", "xmlErrorPtr"),
'xmlOutputBufferPtr': ('O', "outputBuffer", "xmlOutputBufferPtr", "xmlOutputBufferPtr"), 'xmlOutputBufferPtr': ('O', "outputBuffer", "xmlOutputBufferPtr", "xmlOutputBufferPtr"),
'xmlParserInputBufferPtr': ('O', "inputBuffer", "xmlParserInputBufferPtr", "xmlParserInputBufferPtr"), 'xmlParserInputBufferPtr': ('O', "inputBuffer", "xmlParserInputBufferPtr", "xmlParserInputBufferPtr"),
@@ -737,6 +738,7 @@ classes_type = {
"xmlValidCtxtPtr": ("._o", "ValidCtxt(_obj=%s)", "ValidCtxt"), "xmlValidCtxtPtr": ("._o", "ValidCtxt(_obj=%s)", "ValidCtxt"),
"xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"), "xmlCatalogPtr": ("._o", "catalog(_obj=%s)", "catalog"),
"xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"), "xmlURIPtr": ("._o", "URI(_obj=%s)", "URI"),
"const xmlError *": ("._o", "Error(_obj=%s)", "Error"),
"xmlErrorPtr": ("._o", "Error(_obj=%s)", "Error"), "xmlErrorPtr": ("._o", "Error(_obj=%s)", "Error"),
"xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"), "xmlOutputBufferPtr": ("._o", "outputBuffer(_obj=%s)", "outputBuffer"),
"xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"), "xmlParserInputBufferPtr": ("._o", "inputBuffer(_obj=%s)", "inputBuffer"),

View File

@@ -290,7 +290,7 @@ PyObject * libxml_xmlSchemaPtrWrap(xmlSchemaPtr ctxt);
PyObject * libxml_xmlSchemaParserCtxtPtrWrap(xmlSchemaParserCtxtPtr ctxt); PyObject * libxml_xmlSchemaParserCtxtPtrWrap(xmlSchemaParserCtxtPtr ctxt);
PyObject * libxml_xmlSchemaValidCtxtPtrWrap(xmlSchemaValidCtxtPtr valid); PyObject * libxml_xmlSchemaValidCtxtPtrWrap(xmlSchemaValidCtxtPtr valid);
#endif /* LIBXML_SCHEMAS_ENABLED */ #endif /* LIBXML_SCHEMAS_ENABLED */
PyObject * libxml_xmlErrorPtrWrap(xmlErrorPtr error); PyObject * libxml_xmlErrorPtrWrap(const xmlError *error);
PyObject * libxml_xmlSchemaSetValidErrors(PyObject * self, PyObject * args); PyObject * libxml_xmlSchemaSetValidErrors(PyObject * self, PyObject * args);
PyObject * libxml_xmlRegisterInputCallback(PyObject *self, PyObject *args); PyObject * libxml_xmlRegisterInputCallback(PyObject *self, PyObject *args);
PyObject * libxml_xmlUnregisterInputCallback(PyObject *self, PyObject *args); PyObject * libxml_xmlUnregisterInputCallback(PyObject *self, PyObject *args);

View File

@@ -8,6 +8,7 @@
*/ */
#include "libxml_wrap.h" #include "libxml_wrap.h"
#include <libxml/xpathInternals.h> #include <libxml/xpathInternals.h>
#include <string.h>
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
#define PY_IMPORT_STRING_SIZE PyUnicode_FromStringAndSize #define PY_IMPORT_STRING_SIZE PyUnicode_FromStringAndSize
@@ -963,15 +964,30 @@ libxml_xmlSchemaValidCtxtPtrWrap(xmlSchemaValidCtxtPtr valid)
} }
#endif /* LIBXML_SCHEMAS_ENABLED */ #endif /* LIBXML_SCHEMAS_ENABLED */
static void
libxml_xmlDestructError(PyObject *cap) {
xmlErrorPtr err = (xmlErrorPtr) PyCapsule_GetPointer(cap, "xmlErrorPtr");
xmlResetError(err);
xmlFree(err);
}
PyObject * PyObject *
libxml_xmlErrorPtrWrap(xmlErrorPtr error) libxml_xmlErrorPtrWrap(const xmlError *error)
{ {
PyObject *ret; PyObject *ret;
xmlErrorPtr copy;
if (error == NULL) { if (error == NULL) {
Py_INCREF(Py_None); Py_INCREF(Py_None);
return (Py_None); return (Py_None);
} }
ret = PyCapsule_New((void *) error, (char *) "xmlErrorPtr", NULL); copy = xmlMalloc(sizeof(*copy));
if (copy == NULL) {
Py_INCREF(Py_None);
return (Py_None);
}
memset(copy, 0, sizeof(*copy));
xmlCopyError(error, copy);
ret = PyCapsule_New(copy, "xmlErrorPtr", libxml_xmlDestructError);
return (ret); return (ret);
} }

View File

@@ -249,7 +249,7 @@ xmlconfTestNotNSWF(const char *id, const char *filename, int options) {
nb_errors++; nb_errors++;
ret = 0; ret = 0;
} else { } else {
xmlError *error = xmlGetLastError(); const xmlError *error = xmlGetLastError();
if ((error->code == XML_ERR_OK) || if ((error->code == XML_ERR_OK) ||
(error->domain != XML_FROM_NAMESPACE)) { (error->domain != XML_FROM_NAMESPACE)) {

View File

@@ -10545,7 +10545,7 @@ doc_load:
* TODO: (2.2) is not supported. * TODO: (2.2) is not supported.
*/ */
if (doc == NULL) { if (doc == NULL) {
xmlErrorPtr lerr; const xmlError *lerr;
lerr = xmlGetLastError(); lerr = xmlGetLastError();
/* /*
* Check if this a parser error, or if the document could * Check if this a parser error, or if the document could