From nobody Fri Dec 19 07:49:57 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1608304331; cv=none; d=zohomail.com; s=zohoarc; b=CA0kojnVnlV6O3q2Tq5cik+bT+gtLw0u58BkvGnSa7D3RZS48AhzvNkQjUWAXibSqfJcKTwDP6rgMX+gXio9IAoeDkVQUBXQadJycqxz7slWgomMbbo3bkR6sTj7bZ12aaoNdzsfmfMPDPFmOM7YrjlzOyc0Ra67+d6jBrysgK4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1608304331; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=nXtQJqsdgRppIAASC/NcoaDRPonMcj1LQk5ednuu+RY=; b=iAkERX+7+3v9UC9svawTyymhpUcAcaJGPTI0IZSm2h0FdYrKXLzWgVBrqxb1lvGZKeSMhZPZrj4jcQPAFfhqwLkchVdPfBopkedkQ2Oy5rB2Kf7jpF+xYMJpvrLW1R+gyKObIL/yqTMpZqXsY8YiL8AFWkVnpMpJsSqhB0WvM4Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1608304331443286.296873322877; Fri, 18 Dec 2020 07:12:11 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-324-sqEoNxoSOFy5CdxdqKtWog-1; Fri, 18 Dec 2020 10:10:00 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B1CAB107ACF9; Fri, 18 Dec 2020 15:09:54 +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 8AF9310013C0; Fri, 18 Dec 2020 15:09:54 +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 55EAC1809CAC; Fri, 18 Dec 2020 15:09:54 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 0BIF9Uxu014094 for ; Fri, 18 Dec 2020 10:09:30 -0500 Received: by smtp.corp.redhat.com (Postfix) id 88879100238C; Fri, 18 Dec 2020 15:09:30 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.193.214]) by smtp.corp.redhat.com (Postfix) with ESMTP id EE8FE1002382 for ; Fri, 18 Dec 2020 15:09:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1608304328; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=nXtQJqsdgRppIAASC/NcoaDRPonMcj1LQk5ednuu+RY=; b=AeYEhoNgIuBoKhco54xOc/8km4Z+uUjJ9hlczPO5/5xs2ezZT2zx95J62cPzb4sWRlbODz LqOefWmh9enm/sFNznJSTrRo7aWb695vZvzZBNbisR3X17I2QL/qNO3YLFa0CbsJrFMSsM YFZfq6hMsiHKdnBGa1u31hEl63AdoPM= X-MC-Unique: sqEoNxoSOFy5CdxdqKtWog-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 08/10] network: Rework networkGetDHCPLeases() Date: Fri, 18 Dec 2020 16:09:14 +0100 Message-Id: <538b10683c7c91eb80e2fe91db2e28a8ecf837b7.1608303776.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Firstly, bring variables that are used only within loops into their respective loops. Secondly, drop 'error' label which is redundant since we have @rv which holds the return value. Thirdly, fix indendation in one case, the rest is indented properly. Signed-off-by: Michal Privoznik --- src/network/bridge_driver.c | 47 +++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index 43102a02c1..fcc7e05ad8 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -4031,22 +4031,16 @@ networkGetDHCPLeases(virNetworkPtr net, unsigned int flags) { virNetworkDriverStatePtr driver =3D networkGetDriver(); - size_t i, j; + size_t i; size_t nleases =3D 0; int rv =3D -1; size_t size =3D 0; bool need_results =3D !!leases; long long currtime =3D 0; - long long expirytime_tmp =3D -1; - bool ipv6 =3D false; g_autofree char *lease_entries =3D NULL; g_autofree char *custom_lease_file =3D NULL; - const char *ip_tmp =3D NULL; - const char *mac_tmp =3D NULL; - virJSONValuePtr lease_tmp =3D NULL; g_autoptr(virJSONValue) leases_array =3D NULL; - virNetworkIPDefPtr ipdef_tmp =3D NULL; - virNetworkDHCPLeasePtr *leases_ret =3D NULL; + g_autofree virNetworkDHCPLeasePtr *leases_ret =3D NULL; virNetworkObjPtr obj; virNetworkDefPtr def; virMacAddr mac_addr; @@ -4084,34 +4078,38 @@ networkGetDHCPLeases(virNetworkPtr net, _("Unable to read leases file: %s"), custom_lease_file); } - goto error; + goto cleanup; } =20 if (STREQ(lease_entries, "")) { rv =3D 0; - goto error; + goto cleanup; } =20 if (!(leases_array =3D virJSONValueFromString(lease_entries))) { virReportError(VIR_ERR_INTERNAL_ERROR, _("invalid json in file: %s"), custom_lease_file); - goto error; + goto cleanup; } =20 if (!virJSONValueIsArray(leases_array)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Malformed lease_entries array")); - goto error; + goto cleanup; } size =3D virJSONValueArraySize(leases_array); =20 currtime =3D (long long)time(NULL); =20 for (i =3D 0; i < size; i++) { - if (!(lease_tmp =3D virJSONValueArrayGet(leases_array, i))) { + virJSONValuePtr lease_tmp =3D virJSONValueArrayGet(leases_array, i= ); + long long expirytime_tmp =3D -1; + const char *mac_tmp =3D NULL; + + if (!lease_tmp) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to parse json")); - goto error; + goto cleanup; } =20 if (!(mac_tmp =3D virJSONValueObjectGetString(lease_tmp, "mac-addr= ess"))) { @@ -4119,7 +4117,7 @@ networkGetDHCPLeases(virNetworkPtr net, * mac-address is known otherwise not */ virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("found lease without mac-address")); - goto error; + goto cleanup; } =20 if (mac && virMacAddrCompare(mac, mac_tmp)) @@ -4129,7 +4127,7 @@ networkGetDHCPLeases(virNetworkPtr net, /* A lease cannot be present without expiry-time */ virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("found lease without expiry-time")); - goto error; + goto cleanup; } =20 /* Do not report expired lease */ @@ -4138,6 +4136,9 @@ networkGetDHCPLeases(virNetworkPtr net, =20 if (need_results) { g_autoptr(virNetworkDHCPLease) lease =3D g_new0(virNetworkDHCP= Lease, 1); + const char *ip_tmp =3D NULL; + bool ipv6 =3D false; + size_t j; =20 lease->expirytime =3D expirytime_tmp; =20 @@ -4145,7 +4146,7 @@ networkGetDHCPLeases(virNetworkPtr net, /* A lease without ip-address makes no sense */ virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("found lease without ip-address")); - goto error; + goto cleanup; } =20 /* Unlike IPv4, IPv6 uses ':' instead of '.' as separator */ @@ -4154,7 +4155,7 @@ networkGetDHCPLeases(virNetworkPtr net, =20 /* Obtain prefix */ for (j =3D 0; j < def->nips; j++) { - ipdef_tmp =3D &def->ips[j]; + virNetworkIPDefPtr ipdef_tmp =3D ipdef_tmp =3D &def->ips[j= ]; =20 if (ipv6 && VIR_SOCKET_ADDR_IS_FAMILY(&ipdef_tmp->address, AF_INET6)) { @@ -4162,7 +4163,7 @@ networkGetDHCPLeases(virNetworkPtr net, break; } if (!ipv6 && VIR_SOCKET_ADDR_IS_FAMILY(&ipdef_tmp->address, - AF_INET)) { + AF_INET)) { lease->prefix =3D virSocketAddrGetIPPrefix(&ipdef_tmp-= >address, &ipdef_tmp->n= etmask, ipdef_tmp->pr= efix); @@ -4180,7 +4181,7 @@ networkGetDHCPLeases(virNetworkPtr net, lease->hostname =3D g_strdup(virJSONValueObjectGetString(lease= _tmp, "hostname")); =20 if (VIR_APPEND_ELEMENT(leases_ret, nleases, lease) < 0) - goto error; + goto cleanup; =20 } else { nleases++; @@ -4197,15 +4198,11 @@ networkGetDHCPLeases(virNetworkPtr net, =20 cleanup: virNetworkObjEndAPI(&obj); - return rv; - - error: if (leases_ret) { for (i =3D 0; i < nleases; i++) virNetworkDHCPLeaseFree(leases_ret[i]); - g_free(leases_ret); } - goto cleanup; + return rv; } =20 =20 --=20 2.26.2