By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.
Signed-off-by: Sukrit Bhatnagar <skrtbhtngr(a)gmail.com>
Reviewed-by: Erik Skultety <eskultet(a)redhat.com>
---
src/util/viriscsi.c | 44 +++++++++++++++++---------------------------
1 file changed, 17 insertions(+), 27 deletions(-)
diff --git a/src/util/viriscsi.c b/src/util/viriscsi.c
index f00aeb5..0c2d8bb 100644
--- a/src/util/viriscsi.c
+++ b/src/util/viriscsi.c
@@ -88,8 +88,8 @@ virISCSIGetSession(const char *devpath,
.session = NULL,
.devpath = devpath,
};
- char *error = NULL;
int exitstatus = 0;
+ VIR_AUTOFREE(char *) error = NULL;
virCommandPtr cmd = virCommandNewArgList(ISCSIADM, "--mode",
"session", NULL);
@@ -109,7 +109,6 @@ virISCSIGetSession(const char *devpath,
NULLSTR(error));
cleanup:
- VIR_FREE(error);
virCommandFree(cmd);
return cbdata.session;
}
@@ -125,12 +124,11 @@ virStorageBackendIQNFound(const char *initiatoriqn,
char **ifacename)
{
int ret = IQN_ERROR;
- char *outbuf = NULL;
char *line = NULL;
- char *iface = NULL;
- char *iqn = NULL;
virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
"--mode", "iface",
NULL);
+ VIR_AUTOFREE(char *) outbuf = NULL;
+ VIR_AUTOFREE(char *) iqn = NULL;
*ifacename = NULL;
@@ -150,13 +148,13 @@ virStorageBackendIQNFound(const char *initiatoriqn,
char *newline;
char *next;
size_t i;
+ VIR_AUTOFREE(char *) iface = NULL;
if (!(newline = strchr(line, '\n')))
break;
*newline = '\0';
- VIR_FREE(iface);
VIR_FREE(iqn);
/* Find the first space, copy everything up to that point into
@@ -197,9 +195,6 @@ virStorageBackendIQNFound(const char *initiatoriqn,
if (ret == IQN_MISSING)
VIR_DEBUG("Could not find interface with IQN '%s'", iqn);
- VIR_FREE(iqn);
- VIR_FREE(iface);
- VIR_FREE(outbuf);
virCommandFree(cmd);
return ret;
@@ -216,8 +211,9 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
char **ifacename)
{
int ret = -1, exitstatus = -1;
- char *temp_ifacename;
virCommandPtr cmd = NULL;
+ VIR_AUTOFREE(char *) iface_name = NULL;
+ VIR_AUTOFREE(char *) temp_ifacename = NULL;
if (virAsprintf(&temp_ifacename,
"libvirt-iface-%08llx",
@@ -263,23 +259,23 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
}
/* Check again to make sure the interface was created. */
- if (virStorageBackendIQNFound(initiatoriqn, ifacename) != IQN_FOUND) {
+ if (virStorageBackendIQNFound(initiatoriqn, &iface_name) != IQN_FOUND) {
VIR_DEBUG("Failed to find interface '%s' with IQN '%s'
"
"after attempting to create it",
&temp_ifacename[0], initiatoriqn);
goto cleanup;
} else {
VIR_DEBUG("Interface '%s' with IQN '%s' was created
successfully",
- *ifacename, initiatoriqn);
+ iface_name, initiatoriqn);
}
- ret = 0;
+ VIR_STEAL_PTR(*ifacename, iface_name);
+
+ virCommandFree(cmd);
+ return 0;
cleanup:
virCommandFree(cmd);
- VIR_FREE(temp_ifacename);
- if (ret != 0)
- VIR_FREE(*ifacename);
return ret;
}
@@ -299,7 +295,7 @@ virISCSIConnection(const char *portal,
NULL
};
virCommandPtr cmd;
- char *ifacename = NULL;
+ VIR_AUTOFREE(char *) ifacename = NULL;
cmd = virCommandNewArgs(baseargv);
virCommandAddArgSet(cmd, extraargv);
@@ -339,7 +335,6 @@ virISCSIConnection(const char *portal,
cleanup:
virCommandFree(cmd);
- VIR_FREE(ifacename);
return ret;
}
@@ -390,15 +385,13 @@ virISCSIGetTargets(char **const groups,
void *data)
{
struct virISCSITargetList *list = data;
- char *target;
+ VIR_AUTOFREE(char *) target = NULL;
if (VIR_STRDUP(target, groups[1]) < 0)
return -1;
- if (VIR_APPEND_ELEMENT(list->targets, list->ntargets, target) < 0) {
- VIR_FREE(target);
+ if (VIR_APPEND_ELEMENT(list->targets, list->ntargets, target) < 0)
return -1;
- }
return 0;
}
@@ -498,8 +491,7 @@ virISCSIScanTargets(const char *portal,
size_t *ntargets,
char ***targets)
{
- char *ifacename = NULL;
- int ret = -1;
+ VIR_AUTOFREE(char *) ifacename = NULL;
if (ntargets)
*ntargets = 0;
@@ -522,10 +514,8 @@ virISCSIScanTargets(const char *portal,
}
}
- ret = virISCSIScanTargetsInternal(portal, ifacename,
+ return virISCSIScanTargetsInternal(portal, ifacename,
persist, ntargets, targets);
- VIR_FREE(ifacename);
- return ret;
}
--
1.8.3.1