[PATCH 2/2] virStorageBackendRBDGetVolNames: Refactor cleanup in 'rbd_list' version

Peter Krempa posted 2 patches 4 years, 7 months ago
[PATCH 2/2] virStorageBackendRBDGetVolNames: Refactor cleanup in 'rbd_list' version
Posted by Peter Krempa 4 years, 7 months ago
Use automatic memory freeing for the string list so that we can remove
the cleanup section.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
---
 src/storage/storage_backend_rbd.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/src/storage/storage_backend_rbd.c b/src/storage/storage_backend_rbd.c
index 371ebfaf1b..a4e8115dc4 100644
--- a/src/storage/storage_backend_rbd.c
+++ b/src/storage/storage_backend_rbd.c
@@ -611,7 +611,7 @@ virStorageBackendRBDGetVolNames(virStorageBackendRBDState *ptr)
 static char **
 virStorageBackendRBDGetVolNames(virStorageBackendRBDState *ptr)
 {
-    char **names = NULL;
+    g_auto(GStrv) names = NULL;
     size_t nnames = 0;
     int rc;
     size_t max_size = 1024;
@@ -626,7 +626,7 @@ virStorageBackendRBDGetVolNames(virStorageBackendRBDState *ptr)
             break;
         if (rc != -ERANGE) {
             virReportSystemError(errno, "%s", _("Unable to list RBD images"));
-            goto error;
+            return NULL;
         }
         VIR_FREE(namebuf);
     }
@@ -640,18 +640,14 @@ virStorageBackendRBDGetVolNames(virStorageBackendRBDState *ptr)
         namedup = g_strdup(name);

         if (VIR_APPEND_ELEMENT(names, nnames, namedup) < 0)
-            goto error;
+            return NULL;

         name += strlen(name) + 1;
     }

     VIR_EXPAND_N(names, nnames, 1);

-    return names;
-
- error:
-    virStringListFreeCount(names, nnames);
-    return NULL;
+    return g_steal_pointer(&names);
 }
 #endif /* ! WITH_RBD_LIST2 */

-- 
2.31.1

Re: [PATCH 2/2] virStorageBackendRBDGetVolNames: Refactor cleanup in 'rbd_list' version
Posted by Jano Tomko 4 years, 7 months ago
On 6/15/21 4:16 PM, Peter Krempa wrote:
> Use automatic memory freeing for the string list so that we can remove
> the cleanup section.
> 
> Signed-off-by: Peter Krempa <pkrempa@redhat.com>
> ---
>  src/storage/storage_backend_rbd.c | 12 ++++--------
>  1 file changed, 4 insertions(+), 8 deletions(-)
> 

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

Jano