util/xml: open XML files before calling libxml2

Libxml2 has awful error reporting behaviour when reading files. When
we fail to load a file from the test driver we see:

  $ virsh -c test:///wibble.xml
  I/O warning : failed to load external entity "/wibble.xml"
  error: failed to connect to the hypervisor
  error: XML error: failed to parse xml document '/wibble.xml'

where the I/O warning line is something printed by libxml2 itself,
which also lacks any useful detail.

Switching to our own file reading code we can massively improve
things:

  $ ./build/tools/virsh -c test:///wibble.xml
  error: failed to connect to the hypervisor
  error: Failed to open file '/wibble.xml': No such file or directory

Using 10 MB as an upper limit on XML file size ought to be sufficient
for any XML files libvirt is reading.

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-07 15:54:53 +01:00
parent 7f2fd38ee7
commit 94b393dd6e
2 changed files with 6 additions and 4 deletions

View File

@ -1143,6 +1143,7 @@ virXMLParseHelper(int domcode,
xmlNodePtr rootnode;
const char *docname;
int parseFlags = XML_PARSE_NONET | XML_PARSE_NOWARNING;
g_autofree char *xmlStrPtr = NULL;
if (filename)
docname = filename;
@ -1166,10 +1167,11 @@ virXMLParseHelper(int domcode,
}
if (filename) {
xml = xmlCtxtReadFile(pctxt, filename, NULL, parseFlags);
} else {
xml = xmlCtxtReadDoc(pctxt, BAD_CAST xmlStr, url, NULL, parseFlags);
if (virFileReadAll(filename, 1024*1024*10, &xmlStrPtr) < 0)
return NULL;
xmlStr = xmlStrPtr;
}
xml = xmlCtxtReadDoc(pctxt, BAD_CAST xmlStr, url, NULL, parseFlags);
if (!xml) {
if (virGetLastErrorCode() == VIR_ERR_OK) {

View File

@ -1 +1 @@
XML error: failed to parse xml document 'ABS_SRCDIR/qemuxmlconfdata/nonexistent-file.xml'
Failed to open file 'ABS_SRCDIR/qemuxmlconfdata/nonexistent-file.xml': No such file or directory