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@gmail.com>
---
src/util/virnetlink.c | 43 ++++++++++++++++++++-----------------------
1 file changed, 20 insertions(+), 23 deletions(-)
diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index ecf62c9..fcdc09d 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -342,10 +342,8 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
unsigned int protocol, unsigned int groups,
void *opaque)
{
- int ret = -1;
bool end = false;
int len = 0;
- struct nlmsghdr *resp = NULL;
struct nlmsghdr *msg = NULL;
struct sockaddr_nl nladdr = {
@@ -357,9 +355,11 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
if (!(nlhandle = virNetlinkSendRequest(nl_msg, src_pid, nladdr,
protocol, groups)))
- goto cleanup;
+ return -1;
while (!end) {
+ VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
+
len = nl_recv(nlhandle, &nladdr, (unsigned char **)&resp, NULL);
VIR_WARNINGS_NO_CAST_ALIGN
for (msg = resp; NLMSG_OK(msg, len); msg = NLMSG_NEXT(msg, len)) {
@@ -368,19 +368,14 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
end = true;
if (virNetlinkGetErrorCode(msg, len) < 0)
- goto cleanup;
+ return -1;
if (callback(msg, opaque) < 0)
- goto cleanup;
+ return -1;
}
- VIR_FREE(resp);
}
- ret = 0;
-
- cleanup:
- VIR_FREE(resp);
- return ret;
+ return 0;
}
/**
@@ -408,7 +403,6 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
uint32_t src_pid, uint32_t dst_pid)
{
int rc = -1;
- struct nlmsghdr *resp = NULL;
struct nlmsgerr *err;
struct ifinfomsg ifinfo = {
.ifi_family = AF_UNSPEC,
@@ -416,6 +410,9 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
};
unsigned int recvbuflen;
struct nl_msg *nl_msg;
+ VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
+
+ *nlData = NULL;
if (ifname && ifindex <= 0 && virNetDevGetIndex(ifname, &ifindex) < 0)
return -1;
@@ -483,12 +480,12 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
default:
goto malformed_resp;
}
+
+ VIR_STEAL_PTR(*nlData, resp);
rc = 0;
+
cleanup:
nlmsg_free(nl_msg);
- if (rc < 0)
- VIR_FREE(resp);
- *nlData = resp;
return rc;
malformed_resp:
@@ -522,11 +519,11 @@ int
virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
{
int rc = -1;
- struct nlmsghdr *resp = NULL;
struct nlmsgerr *err;
struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
unsigned int recvbuflen;
struct nl_msg *nl_msg;
+ VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
nl_msg = nlmsg_alloc_simple(RTM_DELLINK,
NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
@@ -577,7 +574,6 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
rc = 0;
cleanup:
nlmsg_free(nl_msg);
- VIR_FREE(resp);
return rc;
malformed_resp:
@@ -610,13 +606,15 @@ int
virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
{
int rc = -1;
- struct nlmsghdr *resp = NULL;
struct nlmsgerr *err;
struct ndmsg ndinfo = {
.ndm_family = AF_UNSPEC,
};
unsigned int recvbuflen;
struct nl_msg *nl_msg;
+ VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
+
+ *nlData = NULL;
nl_msg = nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_DUMP | NLM_F_REQUEST);
if (!nl_msg) {
@@ -654,13 +652,12 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
default:
goto malformed_resp;
}
+
+ VIR_STEAL_PTR(*nlData, resp);
rc = recvbuflen;
cleanup:
nlmsg_free(nl_msg);
- if (rc < 0)
- VIR_FREE(resp);
- *nlData = resp;
return rc;
malformed_resp:
@@ -766,12 +763,12 @@ virNetlinkEventCallback(int watch,
void *opaque)
{
virNetlinkEventSrvPrivatePtr srv = opaque;
- struct nlmsghdr *msg;
struct sockaddr_nl peer;
struct ucred *creds = NULL;
size_t i;
int length;
bool handled = false;
+ VIR_AUTOFREE(struct nlmsghdr *) msg = NULL;
length = nl_recv(srv->netlinknh, &peer,
(unsigned char **)&msg, &creds);
@@ -801,7 +798,7 @@ virNetlinkEventCallback(int watch,
if (!handled)
VIR_DEBUG("event not handled.");
- VIR_FREE(msg);
+
virNetlinkEventServerUnlock(srv);
}
--
1.8.3.1
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
On Thu, Aug 09, 2018 at 09:42:11AM +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 <skrtbhtngr@gmail.com>
> ---
> src/util/virnetlink.c | 43 ++++++++++++++++++++-----------------------
> 1 file changed, 20 insertions(+), 23 deletions(-)
>
> diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
> index ecf62c9..fcdc09d 100644
> --- a/src/util/virnetlink.c
> +++ b/src/util/virnetlink.c
> @@ -342,10 +342,8 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
> unsigned int protocol, unsigned int groups,
> void *opaque)
> {
> - int ret = -1;
> bool end = false;
> int len = 0;
> - struct nlmsghdr *resp = NULL;
> struct nlmsghdr *msg = NULL;
>
> struct sockaddr_nl nladdr = {
> @@ -357,9 +355,11 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
>
> if (!(nlhandle = virNetlinkSendRequest(nl_msg, src_pid, nladdr,
> protocol, groups)))
> - goto cleanup;
> + return -1;
>
> while (!end) {
> + VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
> +
> len = nl_recv(nlhandle, &nladdr, (unsigned char **)&resp, NULL);
> VIR_WARNINGS_NO_CAST_ALIGN
> for (msg = resp; NLMSG_OK(msg, len); msg = NLMSG_NEXT(msg, len)) {
> @@ -368,19 +368,14 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg,
> end = true;
>
> if (virNetlinkGetErrorCode(msg, len) < 0)
> - goto cleanup;
> + return -1;
>
> if (callback(msg, opaque) < 0)
> - goto cleanup;
> + return -1;
> }
> - VIR_FREE(resp);
> }
>
> - ret = 0;
> -
> - cleanup:
> - VIR_FREE(resp);
> - return ret;
> + return 0;
> }
>
> /**
> @@ -408,7 +403,6 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
> uint32_t src_pid, uint32_t dst_pid)
> {
> int rc = -1;
> - struct nlmsghdr *resp = NULL;
> struct nlmsgerr *err;
> struct ifinfomsg ifinfo = {
> .ifi_family = AF_UNSPEC,
> @@ -416,6 +410,9 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
> };
> unsigned int recvbuflen;
> struct nl_msg *nl_msg;
> + VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
> +
> + *nlData = NULL;
unnecessary...
>
> if (ifname && ifindex <= 0 && virNetDevGetIndex(ifname, &ifindex) < 0)
> return -1;
> @@ -483,12 +480,12 @@ virNetlinkDumpLink(const char *ifname, int ifindex,
> default:
> goto malformed_resp;
> }
> +
> + VIR_STEAL_PTR(*nlData, resp);
> rc = 0;
> +
> cleanup:
> nlmsg_free(nl_msg);
> - if (rc < 0)
> - VIR_FREE(resp);
> - *nlData = resp;
> return rc;
>
> malformed_resp:
> @@ -522,11 +519,11 @@ int
> virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
> {
> int rc = -1;
> - struct nlmsghdr *resp = NULL;
> struct nlmsgerr *err;
> struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
> unsigned int recvbuflen;
> struct nl_msg *nl_msg;
> + VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
>
> nl_msg = nlmsg_alloc_simple(RTM_DELLINK,
> NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
> @@ -577,7 +574,6 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
> rc = 0;
> cleanup:
> nlmsg_free(nl_msg);
> - VIR_FREE(resp);
> return rc;
>
> malformed_resp:
> @@ -610,13 +606,15 @@ int
> virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
> {
> int rc = -1;
> - struct nlmsghdr *resp = NULL;
> struct nlmsgerr *err;
> struct ndmsg ndinfo = {
> .ndm_family = AF_UNSPEC,
> };
> unsigned int recvbuflen;
> struct nl_msg *nl_msg;
> + VIR_AUTOFREE(struct nlmsghdr *) resp = NULL;
> +
> + *nlData = NULL;
unnecessary...
The rest is fine, I'll adjust before merging.
Reviewed-by: Erik Skultety <eskultet@redhat.com>
>
> nl_msg = nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_DUMP | NLM_F_REQUEST);
> if (!nl_msg) {
> @@ -654,13 +652,12 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
> default:
> goto malformed_resp;
> }
> +
> + VIR_STEAL_PTR(*nlData, resp);
> rc = recvbuflen;
>
> cleanup:
> nlmsg_free(nl_msg);
> - if (rc < 0)
> - VIR_FREE(resp);
> - *nlData = resp;
> return rc;
>
> malformed_resp:
> @@ -766,12 +763,12 @@ virNetlinkEventCallback(int watch,
> void *opaque)
> {
> virNetlinkEventSrvPrivatePtr srv = opaque;
> - struct nlmsghdr *msg;
> struct sockaddr_nl peer;
> struct ucred *creds = NULL;
> size_t i;
> int length;
> bool handled = false;
> + VIR_AUTOFREE(struct nlmsghdr *) msg = NULL;
>
> length = nl_recv(srv->netlinknh, &peer,
> (unsigned char **)&msg, &creds);
> @@ -801,7 +798,7 @@ virNetlinkEventCallback(int watch,
>
> if (!handled)
> VIR_DEBUG("event not handled.");
> - VIR_FREE(msg);
> +
> virNetlinkEventServerUnlock(srv);
> }
>
> --
> 1.8.3.1
>
> --
> libvir-list mailing list
> libvir-list@redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
On Thu, Aug 09, 2018 at 09:42:11AM +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 <skrtbhtngr@gmail.com>
> ---
> src/util/virnetlink.c | 43 ++++++++++++++++++++-----------------------
> 1 file changed, 20 insertions(+), 23 deletions(-)
>
Actually, I squashed the following into this patch:
diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index ecf62c9e72..cb8072ff94 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -297,13 +297,13 @@ int virNetlinkCommand(struct nl_msg *nl_msg,
uint32_t src_pid, uint32_t dst_pid,
unsigned int protocol, unsigned int groups)
{
- int ret = -1;
struct sockaddr_nl nladdr = {
.nl_family = AF_NETLINK,
.nl_pid = dst_pid,
.nl_groups = 0,
};
struct pollfd fds[1];
+ VIR_AUTOFREE(struct nlmsghdr *) temp_resp = NULL;
VIR_AUTOPTR(virNetlinkHandle) nlhandle = NULL;
int len = 0;
@@ -311,28 +311,22 @@ int virNetlinkCommand(struct nl_msg *nl_msg,
if (!(nlhandle = virNetlinkSendRequest(nl_msg, src_pid, nladdr,
protocol, groups)))
- goto cleanup;
+ return -1;
- len = nl_recv(nlhandle, &nladdr, (unsigned char **)resp, NULL);
+ len = nl_recv(nlhandle, &nladdr, (unsigned char **)&temp_resp, NULL);
if (len == 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("nl_recv failed - returned 0 bytes"));
- goto cleanup;
+ return -1;
}
if (len < 0) {
virReportSystemError(errno, "%s", _("nl_recv failed"));
- goto cleanup;
+ return -1;
}
- ret = 0;
+ VIR_STEAL_PTR(*resp, temp_resp);
*respbuflen = len;
- cleanup:
- if (ret < 0) {
- *resp = NULL;
- *respbuflen = 0;
- }
-
- return ret;
+ return 0;
}
Erik
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
© 2016 - 2026 Red Hat, Inc.