[PATCH 4/7] virStorageBackendISCSIDirectSetAuth: Refactor cleanup

Peter Krempa pkrempa at redhat.com
Tue Jan 31 16:02:16 UTC 2023


Use automatic pointer for 'conn' and remove the 'cleanup' label and
'ret' variable.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/storage/storage_backend_iscsi_direct.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/src/storage/storage_backend_iscsi_direct.c b/src/storage/storage_backend_iscsi_direct.c
index 5df5447066..63225c533e 100644
--- a/src/storage/storage_backend_iscsi_direct.c
+++ b/src/storage/storage_backend_iscsi_direct.c
@@ -91,8 +91,7 @@ virStorageBackendISCSIDirectSetAuth(struct iscsi_context *iscsi,
     size_t secret_size;
     g_autofree char *secret_str = NULL;
     virStorageAuthDef *authdef = source->auth;
-    int ret = -1;
-    virConnectPtr conn = NULL;
+    g_autoptr(virConnect) conn = NULL;
     VIR_IDENTITY_AUTORESTORE virIdentity *oldident = NULL;

     if (!authdef || authdef->authType == VIR_STORAGE_AUTH_TYPE_NONE)
@@ -104,19 +103,19 @@ virStorageBackendISCSIDirectSetAuth(struct iscsi_context *iscsi,
     if (authdef->authType != VIR_STORAGE_AUTH_TYPE_CHAP) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("iscsi-direct pool only supports 'chap' auth type"));
-        return ret;
+        return -1;
     }

     if (!(oldident = virIdentityElevateCurrent()))
         return -1;

     if (!(conn = virGetConnectSecret()))
-        return ret;
+        return -1;

     if (virSecretGetSecretString(conn, &authdef->seclookupdef,
                                  VIR_SECRET_USAGE_TYPE_ISCSI,
                                  &secret_value, &secret_size) < 0)
-        goto cleanup;
+        return -1;

     secret_str = g_new0(char, secret_size + 1);
     memcpy(secret_str, secret_value, secret_size);
@@ -125,17 +124,15 @@ virStorageBackendISCSIDirectSetAuth(struct iscsi_context *iscsi,

     if (iscsi_set_initiator_username_pwd(iscsi,
                                          authdef->username, secret_str) < 0) {
+        virSecureErase(secret_str, secret_size);
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Failed to set credential: %s"),
                        iscsi_get_error(iscsi));
-        goto cleanup;
+        return -1;
     }
-
-    ret = 0;
- cleanup:
     virSecureErase(secret_str, secret_size);
-    virObjectUnref(conn);
-    return ret;
+
+    return 0;
 }

 static int
-- 
2.39.1



More information about the libvir-list mailing list