[PATCH] cpu.c: Use g_autoptr and g_autofree in virCPUDataParse

Seeteena Thoufeek posted 1 patch 4 years ago
Test syntax-check failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/1585923756-7256-1-git-send-email-s1seetee@linux.vnet.ibm.com
src/cpu/cpu.c | 18 +++++++-----------
1 file changed, 7 insertions(+), 11 deletions(-)
[PATCH] cpu.c: Use g_autoptr and g_autofree in virCPUDataParse
Posted by Seeteena Thoufeek 4 years ago
Signed-off-by: Seeteena Thoufeek <s1seetee@linux.vnet.ibm.com>
---
 src/cpu/cpu.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/src/cpu/cpu.c b/src/cpu/cpu.c
index 631c755..e556ffe 100644
--- a/src/cpu/cpu.c
+++ b/src/cpu/cpu.c
@@ -772,40 +772,36 @@ virCPUDataPtr
 virCPUDataParse(const char *xmlStr)
 {
     struct cpuArchDriver *driver;
-    xmlDocPtr xml = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlDoc) xml = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     virCPUDataPtr data = NULL;
-    char *arch = NULL;
+    g_autofree char *arch = NULL;
 
     VIR_DEBUG("xmlStr=%s", xmlStr);
 
     if (!(xml = virXMLParseStringCtxt(xmlStr, _("CPU data"), &ctxt))) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("cannot parse CPU data"));
-        goto cleanup;
+        return data;
     }
 
     if (!(arch = virXPathString("string(/cpudata/@arch)", ctxt))) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("missing CPU data architecture"));
-        goto cleanup;
+        return data;
     }
 
     if (!(driver = cpuGetSubDriverByName(arch)))
-        goto cleanup;
+        return data;
 
     if (!driver->dataParse) {
         virReportError(VIR_ERR_NO_SUPPORT,
                        _("cannot parse %s CPU data"), arch);
-        goto cleanup;
+        return data;
     }
 
     data = driver->dataParse(ctxt);
 
- cleanup:
-    xmlXPathFreeContext(ctxt);
-    xmlFreeDoc(xml);
-    VIR_FREE(arch);
     return data;
 }
 
-- 
1.8.3.1


Re: [PATCH] cpu.c: Use g_autoptr and g_autofree in virCPUDataParse
Posted by Ján Tomko 4 years ago
cpu: Use g_autoptr and g_autofree in virCPUDataParse

The first part says which driver/area the patch touches,
no need to put an extension there.

On a Friday in 2020, Seeteena Thoufeek wrote:
>Signed-off-by: Seeteena Thoufeek <s1seetee@linux.vnet.ibm.com>
>---
> src/cpu/cpu.c | 18 +++++++-----------
> 1 file changed, 7 insertions(+), 11 deletions(-)
>
>diff --git a/src/cpu/cpu.c b/src/cpu/cpu.c
>index 631c755..e556ffe 100644
>--- a/src/cpu/cpu.c
>+++ b/src/cpu/cpu.c
>@@ -772,40 +772,36 @@ virCPUDataPtr
> virCPUDataParse(const char *xmlStr)
> {
>     struct cpuArchDriver *driver;
>-    xmlDocPtr xml = NULL;
>-    xmlXPathContextPtr ctxt = NULL;
>+    g_autoptr(xmlDoc) xml = NULL;
>+    g_autoptr(xmlXPathContext) ctxt = NULL;
>     virCPUDataPtr data = NULL;
>-    char *arch = NULL;
>+    g_autofree char *arch = NULL;
>
>     VIR_DEBUG("xmlStr=%s", xmlStr);
>
>     if (!(xml = virXMLParseStringCtxt(xmlStr, _("CPU data"), &ctxt))) {
>         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
>                        _("cannot parse CPU data"));
>-        goto cleanup;
>+        return data;

The point of these refactors is to make the flow of the function easier to
read, so this (and all the other cases except the successful one at the
end) should return NULL;

Jano


>     }
>
>     if (!(arch = virXPathString("string(/cpudata/@arch)", ctxt))) {
>         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
>                        _("missing CPU data architecture"));
>-        goto cleanup;
>+        return data;
>     }
>
>     if (!(driver = cpuGetSubDriverByName(arch)))
>-        goto cleanup;
>+        return data;
>
>     if (!driver->dataParse) {
>         virReportError(VIR_ERR_NO_SUPPORT,
>                        _("cannot parse %s CPU data"), arch);
>-        goto cleanup;
>+        return data;
>     }
>
>     data = driver->dataParse(ctxt);
>
>- cleanup:
>-    xmlXPathFreeContext(ctxt);
>-    xmlFreeDoc(xml);
>-    VIR_FREE(arch);
>     return data;
> }
>
>-- 
>1.8.3.1
>
>