util/xml: don't assume libxml2 has the filename of the document

The libxml2 error handling gets the filename from a libxml2 struct, but
it is better to not assume libxml2 knows the filename being parsed, as
we might have simply provided it a pre-loaded string.

Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2024-08-09 11:30:59 +01:00
parent 0635cd9555
commit 7f2fd38ee7

View File

@ -46,6 +46,7 @@
/* Internal data to be passed to SAX parser and used by error handler. */ /* Internal data to be passed to SAX parser and used by error handler. */
struct virParserData { struct virParserData {
int domcode; int domcode;
const char *filename;
}; };
@ -1022,7 +1023,7 @@ static void
catchXMLError(void *ctx, const char *msg G_GNUC_UNUSED, ...) catchXMLError(void *ctx, const char *msg G_GNUC_UNUSED, ...)
{ {
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
struct virParserData *private = ctxt->_private;
const xmlChar *cur, *base; const xmlChar *cur, *base;
unsigned int n, col; /* GCC warns if signed, because compared with sizeof() */ unsigned int n, col; /* GCC warns if signed, because compared with sizeof() */
int domcode = VIR_FROM_XML; int domcode = VIR_FROM_XML;
@ -1030,6 +1031,10 @@ catchXMLError(void *ctx, const char *msg G_GNUC_UNUSED, ...)
g_autofree char *contextstr = NULL; g_autofree char *contextstr = NULL;
g_autofree char *pointerstr = NULL; g_autofree char *pointerstr = NULL;
const xmlError *lastError = xmlCtxtGetLastError(ctxt); const xmlError *lastError = xmlCtxtGetLastError(ctxt);
const char *filename = NULL;
if (private)
filename = private->filename;
/* conditions for error printing */ /* conditions for error printing */
if (!ctxt || if (!ctxt ||
@ -1040,9 +1045,8 @@ catchXMLError(void *ctx, const char *msg G_GNUC_UNUSED, ...)
lastError->message == NULL) lastError->message == NULL)
return; return;
if (ctxt->_private) if (private)
domcode = ((struct virParserData *) ctxt->_private)->domcode; domcode = private->domcode;
cur = ctxt->input->cur; cur = ctxt->input->cur;
base = ctxt->input->base; base = ctxt->input->base;
@ -1084,10 +1088,10 @@ catchXMLError(void *ctx, const char *msg G_GNUC_UNUSED, ...)
pointerstr = virBufferContentAndReset(&buf); pointerstr = virBufferContentAndReset(&buf);
if (lastError->file) { if (filename) {
virGenericReportError(domcode, VIR_ERR_XML_DETAIL, virGenericReportError(domcode, VIR_ERR_XML_DETAIL,
_("%1$s:%2$d: %3$s%4$s\n%5$s"), _("%1$s:%2$d: %3$s%4$s\n%5$s"),
lastError->file, filename,
lastError->line, lastError->line,
lastError->message, lastError->message,
contextstr, contextstr,
@ -1153,6 +1157,7 @@ virXMLParseHelper(int domcode,
abort(); abort();
private.domcode = domcode; private.domcode = domcode;
private.filename = filename;
pctxt->_private = &private; pctxt->_private = &private;
pctxt->sax->error = catchXMLError; pctxt->sax->error = catchXMLError;