From nobody Sun May 19 23:41:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1587832573; cv=none; d=zohomail.com; s=zohoarc; b=DxB1TslgEvWVtLW7SUi1dW6cd/jW6Is/Q4coOQYv/0RJXQway7pNHY29uYx9Ayf6EzEMwuxWXtisio3H8OHOuJC7SOKAZ22RuZoNQzmex5e0SVZexIpNDJLuRI1kgzhgQESbNKF8cmjUpnBiemD8XJNHdptGp1586mx0hiN2mfg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1587832573; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=IPGH7isPYl1N23lHtfJczAOxv92OVcZMRdAJIVUYEeU=; b=A7E2JI+FUvnOCBVQ/4WMg7vjfmOx31YhQax0bfFGV77ia0SSuk6oIfOrHLAehaQ6uDyfvXE5tQ0UB8mhLUdP7DijNl8CKtG7UV0MeUyhKk4Q9hdPo4Z7AZqtwOuZPE0BVcPp2PmMaFOMx2T24ye+GZrNzpSVZmYWdyKCbAoLzlc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1587832573398719.4404516015793; Sat, 25 Apr 2020 09:36:13 -0700 (PDT) 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-344-2DyTotXXO3qyPArmQC-mvg-1; Sat, 25 Apr 2020 12:36:09 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AE0231005510; Sat, 25 Apr 2020 16:36:01 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C57B55D9CA; Sat, 25 Apr 2020 16:35:59 +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 2ECF64CAA0; Sat, 25 Apr 2020 16:35:54 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 03PGZo5d012914 for ; Sat, 25 Apr 2020 12:35:51 -0400 Received: by smtp.corp.redhat.com (Postfix) id DAE082029F60; Sat, 25 Apr 2020 16:35:50 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast06.extmail.prod.ext.rdu2.redhat.com [10.11.55.22]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D6B892026D5D for ; Sat, 25 Apr 2020 16:35:48 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9C8E4185A78B for ; Sat, 25 Apr 2020 16:35:48 +0000 (UTC) Received: from mail-qt1-f194.google.com (mail-qt1-f194.google.com [209.85.160.194]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-439-iMXWyVW5Po6sNMmaREr0fQ-1; Sat, 25 Apr 2020 12:35:46 -0400 Received: by mail-qt1-f194.google.com with SMTP id o10so10652065qtr.6; Sat, 25 Apr 2020 09:35:46 -0700 (PDT) Received: from localhost.localdomain ([177.138.185.68]) by smtp.gmail.com with ESMTPSA id z26sm6158494qkg.39.2020.04.25.09.35.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Apr 2020 09:35:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1587832571; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=IPGH7isPYl1N23lHtfJczAOxv92OVcZMRdAJIVUYEeU=; b=VWlCIOYb+3nKOE9fw/O49P7lq5oWNizrgHhd11UcGectVvEEyUvUqC+OSwmr0teWjW2gX3 pjbQgzm3FKYPJ3ATROZFNcdzQEotB7u5tJTJblpa0wM2e8iafXlPrmqCtM6hlDqcs8hfXy QZ5L+l/js7hft8tU56YGXabUVWGaKTQ= X-MC-Unique: 2DyTotXXO3qyPArmQC-mvg-1 X-MC-Unique: iMXWyVW5Po6sNMmaREr0fQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=yGno5hioX5ehUUhAdW/oY18UkaIRsaCtS8YMsuS2KlA=; b=XYuW6dg8Fjtdi6j/G8IORliUnKTpfzOANMthZ7o5gZFvQAmO6ZMp05C0IhyuQjuYzx gALpEeLhxCXu9pn/c3qDrCgYYcHdYgZAEUaMa7fRoaL3MXk/gEvHhf8pyC8ghTOqxb13 OvWGb1ySN1BOXFG1rrTwobN1SUgdAyvueEgF9smZ91wPbnSOk3xMc1IR9CyFPwFgZ+PN JTbXt5X32ccgroT01iCRxnp/hb9bvdtZV1xZHgmZlo82tKFqJUoWMrLRpja5IPxPaMog YvwbHQb53og9nAiXikFWiD/YXqJPp0WEex5Nb4w6rrsrPTFZjQ4qlhrrrtlFVSaQus27 ZCfw== X-Gm-Message-State: AGi0PuZ06PaJqZJF4Ng14IjSZ48bPVGLWhGp0t6ElvKf2U5utZPVHPio lQvlLhaDLkalXiY6LHmH2kbIlmuf X-Google-Smtp-Source: APiQypLN/p99MnuAPfqlShcWSBe6KAW0HbPyhMb1La1ngBEUG/lXK+tzaCDrQ2DDqE+Jew+zkONUQA== X-Received: by 2002:aed:3ec2:: with SMTP id o2mr14030894qtf.334.1587832545080; Sat, 25 Apr 2020 09:35:45 -0700 (PDT) From: Julio Faracco To: libvir-list@redhat.com Subject: [PATCH] network: Remove memory leak caused by wrong initialization Date: Sat, 25 Apr 2020 13:35:37 -0300 Message-Id: <20200425163537.76199-1-jcfaracco@gmail.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-MIME-Autoconverted: from quoted-printable to 8bit by lists01.pubmisc.prod.ext.phx2.redhat.com id 03PGZo5d012914 X-loop: libvir-list@redhat.com Cc: mprivozn@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.79 on 10.5.11.14 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" This commit fix a wrong variable initialization. There is a variable called `new_lease` which is being initialized with the content of parameter `lease`. To avoid memory leak, the proper way is initialize with NULL first. This wrong statement was added by commit 97a0aa24. There are some other improvements also. Signed-off-by: Julio Faracco Reviewed-by: Michal Privoznik --- src/conf/network_conf.c | 38 ++++++++++++++++++------------------- src/network/bridge_driver.c | 8 ++------ 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index cd60ee7548..a7c177f8db 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -412,38 +412,32 @@ static int virNetworkDHCPLeaseTimeDefParseXML(virNetworkDHCPLeaseTimeDefPtr *lease, xmlNodePtr node) { - virNetworkDHCPLeaseTimeDefPtr new_lease =3D *lease; - g_autofree char *expiry =3D NULL; - g_autofree char *unit =3D NULL; - int unitInt; + virNetworkDHCPLeaseTimeDefPtr new_lease =3D NULL; + g_autofree char *expirystr =3D NULL; + g_autofree char *unitstr =3D NULL; + unsigned long expiry; + int unit =3D VIR_NETWORK_DHCP_LEASETIME_UNIT_MINUTES; =20 - if (!(expiry =3D virXMLPropString(node, "expiry"))) + if (!(expirystr =3D virXMLPropString(node, "expiry"))) return 0; =20 - if (VIR_ALLOC(new_lease) < 0) - return -1; - new_lease->unit =3D VIR_NETWORK_DHCP_LEASETIME_UNIT_MINUTES; - - if (virStrToLong_ul(expiry, NULL, 10, &new_lease->expiry) < 0) + if (virStrToLong_ul(expirystr, NULL, 10, &expiry) < 0) return -1; =20 - if ((unit =3D virXMLPropString(node, "unit"))) { - if ((unitInt =3D virNetworkDHCPLeaseTimeUnitTypeFromString(unit)) = < 0) { + if ((unitstr =3D virXMLPropString(node, "unit"))) { + if ((unit =3D virNetworkDHCPLeaseTimeUnitTypeFromString(unitstr)) = < 0) { virReportError(VIR_ERR_XML_ERROR, - _("Invalid unit: %s"), unit); + _("Invalid unit: %s"), unitstr); return -1; } - new_lease->unit =3D unitInt; } =20 /* infinite */ - if (new_lease->expiry > 0) { + if (expiry > 0) { /* This boundary check is related to dnsmasq man page settings: * "The minimum lease time is two minutes." */ - if ((new_lease->unit =3D=3D VIR_NETWORK_DHCP_LEASETIME_UNIT_SECOND= S && - new_lease->expiry < 120) || - (new_lease->unit =3D=3D VIR_NETWORK_DHCP_LEASETIME_UNIT_MINUTE= S && - new_lease->expiry < 2)) { + if ((unit =3D=3D VIR_NETWORK_DHCP_LEASETIME_UNIT_SECONDS && expiry= < 120) || + (unit =3D=3D VIR_NETWORK_DHCP_LEASETIME_UNIT_MINUTES && expiry= < 2)) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("The minimum lease time should be greater " "than 2 minutes")); @@ -451,6 +445,12 @@ virNetworkDHCPLeaseTimeDefParseXML(virNetworkDHCPLease= TimeDefPtr *lease, } } =20 + if (VIR_ALLOC(new_lease) < 0) + return -1; + + new_lease->expiry =3D expiry; + new_lease->unit =3D unit; + *lease =3D new_lease; =20 return 0; diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index 87f0452611..e8f0dcf7d0 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -969,7 +969,6 @@ static int networkConnectIsAlive(virConnectPtr conn G_G= NUC_UNUSED) static char * networkBuildDnsmasqLeaseTime(virNetworkDHCPLeaseTimeDefPtr lease) { - char *leasetime =3D NULL; const char *unit; g_auto(virBuffer) buf =3D VIR_BUFFER_INITIALIZER; =20 @@ -984,9 +983,7 @@ networkBuildDnsmasqLeaseTime(virNetworkDHCPLeaseTimeDef= Ptr lease) virBufferAsprintf(&buf, "%lu%c", lease->expiry, unit[0]); } =20 - leasetime =3D virBufferContentAndReset(&buf); - - return leasetime; + return virBufferContentAndReset(&buf); } =20 =20 @@ -999,14 +996,13 @@ networkBuildDnsmasqDhcpHostsList(dnsmasqContext *dctx, { size_t i; bool ipv6 =3D false; - g_autofree char *leasetime =3D NULL; =20 if (VIR_SOCKET_ADDR_IS_FAMILY(&ipdef->address, AF_INET6)) ipv6 =3D true; for (i =3D 0; i < ipdef->nhosts; i++) { virNetworkDHCPHostDefPtr host =3D &(ipdef->hosts[i]); + g_autofree char *leasetime =3D networkBuildDnsmasqLeaseTime(host->= lease); =20 - leasetime =3D networkBuildDnsmasqLeaseTime(host->lease); if (VIR_SOCKET_ADDR_VALID(&host->ip)) if (dnsmasqAddDhcpHost(dctx, host->mac, &host->ip, host->name, host->id, leasetime, --=20 2.25.3