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>
---
src/util/virnetdevbridge.c | 45 +++++++++++++++------------------------------
1 file changed, 15 insertions(+), 30 deletions(-)
diff --git a/src/util/virnetdevbridge.c b/src/util/virnetdevbridge.c
index e46ac35..bf30d7c 100644
--- a/src/util/virnetdevbridge.c
+++ b/src/util/virnetdevbridge.c
@@ -126,8 +126,7 @@ static int virNetDevBridgeSet(const char *brname,
int fd, /* control socket */
struct ifreq *ifr) /* pre-filled bridge name */
{
- char *path = NULL;
- int ret = -1;
+ VIR_AUTOFREE(char *) path = NULL;
if (virAsprintf(&path, SYSFS_NET_DIR "%s/bridge/%s", brname, paramname)
< 0)
return -1;
@@ -138,7 +137,7 @@ static int virNetDevBridgeSet(const char *brname,
if (virFileWriteStr(path, valuestr, 0) < 0) {
virReportSystemError(errno,
_("Unable to set bridge %s %s"), brname,
paramname);
- goto cleanup;
+ return -1;
}
} else {
unsigned long paramid;
@@ -149,21 +148,18 @@ static int virNetDevBridgeSet(const char *brname,
} else {
virReportSystemError(EINVAL,
_("Unable to set bridge %s %s"), brname,
paramname);
- goto cleanup;
+ return -1;
}
unsigned long args[] = { paramid, value, 0, 0 };
ifr->ifr_data = (char*)&args;
if (ioctl(fd, SIOCDEVPRIVATE, ifr) < 0) {
virReportSystemError(errno,
_("Unable to set bridge %s %s"), brname,
paramname);
- goto cleanup;
+ return -1;
}
}
- ret = 0;
- cleanup:
- VIR_FREE(path);
- return ret;
+ return 0;
}
@@ -171,7 +167,7 @@ static int virNetDevBridgeGet(const char *brname,
const char *paramname, /* sysfs param name */
unsigned long *value) /* current value */
{
- char *path = NULL;
+ VIR_AUTOFREE(char *) path = NULL;
int ret = -1;
int fd = -1;
struct ifreq ifr;
@@ -180,7 +176,7 @@ static int virNetDevBridgeGet(const char *brname,
return -1;
if (virFileExists(path)) {
- char *valuestr;
+ VIR_AUTOFREE(char *) valuestr = NULL;
if (virFileReadAll(path, INT_BUFSIZE_BOUND(unsigned long),
&valuestr) < 0)
goto cleanup;
@@ -189,10 +185,8 @@ static int virNetDevBridgeGet(const char *brname,
virReportSystemError(EINVAL,
_("Unable to get bridge %s %s"),
brname, paramname);
- VIR_FREE(valuestr);
goto cleanup;
}
- VIR_FREE(valuestr);
} else {
struct __bridge_info info;
unsigned long args[] = { BRCTL_GET_BRIDGE_INFO, (unsigned long)&info, 0, 0
};
@@ -221,7 +215,6 @@ static int virNetDevBridgeGet(const char *brname,
ret = 0;
cleanup:
VIR_FORCE_CLOSE(fd);
- VIR_FREE(path);
return ret;
}
#endif /* __linux__ */
@@ -233,7 +226,7 @@ virNetDevBridgePortSet(const char *brname,
const char *paramname,
unsigned long value)
{
- char *path = NULL;
+ VIR_AUTOFREE(char *) path = NULL;
char valuestr[INT_BUFSIZE_BOUND(value)];
int ret = -1;
@@ -254,7 +247,6 @@ virNetDevBridgePortSet(const char *brname,
brname, ifname, paramname, valuestr);
}
- VIR_FREE(path);
return ret;
}
@@ -265,29 +257,24 @@ virNetDevBridgePortGet(const char *brname,
const char *paramname,
unsigned long *value)
{
- char *path = NULL;
- char *valuestr = NULL;
- int ret = -1;
+ VIR_AUTOFREE(char *) path = NULL;
+ VIR_AUTOFREE(char *) valuestr = NULL;
if (virAsprintf(&path, SYSFS_NET_DIR "%s/brif/%s/%s",
brname, ifname, paramname) < 0)
return -1;
if (virFileReadAll(path, INT_BUFSIZE_BOUND(unsigned long), &valuestr) < 0)
- goto cleanup;
+ return -1;
if (virStrToLong_ul(valuestr, NULL, 10, value) < 0) {
virReportSystemError(EINVAL,
_("Unable to get bridge %s port %s %s"),
brname, ifname, paramname);
- goto cleanup;
+ return -1;
}
- ret = 0;
- cleanup:
- VIR_FREE(path);
- VIR_FREE(valuestr);
- return ret;
+ return 0;
}
@@ -430,7 +417,7 @@ virNetDevBridgeCreate(const char *brname)
/* use a netlink RTM_NEWLINK message to create the bridge */
const char *type = "bridge";
int rc = -1;
- struct nlmsghdr *resp = NULL;
+ VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
struct nlmsgerr *err;
struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
unsigned int recvbuflen;
@@ -495,7 +482,6 @@ virNetDevBridgeCreate(const char *brname)
rc = 0;
cleanup:
nlmsg_free(nl_msg);
- VIR_FREE(resp);
return rc;
malformed_resp:
@@ -1069,7 +1055,7 @@ virNetDevBridgeFDBAddDel(const virMacAddr *mac, const char *ifname,
unsigned int flags, bool isAdd)
{
int ret = -1;
- struct nlmsghdr *resp = NULL;
+ VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
struct nlmsgerr *err;
unsigned int recvbuflen;
struct nl_msg *nl_msg;
@@ -1142,7 +1128,6 @@ virNetDevBridgeFDBAddDel(const virMacAddr *mac, const char *ifname,
ret = 0;
cleanup:
nlmsg_free(nl_msg);
- VIR_FREE(resp);
return ret;
malformed_resp:
--
1.8.3.1