From e87eeefb3e4380b553e491ebe05ba0af17531399 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Wed, 14 Apr 2021 13:03:58 +0200 Subject: [PATCH] virXMLParseHelper: Rework error reporting Move the reporting of parsing error on the error path of the parser as other code paths report their own errors already. Additionally prefer printing the 'url' as document name if provided instead of "[inline data]" as that usually gives a better hint at least which kind of XML is being parsed. Signed-off-by: Peter Krempa Reviewed-by: Tim Wiederhake --- src/util/virxml.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/util/virxml.c b/src/util/virxml.c index 117f50f2bf..029b3d646e 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -788,6 +788,14 @@ virXMLParseHelper(int domcode, struct virParserData private; xmlParserCtxtPtr pctxt; xmlDocPtr xml = NULL; + const char *docname; + + if (filename) + docname = filename; + else if (url) + docname = url; + else + docname = "[inline data]"; /* Set up a parser context so we can catch the details of XML errors. */ pctxt = xmlNewParserCtxt(); @@ -807,8 +815,16 @@ virXMLParseHelper(int domcode, XML_PARSE_NONET | XML_PARSE_NOWARNING); } - if (!xml) + + if (!xml) { + if (virGetLastErrorCode() == VIR_ERR_OK) { + virGenericReportError(domcode, VIR_ERR_XML_ERROR, + _("failed to parse xml document '%s'"), + docname); + } + goto error; + } if (xmlDocGetRootElement(xml) == NULL) { virGenericReportError(domcode, VIR_ERR_INTERNAL_ERROR, @@ -832,11 +848,6 @@ virXMLParseHelper(int domcode, xmlFreeDoc(xml); xml = NULL; - if (virGetLastErrorCode() == VIR_ERR_OK) { - virGenericReportError(domcode, VIR_ERR_XML_ERROR, - _("failed to parse xml document '%s'"), - filename ? filename : "[inline data]"); - } goto cleanup; }