Re: [libvirt] [PATCH v3 05/11] util: netdevbridge: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-13 Thread Erik Skultety
On Thu, Aug 09, 2018 at 09:42:13AM +0530, Sukrit Bhatnagar wrote:
> 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 
> ---
Reviewed-by: Erik Skultety 

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v3 05/11] util: netdevbridge: use VIR_AUTOFREE instead of VIR_FREE for scalar types

2018-08-09 Thread Sukrit Bhatnagar
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 
---
 src/util/virnetdevbridge.c | 46 --
 1 file changed, 16 insertions(+), 30 deletions(-)

diff --git a/src/util/virnetdevbridge.c b/src/util/virnetdevbridge.c
index e46ac35..fe3697b 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(, 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*)
 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,16 +167,17 @@ static int virNetDevBridgeGet(const char *brname,
   const char *paramname,  /* sysfs param name */
   unsigned long *value)   /* current value */
 {
-char *path = NULL;
 int ret = -1;
 int fd = -1;
 struct ifreq ifr;
+VIR_AUTOFREE(char *) path = NULL;
 
 if (virAsprintf(, SYSFS_NET_DIR "%s/bridge/%s", brname, paramname) < 
0)
 return -1;
 
 if (virFileExists(path)) {
-char *valuestr;
+VIR_AUTOFREE(char *) valuestr = NULL;
+
 if (virFileReadAll(path, INT_BUFSIZE_BOUND(unsigned long),
) < 0)
 goto cleanup;
@@ -189,10 +186,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), 
0, 0 };
@@ -221,7 +216,6 @@ static int virNetDevBridgeGet(const char *brname,
 ret = 0;
  cleanup:
 VIR_FORCE_CLOSE(fd);
-VIR_FREE(path);
 return ret;
 }
 #endif /* __linux__ */
@@ -233,9 +227,9 @@ virNetDevBridgePortSet(const char *brname,
const char *paramname,
unsigned long value)
 {
-char *path = NULL;
 char valuestr[INT_BUFSIZE_BOUND(value)];
 int ret = -1;
+VIR_AUTOFREE(char *) path = NULL;
 
 snprintf(valuestr, sizeof(valuestr), "%lu", value);
 
@@ -254,7 +248,6 @@ virNetDevBridgePortSet(const char *brname,
  brname, ifname, paramname, valuestr);
 }
 
-VIR_FREE(path);
 return ret;
 }
 
@@ -265,29 +258,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(, SYSFS_NET_DIR "%s/brif/%s/%s",
 brname, ifname, paramname) < 0)
 return -1;
 
 if (virFileReadAll(path, INT_BUFSIZE_BOUND(unsigned long), ) < 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,12 +418,12 @@ virNetDevBridgeCreate(const char *brname)
 /* use a netlink RTM_NEWLINK message to create the bridge */