From nobody Mon Feb 9 15:08:17 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 153371421007340.61538177862565; Wed, 8 Aug 2018 00:43:30 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D26FF821CB; Wed, 8 Aug 2018 07:43:27 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7710B65325; Wed, 8 Aug 2018 07:43:27 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 2638118037F0; Wed, 8 Aug 2018 07:43:27 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w787hNP1005515 for ; Wed, 8 Aug 2018 03:43:23 -0400 Received: by smtp.corp.redhat.com (Postfix) id ADE205D9CA; Wed, 8 Aug 2018 07:43:23 +0000 (UTC) Received: from mx1.redhat.com (ext-mx07.extmail.prod.ext.phx2.redhat.com [10.5.110.31]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A3C3B5D9C9 for ; Wed, 8 Aug 2018 07:43:21 +0000 (UTC) Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 88061C049D64 for ; Wed, 8 Aug 2018 07:43:20 +0000 (UTC) Received: by mail-pg1-f175.google.com with SMTP id w10-v6so697898pgv.2 for ; Wed, 08 Aug 2018 00:43:20 -0700 (PDT) Received: from centos.localdomain ([42.106.0.165]) by smtp.gmail.com with ESMTPSA id 9-v6sm9617719pfv.53.2018.08.08.00.43.17 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 08 Aug 2018 00:43:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=b2yc2xPot4OY67CVnw41dxTNYUHd2Tb9ZYPAOwxnb3c=; b=S4dn52BUVB4M/YpZzl4k42xjmoiUZa14Dg5LiNydneDwB36coIn4rO89jrKx/HHugE D10tEMR7WZV1nCoY8pxgLbjYIdkAUuTHPMaF/wKSgI5A6hOw2nCXD5FXxNicYVrYFU6R RKSzkp82XwRmnfX7QtQKYr51htbu7fbfgFhpUBtjCIe7J6y2bQfnP/7/0yosvaIX5tQd PjuT8rXgNv+mom3YaSPh377ajf7ftqNrAdEKj9qA5pxSzE1JGPLITIBnXUb8HnAJc+/v hCAqqYnl3Ds1FzjbHUmHZ6UPiMSHK2zCyqrwlQlnfrFSUrVnyyuAto9DvvXjZA1bP4d7 wUfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=b2yc2xPot4OY67CVnw41dxTNYUHd2Tb9ZYPAOwxnb3c=; b=E/uMnk4oAbOOlTKNDyxlBdRLhWsPvEoM9cszNCIH4UL6HEPjTYrt5+vc4nZNfYRy6A BP3io/ckMnOfBfwDlghgwbwvyLMnvKoFnbYv8hP+6Q83QEuf7RYQ5CoLI9W695Kb7oBM spGiUQ8UqEQQcSFRwdKZxrYJ6WJzSq4A/ZwJmypApf7fL8HviVFljyJ3K+wA3WCTPQpB lHOv2eKPZPRZQ3zIomQq4CxZuTf+26mW/e7A9Ph1Zy6lKlb9ANheU+/gz3jv3jsbeNzt 7M5ScxooBGuGrOcNHAVRWWpiXPyDAqWfmhtuHCyLwRO+5Ndma/2yjzSLsHWT6XrvDkqN YO3Q== X-Gm-Message-State: AOUpUlHUIWbkIy+LoytZYECoAbnCQ8NBNxxQ+cuHkBBXi9e8s9qL/lV3 FsMudvMUj8mHk9CcCWWzmxPt2KLP X-Google-Smtp-Source: AA+uWPzMpFb3+I0kXMUWfx2eWh3vcOVaULJQMyBxNlmm/IkOpu4EVTKBxgAprulAhVUqJFggTuBvGA== X-Received: by 2002:a63:f713:: with SMTP id x19-v6mr1451641pgh.233.1533714199753; Wed, 08 Aug 2018 00:43:19 -0700 (PDT) From: Sukrit Bhatnagar To: libvir-list@redhat.com Date: Mon, 6 Aug 2018 02:13:32 +0530 Message-Id: <1533501842-24503-6-git-send-email-skrtbhtngr@gmail.com> In-Reply-To: <1533501842-24503-1-git-send-email-skrtbhtngr@gmail.com> References: <1533501842-24503-1-git-send-email-skrtbhtngr@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Wed, 08 Aug 2018 07:43:20 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Wed, 08 Aug 2018 07:43:20 +0000 (UTC) for IP:'209.85.215.175' DOMAIN:'mail-pg1-f175.google.com' HELO:'mail-pg1-f175.google.com' FROM:'skrtbhtngr@gmail.com' RCPT:'' X-RedHat-Spam-Score: -0.11 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_PASS) 209.85.215.175 mail-pg1-f175.google.com 209.85.215.175 mail-pg1-f175.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.31 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 05/35] util: netlink: use VIR_AUTOPTR for aggregate types X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 08 Aug 2018 07:43:28 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDMRC_1 RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" By making use of GNU C's cleanup attribute handled by the VIR_AUTOPTR macro for declaring aggregate pointer variables, majority of the calls to *Free functions can be dropped, which in turn leads to getting rid of most of our cleanup sections. Signed-off-by: Sukrit Bhatnagar --- src/util/virnetlink.c | 88 +++++++++++++++++++----------------------------= ---- 1 file changed, 33 insertions(+), 55 deletions(-) diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c index aa3a86d..66e80e2 100644 --- a/src/util/virnetlink.c +++ b/src/util/virnetlink.c @@ -297,15 +297,16 @@ int virNetlinkCommand(struct nl_msg *nl_msg, uint32_t src_pid, uint32_t dst_pid, unsigned int protocol, unsigned int groups) { - int ret =3D -1; struct sockaddr_nl nladdr =3D { .nl_family =3D AF_NETLINK, .nl_pid =3D dst_pid, .nl_groups =3D 0, }; struct pollfd fds[1]; - virNetlinkHandle *nlhandle =3D NULL; int len =3D 0; + VIR_AUTOPTR(virNetlinkHandle) nlhandle =3D NULL; + + *respbuflen =3D 0; =20 memset(fds, 0, sizeof(fds)); =20 @@ -324,16 +325,12 @@ int virNetlinkCommand(struct nl_msg *nl_msg, goto cleanup; } =20 - ret =3D 0; *respbuflen =3D len; - cleanup: - if (ret < 0) { - *resp =3D NULL; - *respbuflen =3D 0; - } + return 0; =20 - virNetlinkFree(nlhandle); - return ret; + cleanup: + *resp =3D NULL; + return -1; } =20 int @@ -343,7 +340,6 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg, unsigned int protocol, unsigned int groups, void *opaque) { - int ret =3D -1; bool end =3D false; int len =3D 0; struct nlmsghdr *msg =3D NULL; @@ -353,11 +349,11 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg, .nl_pid =3D dst_pid, .nl_groups =3D 0, }; - virNetlinkHandle *nlhandle =3D NULL; + VIR_AUTOPTR(virNetlinkHandle) nlhandle =3D NULL; =20 if (!(nlhandle =3D virNetlinkSendRequest(nl_msg, src_pid, nladdr, protocol, groups))) - goto cleanup; + return -1; =20 while (!end) { VIR_AUTOFREE(struct nlmsghdr *) resp =3D NULL; @@ -370,18 +366,14 @@ virNetlinkDumpCommand(struct nl_msg *nl_msg, end =3D true; =20 if (virNetlinkGetErrorCode(msg, len) < 0) - goto cleanup; + return -1; =20 if (callback(msg, opaque) < 0) - goto cleanup; + return -1; } } =20 - ret =3D 0; - - cleanup: - virNetlinkFree(nlhandle); - return ret; + return 0; } =20 /** @@ -415,7 +407,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex, .ifi_index =3D ifindex }; unsigned int recvbuflen; - struct nl_msg *nl_msg; + VIR_AUTOPTR(virNlMsg) nl_msg =3D NULL; VIR_AUTOFREE(struct nlmsghdr *) resp =3D NULL; =20 *nlData =3D NULL; @@ -456,7 +448,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex, =20 if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, src_pid, dst_pid, NETLINK_ROUTE, 0) < 0) - goto cleanup; + return -1; =20 if (recvbuflen < NLMSG_LENGTH(0) || resp =3D=3D NULL) goto malformed_resp; @@ -471,7 +463,7 @@ virNetlinkDumpLink(const char *ifname, int ifindex, virReportSystemError(-err->error, _("error dumping %s (%d) interface"), ifname, ifindex); - goto cleanup; + return -1; } break; =20 @@ -488,21 +480,17 @@ virNetlinkDumpLink(const char *ifname, int ifindex, } =20 VIR_STEAL_PTR(*nlData, resp); - rc =3D 0; - - cleanup: - nlmsg_free(nl_msg); - return rc; + return 0; =20 malformed_resp: virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("malformed netlink response message")); - goto cleanup; + return rc; =20 buffer_too_small: virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("allocated netlink buffer is too small")); - goto cleanup; + return rc; } =20 =20 @@ -524,11 +512,10 @@ virNetlinkDumpLink(const char *ifname, int ifindex, int virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback) { - int rc =3D -1; struct nlmsgerr *err; struct ifinfomsg ifinfo =3D { .ifi_family =3D AF_UNSPEC }; unsigned int recvbuflen; - struct nl_msg *nl_msg; + VIR_AUTOPTR(virNlMsg) nl_msg =3D NULL; VIR_AUTOFREE(struct nlmsghdr *) resp =3D NULL; =20 nl_msg =3D nlmsg_alloc_simple(RTM_DELLINK, @@ -546,7 +533,7 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLink= Fallback fallback) =20 if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, 0, 0, NETLINK_ROUTE, 0) < 0) { - goto cleanup; + return -1; } =20 if (recvbuflen < NLMSG_LENGTH(0) || resp =3D=3D NULL) @@ -558,15 +545,14 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLi= nkFallback fallback) if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err))) goto malformed_resp; =20 - if (-err->error =3D=3D EOPNOTSUPP && fallback) { - rc =3D fallback(ifname); - goto cleanup; - } + if (-err->error =3D=3D EOPNOTSUPP && fallback) + return fallback(ifname); + if (err->error) { virReportSystemError(-err->error, _("error destroying network device %s"), ifname); - goto cleanup; + return -1; } break; =20 @@ -577,20 +563,17 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLi= nkFallback fallback) goto malformed_resp; } =20 - rc =3D 0; - cleanup: - nlmsg_free(nl_msg); - return rc; + return 0; =20 malformed_resp: virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("malformed netlink response message")); - goto cleanup; + return -1; =20 buffer_too_small: virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("allocated netlink buffer is too small")); - goto cleanup; + return -1; } =20 /** @@ -611,13 +594,12 @@ virNetlinkDelLink(const char *ifname, virNetlinkDelLi= nkFallback fallback) int virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid) { - int rc =3D -1; struct nlmsgerr *err; struct ndmsg ndinfo =3D { .ndm_family =3D AF_UNSPEC, }; unsigned int recvbuflen; - struct nl_msg *nl_msg; + VIR_AUTOPTR(virNlMsg) nl_msg =3D NULL; VIR_AUTOFREE(struct nlmsghdr *) resp =3D NULL; =20 *nlData =3D NULL; @@ -634,7 +616,7 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, = uint32_t dst_pid) =20 if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, src_pid, dst_pid, NETLINK_ROUTE, 0) < 0) - goto cleanup; + return -1; =20 if (recvbuflen < NLMSG_LENGTH(0) || resp =3D=3D NULL) goto malformed_resp; @@ -648,7 +630,7 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, = uint32_t dst_pid) if (err->error) { virReportSystemError(-err->error, "%s", _("error dumping")); - goto cleanup; + return -1; } break; =20 @@ -660,21 +642,17 @@ virNetlinkGetNeighbor(void **nlData, uint32_t src_pid= , uint32_t dst_pid) } =20 VIR_STEAL_PTR(*nlData, resp); - rc =3D recvbuflen; - - cleanup: - nlmsg_free(nl_msg); - return rc; + return recvbuflen; =20 malformed_resp: virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("malformed netlink response message")); - goto cleanup; + return -1; =20 buffer_too_small: virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("allocated netlink buffer is too small")); - goto cleanup; + return -1; } =20 int --=20 1.8.3.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list