[PATCH v2 1/3] util/xml: fix extraction of XML context

Daniel P. Berrangé posted 3 patches 1 year, 3 months ago
[PATCH v2 1/3] util/xml: fix extraction of XML context
Posted by Daniel P. Berrangé 1 year, 3 months ago
Currently given an input of '<dom\n' we emit an error:

  error: Failed to define domain from tests/qemuxmlconfdata/broken-xml-invalid.xml
  error: at line 2: Couldn't find end of Start Tag dom line 1
  (null)
  ^

With this fix we emit:

  error: Failed to define domain from tests/qemuxmlconfdata/broken-xml-invalid.xml
  error: at line 2: Couldn't find end of Start Tag dom line 1
  <dom
  ----^

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 src/util/virxml.c                                          | 4 ++++
 tests/qemuxmlconfdata/broken-xml-invalid.x86_64-latest.err | 4 ++--
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/util/virxml.c b/src/util/virxml.c
index 51173303fe..91f736bf39 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -1047,6 +1047,10 @@ catchXMLError(void *ctx, const char *msg G_GNUC_UNUSED, ...)
     cur = ctxt->input->cur;
     base = ctxt->input->base;
 
+    /* skip backwards over NUL terminator */
+    if ((cur > base) && *cur == '\0')
+        cur--;
+
     /* skip backwards over any end-of-lines */
     while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r')))
         cur--;
diff --git a/tests/qemuxmlconfdata/broken-xml-invalid.x86_64-latest.err b/tests/qemuxmlconfdata/broken-xml-invalid.x86_64-latest.err
index 35a1801371..a3bacd5d3a 100644
--- a/tests/qemuxmlconfdata/broken-xml-invalid.x86_64-latest.err
+++ b/tests/qemuxmlconfdata/broken-xml-invalid.x86_64-latest.err
@@ -1,3 +1,3 @@
 ABS_SRCDIR/qemuxmlconfdata/broken-xml-invalid.xml:2: Couldn't find end of Start Tag dom line 1
-(null)
-^
+<dom
+----^
-- 
2.46.0
Re: [PATCH v2 1/3] util/xml: fix extraction of XML context
Posted by Peter Krempa 1 year, 3 months ago
On Tue, Oct 22, 2024 at 10:27:52 +0100, Daniel P. Berrangé wrote:
> Currently given an input of '<dom\n' we emit an error:
> 
>   error: Failed to define domain from tests/qemuxmlconfdata/broken-xml-invalid.xml
>   error: at line 2: Couldn't find end of Start Tag dom line 1
>   (null)
>   ^
> 
> With this fix we emit:
> 
>   error: Failed to define domain from tests/qemuxmlconfdata/broken-xml-invalid.xml
>   error: at line 2: Couldn't find end of Start Tag dom line 1
>   <dom
>   ----^
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  src/util/virxml.c                                          | 4 ++++
>  tests/qemuxmlconfdata/broken-xml-invalid.x86_64-latest.err | 4 ++--
>  2 files changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/src/util/virxml.c b/src/util/virxml.c
> index 51173303fe..91f736bf39 100644
> --- a/src/util/virxml.c
> +++ b/src/util/virxml.c
> @@ -1047,6 +1047,10 @@ catchXMLError(void *ctx, const char *msg G_GNUC_UNUSED, ...)
>      cur = ctxt->input->cur;
>      base = ctxt->input->base;
>  
> +    /* skip backwards over NUL terminator */

Preferrably also note "why" we're skipping backwards.

> +    if ((cur > base) && *cur == '\0')
> +        cur--;
> +
>      /* skip backwards over any end-of-lines */
>      while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r')))
>          cur--;
> diff --git a/tests/qemuxmlconfdata/broken-xml-invalid.x86_64-latest.err b/tests/qemuxmlconfdata/broken-xml-invalid.x86_64-latest.err
> index 35a1801371..a3bacd5d3a 100644
> --- a/tests/qemuxmlconfdata/broken-xml-invalid.x86_64-latest.err
> +++ b/tests/qemuxmlconfdata/broken-xml-invalid.x86_64-latest.err
> @@ -1,3 +1,3 @@
>  ABS_SRCDIR/qemuxmlconfdata/broken-xml-invalid.xml:2: Couldn't find end of Start Tag dom line 1
> -(null)
> -^
> +<dom
> +----^

Reviewed-by: Peter Krempa <pkrempa@redhat.com>