[PATCH 03/12] util: xml: Refactor cleanup path in virXMLValidatorInit

Peter Krempa posted 12 patches 3 years, 3 months ago
[PATCH 03/12] util: xml: Refactor cleanup path in virXMLValidatorInit
Posted by Peter Krempa 3 years, 3 months ago
Automatically free 'validator' on errors.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
---
 src/util/virxml.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/src/util/virxml.c b/src/util/virxml.c
index 35c0340779..e9595da97d 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -1608,7 +1608,7 @@ virXMLValidatorRNGErrorIgnore(void *ctx G_GNUC_UNUSED,
 virXMLValidator *
 virXMLValidatorInit(const char *schemafile)
 {
-    virXMLValidator *validator = NULL;
+    g_autoptr(virXMLValidator) validator = NULL;

     validator = g_new0(virXMLValidator, 1);

@@ -1619,7 +1619,7 @@ virXMLValidatorInit(const char *schemafile)
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Unable to create RNG parser for %s"),
                        validator->schemafile);
-        goto error;
+        return NULL;
     }

     xmlRelaxNGSetParserErrors(validator->rngParser,
@@ -1632,25 +1632,22 @@ virXMLValidatorInit(const char *schemafile)
                        _("Unable to parse RNG %s: %s"),
                        validator->schemafile,
                        virBufferCurrentContent(&validator->buf));
-        goto error;
+        return NULL;
     }

     if (!(validator->rngValid = xmlRelaxNGNewValidCtxt(validator->rng))) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Unable to create RNG validation context %s"),
                        validator->schemafile);
-        goto error;
+        return NULL;
     }

     xmlRelaxNGSetValidErrors(validator->rngValid,
                              virXMLValidatorRNGErrorCatch,
                              virXMLValidatorRNGErrorIgnore,
                              &validator->buf);
-    return validator;

- error:
-    virXMLValidatorFree(validator);
-    return NULL;
+    return g_steal_pointer(&validator);
 }


-- 
2.38.1
Re: [PATCH 03/12] util: xml: Refactor cleanup path in virXMLValidatorInit
Posted by Ján Tomko 3 years, 3 months ago
On a Tuesday in 2022, Peter Krempa wrote:
>Automatically free 'validator' on errors.
>
>Signed-off-by: Peter Krempa <pkrempa@redhat.com>
>---
> src/util/virxml.c | 13 +++++--------
> 1 file changed, 5 insertions(+), 8 deletions(-)
>

Reviewed-by: Ján Tomko <jtomko@redhat.com>

Jano