From nobody Tue May 13 11:17:07 2025 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 153263920404486.08270526880256; Thu, 26 Jul 2018 14:06:44 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 918463003726; Thu, 26 Jul 2018 21:06:41 +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 20FB2CE6CB; Thu, 26 Jul 2018 21:06:36 +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 CA4AB1800FED; Thu, 26 Jul 2018 21:06:35 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w6QL6Y4n025610 for ; Thu, 26 Jul 2018 17:06:34 -0400 Received: by smtp.corp.redhat.com (Postfix) id 027D260FA6; Thu, 26 Jul 2018 21:06:34 +0000 (UTC) Received: from mx1.redhat.com (ext-mx06.extmail.prod.ext.phx2.redhat.com [10.5.110.30]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E4D6E610C9 for ; Thu, 26 Jul 2018 21:06:01 +0000 (UTC) Received: from mail-pg1-f195.google.com (mail-pg1-f195.google.com [209.85.215.195]) (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 A51D336809 for ; Thu, 26 Jul 2018 21:05:59 +0000 (UTC) Received: by mail-pg1-f195.google.com with SMTP id a11-v6so1885492pgw.6 for ; Thu, 26 Jul 2018 14:05:59 -0700 (PDT) Received: from centos.localdomain ([42.106.3.93]) by smtp.gmail.com with ESMTPSA id z10-v6sm3279182pfh.83.2018.07.26.14.05.57 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 26 Jul 2018 14:05:58 -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=MAFjVvZCdpYpdSEAT8FA/IlFpseyzJqRATUv/cTKIXg=; b=Oh2YdtzeWVwKHMB0wE3zcUzJCyywShS5x+ts77zJFNJCb36yAydDT4DkqhxP3+NmjU 2v61Jm5uFeqre/r5HO/EeTbX2NcX/twOmgW8E4hjVZgK2eDJYBAJ8A4tRkkm/WCoO2Us TY7KXlCTCwRxqyrsF/rxCNLc8Gvm4DdJV41ED84C/rWYrSm79poGgc9WnpaTJ6zBMxJp u12k/AQwEbz48mHTXln3LW7vDlVsZjRGejHkKgVvBui8hUfqhhW8fVZE87GcFkqGnuLy IcpdAVVMFFf9p8VHOgAFbqPiZpFwHngAn4Mrz8BN1x9Q9MKE1dY8XsR1AhX4AMyazEr3 2a9g== 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=MAFjVvZCdpYpdSEAT8FA/IlFpseyzJqRATUv/cTKIXg=; b=ddnPccqk9x+s6EiJ91PFHif+7sOEMlN1ZJ3JWWsrNvckofBUxB2RmIkL4XMDR9mFfj RIZ0Gl3c/e0eiGr+EOUWLdbdmqt+N/aHp5aRbT5Qgra5rzmpVAoqt2a0R7WDdVunJMqb 0+rOayE2tLFkvpXhjOfaS/44j/eZLex2FWgyWDE6MRPcCFrOtWcnG95YQnSBHvJZKtS9 EllsglxNHqkEYK4HXQcyjMNc8mmlb8Ce5kUVYgbOWZS6ONnBzLbz3rDRoZ9yJuGMTOvz 6WkxBRT8EAhwWUR36tgtaxT20slfz4bKE3VEr/lTzFcWaa74R8Dn+n44Q48jot5qvR1R UFHg== X-Gm-Message-State: AOUpUlGXgzG2hT33Q28P5+HRpcUd9Yp5MOBdPCp6jkYaztt0ySWhNNcB WV2q2FdTv5oVzzufTDNRzj5eKp9O X-Google-Smtp-Source: AAOMgpcAdzM1hbDKVTHTbqLMUBZWjcBicmYICDKqlYWUjUvS4903XPAY6YwhsIBcRBiloJipeGeubA== X-Received: by 2002:a62:45d2:: with SMTP id n79-v6mr3645936pfi.137.1532639158840; Thu, 26 Jul 2018 14:05:58 -0700 (PDT) From: Sukrit Bhatnagar To: libvir-list@redhat.com Date: Tue, 24 Jul 2018 21:22:42 +0530 Message-Id: <1532447562-24010-42-git-send-email-skrtbhtngr@gmail.com> In-Reply-To: <1532447562-24010-1-git-send-email-skrtbhtngr@gmail.com> References: <1532447562-24010-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.30]); Thu, 26 Jul 2018 21:05:59 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 26 Jul 2018 21:05:59 +0000 (UTC) for IP:'209.85.215.195' DOMAIN:'mail-pg1-f195.google.com' HELO:'mail-pg1-f195.google.com' FROM:'skrtbhtngr@gmail.com' RCPT:'' X-RedHat-Spam-Score: 1.389 * (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, RCVD_IN_SORBS_WEB, SPF_PASS) 209.85.215.195 mail-pg1-f195.google.com 209.85.215.195 mail-pg1-f195.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.30 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 41/41] util: lease: 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.84 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Thu, 26 Jul 2018 21:06:42 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: 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 Reviewed-by: Erik Skultety --- src/util/virlease.c | 76 ++++++++++++++++++++-----------------------------= ---- 1 file changed, 29 insertions(+), 47 deletions(-) diff --git a/src/util/virlease.c b/src/util/virlease.c index baaceaf..7c6c37e 100644 --- a/src/util/virlease.c +++ b/src/util/virlease.c @@ -56,40 +56,36 @@ virLeaseReadCustomLeaseFile(virJSONValuePtr leases_arra= y_new, char **server_duid) { VIR_AUTOFREE(char *) lease_entries =3D NULL; - virJSONValuePtr leases_array =3D NULL; + VIR_AUTOPTR(virJSONValue) leases_array =3D NULL; long long expirytime; int custom_lease_file_len =3D 0; virJSONValuePtr lease_tmp =3D NULL; const char *ip_tmp =3D NULL; const char *server_duid_tmp =3D NULL; size_t i; - int ret =3D -1; =20 /* Read entire contents */ if ((custom_lease_file_len =3D virFileReadAll(custom_lease_file, VIR_NETWORK_DHCP_LEASE_FIL= E_SIZE_MAX, &lease_entries)) < 0) { - goto cleanup; + return -1; } =20 /* Check for previous leases */ - if (custom_lease_file_len =3D=3D 0) { - ret =3D 0; - goto cleanup; - } + if (custom_lease_file_len =3D=3D 0) + return 0; =20 if (!(leases_array =3D virJSONValueFromString(lease_entries))) { virReportError(VIR_ERR_INTERNAL_ERROR, _("invalid json in file: %s, rewriting it"), custom_lease_file); - ret =3D 0; - goto cleanup; + return 0; } =20 if (!virJSONValueIsArray(leases_array)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("couldn't fetch array of leases")); - goto cleanup; + return -1; } =20 i =3D 0; @@ -97,14 +93,14 @@ virLeaseReadCustomLeaseFile(virJSONValuePtr leases_arra= y_new, if (!(lease_tmp =3D virJSONValueArrayGet(leases_array, i))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to parse json")); - goto cleanup; + return -1; } =20 if (!(ip_tmp =3D virJSONValueObjectGetString(lease_tmp, "ip-addres= s")) || (virJSONValueObjectGetNumberLong(lease_tmp, "expiry-time", &ex= pirytime) < 0)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to parse json")); - goto cleanup; + return -1; } =20 /* Check whether lease has to be included or not */ @@ -121,14 +117,14 @@ virLeaseReadCustomLeaseFile(virJSONValuePtr leases_ar= ray_new, /* Control reaches here when the 'action' is not for an * ipv6 lease or, for some weird reason the env var * DNSMASQ_SERVER_DUID wasn't set*/ - goto cleanup; + return -1; } } else { /* Inject server-duid into those ipv6 leases which * didn't have it previously, for example, those * created by leaseshelper from libvirt 1.2.6 */ if (virJSONValueObjectAppendString(lease_tmp, "server-duid= ", *server_duid) < 0) - goto cleanup; + return -1; } } =20 @@ -136,17 +132,13 @@ virLeaseReadCustomLeaseFile(virJSONValuePtr leases_ar= ray_new, if (virJSONValueArrayAppend(leases_array_new, lease_tmp) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to create json")); - goto cleanup; + return -1; } =20 ignore_value(virJSONValueArraySteal(leases_array, i)); } =20 - ret =3D 0; - - cleanup: - virJSONValueFree(leases_array); - return ret; + return 0; } =20 =20 @@ -157,7 +149,6 @@ virLeasePrintLeases(virJSONValuePtr leases_array_new, virJSONValuePtr lease_tmp =3D NULL; const char *ip_tmp =3D NULL; long long expirytime =3D 0; - int ret =3D -1; size_t i; =20 /* Man page of dnsmasq says: the script (helper program, in our case) @@ -174,7 +165,7 @@ virLeasePrintLeases(virJSONValuePtr leases_array_new, if (!(ip_tmp =3D virJSONValueObjectGetString(lease_tmp, "ip-addres= s"))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to parse json")); - goto cleanup; + return -1; } if (!strchr(ip_tmp, ':')) { if (virJSONValueObjectGetNumberLong(lease_tmp, "expiry-time", @@ -198,7 +189,7 @@ virLeasePrintLeases(virJSONValuePtr leases_array_new, if (!(ip_tmp =3D virJSONValueObjectGetString(lease_tmp, "ip-ad= dress"))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to parse json")); - goto cleanup; + return -1; } if (strchr(ip_tmp, ':')) { if (virJSONValueObjectGetNumberLong(lease_tmp, "expiry-tim= e", @@ -215,10 +206,7 @@ virLeasePrintLeases(virJSONValuePtr leases_array_new, } } =20 - ret =3D 0; - - cleanup: - return ret; + return 0; } =20 =20 @@ -231,24 +219,21 @@ virLeaseNew(virJSONValuePtr *lease_ret, const char *iaid, const char *server_duid) { - virJSONValuePtr lease_new =3D NULL; + VIR_AUTOPTR(virJSONValue) lease_new =3D NULL; const char *exptime_tmp =3D virGetEnvAllowSUID("DNSMASQ_LEASE_EXPIRES"= ); long long expirytime =3D 0; VIR_AUTOFREE(char *) exptime =3D NULL; - int ret =3D -1; =20 /* In case hostname is still unknown, use the last known one */ if (!hostname) hostname =3D virGetEnvAllowSUID("DNSMASQ_OLD_HOSTNAME"); =20 - if (!mac) { - ret =3D 0; - goto cleanup; - } + if (!mac) + return 0; =20 if (exptime_tmp) { if (VIR_STRDUP(exptime, exptime_tmp) < 0) - goto cleanup; + return -1; =20 /* Removed extraneous trailing space in DNSMASQ_LEASE_EXPIRES * (dnsmasq < 2.52) */ @@ -261,35 +246,32 @@ virLeaseNew(virJSONValuePtr *lease_ret, virReportError(VIR_ERR_INTERNAL_ERROR, _("Unable to convert lease expiry time to long long= : %s"), NULLSTR(exptime)); - goto cleanup; + return -1; } =20 /* Create new lease */ if (!(lease_new =3D virJSONValueNewObject())) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to create json")); - goto cleanup; + return -1; } =20 if (iaid && virJSONValueObjectAppendString(lease_new, "iaid", iaid) < = 0) - goto cleanup; + return -1; if (ip && virJSONValueObjectAppendString(lease_new, "ip-address", ip) = < 0) - goto cleanup; + return -1; if (mac && virJSONValueObjectAppendString(lease_new, "mac-address", ma= c) < 0) - goto cleanup; + return -1; if (hostname && virJSONValueObjectAppendString(lease_new, "hostname", = hostname) < 0) - goto cleanup; + return -1; if (clientid && virJSONValueObjectAppendString(lease_new, "client-id",= clientid) < 0) - goto cleanup; + return -1; if (server_duid && virJSONValueObjectAppendString(lease_new, "server-d= uid", server_duid) < 0) - goto cleanup; + return -1; if (expirytime && virJSONValueObjectAppendNumberLong(lease_new, "expir= y-time", expirytime) < 0) - goto cleanup; + return -1; =20 - ret =3D 0; *lease_ret =3D lease_new; lease_new =3D NULL; - cleanup: - virJSONValueFree(lease_new); - return ret; + return 0; } --=20 1.8.3.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list