[libvirt PATCH 05/21] util: Use glib memory functions in virLastErrorObject

Tim Wiederhake posted 21 patches 5 years, 5 months ago
There is a newer version of this series
[libvirt PATCH 05/21] util: Use glib memory functions in virLastErrorObject
Posted by Tim Wiederhake 5 years, 5 months ago
Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
---
 src/util/virerror.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/src/util/virerror.c b/src/util/virerror.c
index 4ba99defbb..99a0855b51 100644
--- a/src/util/virerror.c
+++ b/src/util/virerror.c
@@ -237,15 +237,17 @@ virErrorCopyNew(virErrorPtr err)
 static virErrorPtr
 virLastErrorObject(void)
 {
-    virErrorPtr err;
+    g_autoptr(virError) err = NULL;
+
     err = virThreadLocalGet(&virLastErr);
-    if (!err) {
-        if (VIR_ALLOC_QUIET(err) < 0)
-            return NULL;
-        if (virThreadLocalSet(&virLastErr, err) < 0)
-            VIR_FREE(err);
-    }
-    return err;
+    if (err)
+        return g_steal_pointer(&err);
+
+    err = g_new0(virError, 1);
+    if (virThreadLocalSet(&virLastErr, err) < 0)
+        return NULL;
+
+    return g_steal_pointer(&err);
 }
 
 
-- 
2.26.2

Re: [libvirt PATCH 05/21] util: Use glib memory functions in virLastErrorObject
Posted by Ján Tomko 5 years, 5 months ago
On a Friday in 2020, Tim Wiederhake wrote:
>Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
>---
> src/util/virerror.c | 18 ++++++++++--------
> 1 file changed, 10 insertions(+), 8 deletions(-)
>
>diff --git a/src/util/virerror.c b/src/util/virerror.c
>index 4ba99defbb..99a0855b51 100644
>--- a/src/util/virerror.c
>+++ b/src/util/virerror.c
>@@ -237,15 +237,17 @@ virErrorCopyNew(virErrorPtr err)
> static virErrorPtr
> virLastErrorObject(void)
> {
>-    virErrorPtr err;
>+    g_autoptr(virError) err = NULL;
>+

This function would also look nicer without g_auto - most of the code
paths don't want to free err.

Jano

>     err = virThreadLocalGet(&virLastErr);
>-    if (!err) {
>-        if (VIR_ALLOC_QUIET(err) < 0)
>-            return NULL;
>-        if (virThreadLocalSet(&virLastErr, err) < 0)
>-            VIR_FREE(err);
>-    }
>-    return err;
>+    if (err)
>+        return g_steal_pointer(&err);
>+
>+    err = g_new0(virError, 1);
>+    if (virThreadLocalSet(&virLastErr, err) < 0)
>+        return NULL;
>+
>+    return g_steal_pointer(&err);
> }
>
>
>-- 
>2.26.2
>