Re: [libvirt PATCH v2 14/15] nwfilter: convert local pointers to use g_auto*

2020-07-15 Thread Ján Tomko

On a Tuesday in 2020, Laine Stump wrote:

Signed-off-by: Laine Stump 
---
src/nwfilter/nwfilter_dhcpsnoop.c | 91 +++
src/nwfilter/nwfilter_ebiptables_driver.c | 75 +++
src/nwfilter/nwfilter_gentech_driver.c| 15 ++--
src/nwfilter/nwfilter_learnipaddr.c   |  7 +-
4 files changed, 61 insertions(+), 127 deletions(-)



Reviewed-by: Ján Tomko 

Jano


signature.asc
Description: PGP signature


[libvirt PATCH v2 14/15] nwfilter: convert local pointers to use g_auto*

2020-07-07 Thread Laine Stump
Signed-off-by: Laine Stump 
---
 src/nwfilter/nwfilter_dhcpsnoop.c | 91 +++
 src/nwfilter/nwfilter_ebiptables_driver.c | 75 +++
 src/nwfilter/nwfilter_gentech_driver.c| 15 ++--
 src/nwfilter/nwfilter_learnipaddr.c   |  7 +-
 4 files changed, 61 insertions(+), 127 deletions(-)

diff --git a/src/nwfilter/nwfilter_dhcpsnoop.c 
b/src/nwfilter/nwfilter_dhcpsnoop.c
index 4bc1607694..64671af135 100644
--- a/src/nwfilter/nwfilter_dhcpsnoop.c
+++ b/src/nwfilter/nwfilter_dhcpsnoop.c
@@ -292,18 +292,17 @@ static const unsigned char dhcp_magic[4] = { 99, 130, 83, 
99 };
 static char *
 virNWFilterSnoopActivate(virNWFilterSnoopReqPtr req)
 {
-char *key;
-
-key = g_strdup_printf("%p-%d", req, req->ifindex);
+g_autofree char *key = g_strdup_printf("%p-%d", req, req->ifindex);
+char *ret = NULL;
 
 virNWFilterSnoopActiveLock();
 
-if (virHashAddEntry(virNWFilterSnoopState.active, key, (void *)0x1) < 0)
-VIR_FREE(key);
+if (virHashAddEntry(virNWFilterSnoopState.active, key, (void *)0x1) == 0)
+ret = g_steal_pointer();
 
 virNWFilterSnoopActiveUnlock();
 
-return key;
+return ret;
 }
 
 static void
@@ -442,11 +441,10 @@ static int
 virNWFilterSnoopIPLeaseInstallRule(virNWFilterSnoopIPLeasePtr ipl,
bool instantiate)
 {
-char *ipaddr;
+g_autofree char *ipaddr = virSocketAddrFormat(>ipAddress);
 int rc = -1;
 virNWFilterSnoopReqPtr req;
 
-ipaddr = virSocketAddrFormat(>ipAddress);
 if (!ipaddr)
 return -1;
 
@@ -473,9 +471,6 @@ 
virNWFilterSnoopIPLeaseInstallRule(virNWFilterSnoopIPLeasePtr ipl,
 
  cleanup:
 virNWFilterSnoopReqUnlock(req);
-
-VIR_FREE(ipaddr);
-
 return rc;
 }
 
@@ -551,7 +546,7 @@ virNWFilterSnoopReqGet(virNWFilterSnoopReqPtr req)
 static virNWFilterSnoopReqPtr
 virNWFilterSnoopReqNew(const char *ifkey)
 {
-virNWFilterSnoopReqPtr req;
+g_autofree virNWFilterSnoopReqPtr req = g_new0(virNWFilterSnoopReq, 1);
 
 if (ifkey == NULL || strlen(ifkey) != VIR_IFKEY_LEN - 1) {
 virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -562,28 +557,20 @@ virNWFilterSnoopReqNew(const char *ifkey)
 return NULL;
 }
 
-req = g_new0(virNWFilterSnoopReq, 1);
-
 req->threadStatus = THREAD_STATUS_NONE;
 
-if (virStrcpyStatic(req->ifkey, ifkey) < 0||
-virMutexInitRecursive(>lock) < 0)
-goto err_free_req;
+if (virStrcpyStatic(req->ifkey, ifkey) < 0 ||
+virMutexInitRecursive(>lock) < 0) {
+return NULL;
+}
 
-if (virCondInit(>threadStatusCond) < 0)
-goto err_destroy_mutex;
+if (virCondInit(>threadStatusCond) < 0) {
+virMutexDestroy(>lock);
+return NULL;
+}
 
 virNWFilterSnoopReqGet(req);
-
-return req;
-
- err_destroy_mutex:
-virMutexDestroy(>lock);
-
- err_free_req:
-VIR_FREE(req);
-
-return NULL;
+return g_steal_pointer();
 }
 
 /*
@@ -815,7 +802,7 @@ virNWFilterSnoopReqLeaseDel(virNWFilterSnoopReqPtr req,
 {
 int ret = 0;
 virNWFilterSnoopIPLeasePtr ipl;
-char *ipstr = NULL;
+g_autofree char *ipstr = NULL;
 
 /* protect req->start, req->ifname and the lease */
 virNWFilterSnoopReqLock(req);
@@ -868,8 +855,6 @@ virNWFilterSnoopReqLeaseDel(virNWFilterSnoopReqPtr req,
 ignore_value(!!g_atomic_int_dec_and_test());
 
  lease_not_found:
-VIR_FREE(ipstr);
-
 virNWFilterSnoopReqUnlock(req);
 
 return ret;
@@ -1045,7 +1030,7 @@ virNWFilterSnoopDHCPOpen(const char *ifname, virMacAddr 
*mac,
 pcap_t *handle = NULL;
 struct bpf_program fp;
 char pcap_errbuf[PCAP_ERRBUF_SIZE];
-char *ext_filter = NULL;
+g_autofree char *ext_filter = NULL;
 char macaddr[VIR_MAC_STRING_BUFLEN];
 
 virMacAddrFormat(mac, macaddr);
@@ -1075,7 +1060,7 @@ virNWFilterSnoopDHCPOpen(const char *ifname, virMacAddr 
*mac,
 if (handle == NULL) {
 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("pcap_create failed"));
-goto cleanup_nohandle;
+return NULL;
 }
 
 if (pcap_set_snaplen(handle, PCAP_PBUFSIZE) < 0 ||
@@ -1107,17 +1092,12 @@ virNWFilterSnoopDHCPOpen(const char *ifname, virMacAddr 
*mac,
 }
 
 pcap_freecode();
-VIR_FREE(ext_filter);
-
 return handle;
 
  cleanup_freecode:
 pcap_freecode();
  cleanup:
 pcap_close(handle);
- cleanup_nohandle:
-VIR_FREE(ext_filter);
-
 return NULL;
 }
 
@@ -1128,7 +1108,7 @@ virNWFilterSnoopDHCPOpen(const char *ifname, virMacAddr 
*mac,
 static void virNWFilterDHCPDecodeWorker(void *jobdata, void *opaque)
 {
 virNWFilterSnoopReqPtr req = opaque;
-virNWFilterDHCPDecodeJobPtr job = jobdata;
+g_autofree virNWFilterDHCPDecodeJobPtr job = jobdata;
 virNWFilterSnoopEthHdrPtr packet = (virNWFilterSnoopEthHdrPtr)job->packet;
 
 if (virNWFilterSnoopDHCPDecode(req, packet,
@@ -1140,7 +1120,6 @@ static void virNWFilterDHCPDecodeWorker(void *jobdata, 
void