From nobody Mon Nov 25 21:51:28 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) client-ip=207.211.31.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 207.211.31.120 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=1594156502; cv=none; d=zohomail.com; s=zohoarc; b=LOPB3LNIhP1zQkYINeEvoV/vn1+jLZoE6T3eFEZF3h/7zU3tnLBAejPt51n7z9mY5yngIJ/tCArB3Ajh49it09s6ptBPINLCirookl2ixtuBw5NnTDosxIusUkQBfF2CRoe6v8p6Z9Yyg2btw1Ci9Pfpfspc4Hkb7zONK1ymNEw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594156502; 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=bA26iqyIikBHvyFWvprvGlDk4SkPHiSLHPULS9ThjCY=; b=ULQLnx0ftUM5g8r5JSuJbjusf8uFoeALI8sTC1HWVPN7igon5mQhvZ4v4O/zMqfYYgUaw0CCThNF8K++QCwc28fIWeQKHuI2cSCnOVKh6tVMzlXCv1CG5gs5dlVykkFwmbFzkVnSkjCkpbteYqUVTJcbcZrX929XHGM08Gbx4/8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 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-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by mx.zohomail.com with SMTPS id 1594156501999688.5581940854748; Tue, 7 Jul 2020 14:15:01 -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-85-gJqKNnwBNNmbIZcHoRS0Xw-1; Tue, 07 Jul 2020 17:14:58 -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 270C4EC1AE; Tue, 7 Jul 2020 21:14:52 +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 054B15D9C9; Tue, 7 Jul 2020 21:14:52 +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 CB2B91809557; Tue, 7 Jul 2020 21:14:51 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 067L8PMc007959 for ; Tue, 7 Jul 2020 17:08:25 -0400 Received: by smtp.corp.redhat.com (Postfix) id 70608797EB; Tue, 7 Jul 2020 21:08:25 +0000 (UTC) Received: from vhost2.laine.org (ovpn-112-194.phx2.redhat.com [10.3.112.194]) by smtp.corp.redhat.com (Postfix) with ESMTP id 10F3E797E0 for ; Tue, 7 Jul 2020 21:08:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594156500; 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=bA26iqyIikBHvyFWvprvGlDk4SkPHiSLHPULS9ThjCY=; b=NpMgRiN/rlI26Lo8sssa4ny5RA7MwQ5qBUXgL72wokiviXMM5I/pjvsQblRAgoka535C0W kcN6JoNliJvTuWnGhvW/cC/mkv0KGDWTOc+8ztgrKUqGNybhz/xKSUMMBE5zmP1Y1L8ZEC cvBPDOLKN8e/WVu5jD57fdhuPjLzL58= X-MC-Unique: gJqKNnwBNNmbIZcHoRS0Xw-1 From: Laine Stump To: libvir-list@redhat.com Subject: [libvirt PATCH v2 05/15] network: use g_auto wherever appropriate Date: Tue, 7 Jul 2020 17:08:05 -0400 Message-Id: <20200707210815.1797463-6-laine@redhat.com> In-Reply-To: <20200707210815.1797463-1-laine@redhat.com> References: <20200707210815.1797463-1-laine@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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.79 on 10.5.11.14 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" This includes standard g_autofree() as well as other objects that have a cleanup function defined to use via g_autoptr (virCommand, virJSONValue) Signed-off-by: Laine Stump Reviewed-by: J=C3=A1n Tomko --- src/network/bridge_driver.c | 206 ++++++++++-------------------- src/network/bridge_driver_linux.c | 7 +- src/network/leaseshelper.c | 16 +-- 3 files changed, 76 insertions(+), 153 deletions(-) diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index ab359acdb5..31bd0dd92c 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -160,6 +160,7 @@ networkDnsmasqDefNamespaceFree(void *nsdata) =20 VIR_FREE(def); } +G_DEFINE_AUTOPTR_CLEANUP_FUNC(networkDnsmasqXmlNsDef, networkDnsmasqDefNam= espaceFree); =20 =20 static int @@ -195,7 +196,7 @@ static int networkDnsmasqDefNamespaceParse(xmlXPathContextPtr ctxt, void **data) { - networkDnsmasqXmlNsDefPtr nsdata =3D g_new0(networkDnsmasqXmlNsDef, 1); + g_autoptr(networkDnsmasqXmlNsDef) nsdata =3D g_new0(networkDnsmasqXmlN= sDef, 1); int ret =3D -1; =20 if (networkDnsmasqDefNamespaceParseOptions(nsdata, ctxt)) @@ -207,7 +208,6 @@ networkDnsmasqDefNamespaceParse(xmlXPathContextPtr ctxt, ret =3D 0; =20 cleanup: - networkDnsmasqDefNamespaceFree(nsdata); return ret; } =20 @@ -329,7 +329,7 @@ networkRunHook(virNetworkObjPtr obj, { virNetworkDefPtr def; g_auto(virBuffer) buf =3D VIR_BUFFER_INITIALIZER; - char *xml =3D NULL; + g_autofree char *xml =3D NULL; int hookret; int ret =3D -1; =20 @@ -366,7 +366,6 @@ networkRunHook(virNetworkObjPtr obj, =20 ret =3D 0; cleanup: - VIR_FREE(xml); return ret; } =20 @@ -431,14 +430,14 @@ static int networkRemoveInactive(virNetworkDriverStatePtr driver, virNetworkObjPtr obj) { - char *leasefile =3D NULL; - char *customleasefile =3D NULL; - char *radvdconfigfile =3D NULL; - char *configfile =3D NULL; - char *radvdpidbase =3D NULL; - char *statusfile =3D NULL; - char *macMapFile =3D NULL; - dnsmasqContext *dctx =3D NULL; + g_autofree char *leasefile =3D NULL; + g_autofree char *customleasefile =3D NULL; + g_autofree char *radvdconfigfile =3D NULL; + g_autofree char *configfile =3D NULL; + g_autofree char *radvdpidbase =3D NULL; + g_autofree char *statusfile =3D NULL; + g_autofree char *macMapFile =3D NULL; + g_autoptr(dnsmasqContext) dctx =3D NULL; virNetworkDefPtr def =3D virNetworkObjGetPersistentDef(obj); =20 int ret =3D -1; @@ -492,14 +491,6 @@ networkRemoveInactive(virNetworkDriverStatePtr driver, ret =3D 0; =20 cleanup: - VIR_FREE(leasefile); - VIR_FREE(configfile); - VIR_FREE(customleasefile); - VIR_FREE(radvdconfigfile); - VIR_FREE(radvdpidbase); - VIR_FREE(statusfile); - VIR_FREE(macMapFile); - dnsmasqContextFree(dctx); return ret; } =20 @@ -550,9 +541,9 @@ networkUpdateState(virNetworkObjPtr obj, { virNetworkDefPtr def; virNetworkDriverStatePtr driver =3D opaque; - dnsmasqCapsPtr dnsmasq_caps =3D networkGetDnsmasqCaps(driver); + g_autoptr(dnsmasqCaps) dnsmasq_caps =3D networkGetDnsmasqCaps(driver); virMacMapPtr macmap; - char *macMapFile =3D NULL; + g_autofree char *macMapFile =3D NULL; int ret =3D -1; =20 virObjectLock(obj); @@ -614,7 +605,7 @@ networkUpdateState(virNetworkObjPtr obj, if (virNetworkObjIsActive(obj) && def->ips && (def->nips > 0)) { pid_t radvdPid; pid_t dnsmasqPid; - char *radvdpidbase; + g_autofree char *radvdpidbase =3D NULL; =20 ignore_value(virPidFileReadIfAlive(driver->pidDir, def->name, @@ -630,14 +621,11 @@ networkUpdateState(virNetworkObjPtr obj, radvdpidbase, &radvdPid, RADVD)); virNetworkObjSetRadvdPid(obj, radvdPid); - VIR_FREE(radvdpidbase); } =20 ret =3D 0; cleanup: virObjectUnlock(obj); - virObjectUnref(dnsmasq_caps); - VIR_FREE(macMapFile); return ret; } =20 @@ -716,8 +704,8 @@ networkStateInitialize(bool privileged, void *opaque G_GNUC_UNUSED) { int ret =3D VIR_DRV_STATE_INIT_ERROR; - char *configdir =3D NULL; - char *rundir =3D NULL; + g_autofree char *configdir =3D NULL; + g_autofree char *rundir =3D NULL; bool autostart =3D true; #ifdef WITH_FIREWALLD DBusConnection *sysbus =3D NULL; @@ -845,8 +833,6 @@ networkStateInitialize(bool privileged, =20 ret =3D VIR_DRV_STATE_INIT_COMPLETE; cleanup: - VIR_FREE(configdir); - VIR_FREE(rundir); return ret; =20 error: @@ -1047,10 +1033,11 @@ networkDnsmasqConfLocalPTRs(virBufferPtr buf, { virNetworkIPDefPtr ip; size_t i; - char *ptr =3D NULL; int rc; =20 for (i =3D 0; i < def->nips; i++) { + g_autofree char *ptr =3D NULL; + ip =3D def->ips + i; =20 if (ip->localPTR !=3D VIR_TRISTATE_BOOL_YES) @@ -1071,7 +1058,6 @@ networkDnsmasqConfLocalPTRs(virBufferPtr buf, } =20 virBufferAsprintf(buf, "local=3D/%s/\n", ptr); - VIR_FREE(ptr); } =20 return 0; @@ -1095,7 +1081,6 @@ networkDnsmasqConfContents(virNetworkObjPtr obj, bool wantDNS =3D dns->enable !=3D VIR_TRISTATE_BOOL_NO; virNetworkIPDefPtr tmpipdef, ipdef, ipv4def, ipv6def; bool ipv6SLAAC; - char *saddr =3D NULL, *eaddr =3D NULL; =20 *configstr =3D NULL; =20 @@ -1150,12 +1135,11 @@ networkDnsmasqConfContents(virNetworkObjPtr obj, if (fwd->domain) virBufferAsprintf(&configbuf, "/%s/", fwd->domain); if (VIR_SOCKET_ADDR_VALID(&fwd->addr)) { - char *addr =3D virSocketAddrFormat(&fwd->addr); + g_autofree char *addr =3D virSocketAddrFormat(&fwd->addr); =20 if (!addr) goto cleanup; virBufferAsprintf(&configbuf, "%s\n", addr); - VIR_FREE(addr); if (!fwd->domain) addNoResolv =3D true; } else { @@ -1228,7 +1212,7 @@ networkDnsmasqConfContents(virNetworkObjPtr obj, for (i =3D 0; (tmpipdef =3D virNetworkDefGetIPByIndex(def, AF_UNSPEC, i)); i++) { - char *ipaddr =3D virSocketAddrFormat(&tmpipdef->address); + g_autofree char *ipaddr =3D virSocketAddrFormat(&tmpipdef->add= ress); =20 if (!ipaddr) goto cleanup; @@ -1254,11 +1238,9 @@ networkDnsmasqConfContents(virNetworkObjPtr obj, "(as described in RFC1918/RFC3484/RFC4193= )."), ipaddr, (int)version / 1000000, (int)(version % 1000000) / 1000); - VIR_FREE(ipaddr); goto cleanup; } virBufferAsprintf(&configbuf, "listen-address=3D%s\n", ipaddr); - VIR_FREE(ipaddr); } } =20 @@ -1414,6 +1396,8 @@ networkDnsmasqConfContents(virNetworkObjPtr obj, int thisRange; virNetworkDHCPRangeDef range =3D ipdef->ranges[r]; g_autofree char *leasetime =3D NULL; + g_autofree char *saddr =3D NULL; + g_autofree char *eaddr =3D NULL; =20 if (!(saddr =3D virSocketAddrFormat(&range.addr.start)) || !(eaddr =3D virSocketAddrFormat(&range.addr.end))) @@ -1446,8 +1430,6 @@ networkDnsmasqConfContents(virNetworkObjPtr obj, =20 virBufferAddLit(&configbuf, "\n"); =20 - VIR_FREE(saddr); - VIR_FREE(eaddr); thisRange =3D virSocketAddrGetRange(&range.addr.start, &range.addr.end, &ipdef->address, @@ -1464,7 +1446,7 @@ networkDnsmasqConfContents(virNetworkObjPtr obj, * support) */ if (!ipdef->nranges && ipdef->nhosts) { - char *bridgeaddr =3D virSocketAddrFormat(&ipdef->address); + g_autofree char *bridgeaddr =3D virSocketAddrFormat(&ipdef->ad= dress); if (!bridgeaddr) goto cleanup; virBufferAsprintf(&configbuf, "dhcp-range=3D%s,static", @@ -1472,7 +1454,6 @@ networkDnsmasqConfContents(virNetworkObjPtr obj, if (VIR_SOCKET_ADDR_IS_FAMILY(&ipdef->address, AF_INET6)) virBufferAsprintf(&configbuf, ",%d", prefix); virBufferAddLit(&configbuf, "\n"); - VIR_FREE(bridgeaddr); } =20 if (networkBuildDnsmasqDhcpHostsList(dctx, ipdef) < 0) @@ -1492,13 +1473,12 @@ networkDnsmasqConfContents(virNetworkObjPtr obj, =20 if (ipdef->bootfile) { if (VIR_SOCKET_ADDR_VALID(&ipdef->bootserver)) { - char *bootserver =3D virSocketAddrFormat(&ipdef->boots= erver); + g_autofree char *bootserver =3D virSocketAddrFormat(&i= pdef->bootserver); =20 if (!bootserver) goto cleanup; virBufferAsprintf(&configbuf, "dhcp-boot=3D%s%s%s\n", ipdef->bootfile, ",,", bootserver); - VIR_FREE(bootserver); } else { virBufferAsprintf(&configbuf, "dhcp-boot=3D%s\n", ipde= f->bootfile); } @@ -1543,12 +1523,11 @@ networkDnsmasqConfContents(virNetworkObjPtr obj, (ipdef =3D virNetworkDefGetIPByIndex(def, AF_INET6, i)); i++) { if (!(ipdef->nranges || ipdef->nhosts)) { - char *bridgeaddr =3D virSocketAddrFormat(&ipdef->addre= ss); + g_autofree char *bridgeaddr =3D virSocketAddrFormat(&i= pdef->address); if (!bridgeaddr) goto cleanup; virBufferAsprintf(&configbuf, "dhcp-range=3D%s,ra-only\n", bridgea= ddr); - VIR_FREE(bridgeaddr); } } } @@ -1569,8 +1548,6 @@ networkDnsmasqConfContents(virNetworkObjPtr obj, ret =3D 0; =20 cleanup: - VIR_FREE(saddr); - VIR_FREE(eaddr); return ret; } =20 @@ -1584,13 +1561,13 @@ networkBuildDhcpDaemonCommandLine(virNetworkDriverS= tatePtr driver, dnsmasqContext *dctx) { virNetworkDefPtr def =3D virNetworkObjGetDef(obj); - dnsmasqCapsPtr dnsmasq_caps =3D networkGetDnsmasqCaps(driver); - virCommandPtr cmd =3D NULL; + g_autoptr(dnsmasqCaps) dnsmasq_caps =3D networkGetDnsmasqCaps(driver); + g_autoptr(virCommand) cmd =3D NULL; int ret =3D -1; - char *configfile =3D NULL; - char *configstr =3D NULL; - char *hostsfilestr =3D NULL; - char *leaseshelper_path =3D NULL; + g_autofree char *configfile =3D NULL; + g_autofree char *configstr =3D NULL; + g_autofree char *hostsfilestr =3D NULL; + g_autofree char *leaseshelper_path =3D NULL; =20 virNetworkObjSetDnsmasqPid(obj, -1); =20 @@ -1625,14 +1602,9 @@ networkBuildDhcpDaemonCommandLine(virNetworkDriverSt= atePtr driver, virCommandAddArgFormat(cmd, "--dhcp-script=3D%s", leaseshelper_path); virCommandAddEnvPair(cmd, "VIR_BRIDGE_NAME", def->bridge); =20 - *cmdout =3D cmd; + *cmdout =3D g_steal_pointer(&cmd); ret =3D 0; cleanup: - virObjectUnref(dnsmasq_caps); - VIR_FREE(configfile); - VIR_FREE(configstr); - VIR_FREE(hostsfilestr); - VIR_FREE(leaseshelper_path); return ret; } =20 @@ -1645,11 +1617,11 @@ networkStartDhcpDaemon(virNetworkDriverStatePtr dri= ver, virNetworkIPDefPtr ipdef; size_t i; bool needDnsmasq =3D false; - virCommandPtr cmd =3D NULL; - char *pidfile =3D NULL; + g_autoptr(virCommand) cmd =3D NULL; + g_autofree char *pidfile =3D NULL; pid_t dnsmasqPid; int ret =3D -1; - dnsmasqContext *dctx =3D NULL; + g_autoptr(dnsmasqContext) dctx =3D NULL; =20 /* see if there are any IP addresses that need a dhcp server */ i =3D 0; @@ -1722,9 +1694,6 @@ networkStartDhcpDaemon(virNetworkDriverStatePtr drive= r, =20 ret =3D 0; cleanup: - VIR_FREE(pidfile); - virCommandFree(cmd); - dnsmasqContextFree(dctx); return ret; } =20 @@ -1745,7 +1714,7 @@ networkRefreshDhcpDaemon(virNetworkDriverStatePtr dri= ver, size_t i; pid_t dnsmasqPid; virNetworkIPDefPtr ipdef, ipv4def, ipv6def; - dnsmasqContext *dctx =3D NULL; + g_autoptr(dnsmasqContext) dctx =3D NULL; =20 /* if no IP addresses specified, nothing to do */ if (!virNetworkDefGetIPByIndex(def, AF_UNSPEC, 0)) @@ -1797,7 +1766,6 @@ networkRefreshDhcpDaemon(virNetworkDriverStatePtr dri= ver, dnsmasqPid =3D virNetworkObjGetDnsmasqPid(obj); ret =3D kill(dnsmasqPid, SIGHUP); cleanup: - dnsmasqContextFree(dctx); return ret; } =20 @@ -1872,7 +1840,7 @@ networkRadvdConfContents(virNetworkObjPtr obj, /* add a section for each IPv6 address in the config */ for (i =3D 0; (ipdef =3D virNetworkDefGetIPByIndex(def, AF_INET6, i));= i++) { int prefix; - char *netaddr; + g_autofree char *netaddr =3D NULL; =20 prefix =3D virNetworkIPDefPrefix(ipdef); if (prefix < 0) { @@ -1889,7 +1857,6 @@ networkRadvdConfContents(virNetworkObjPtr obj, " {\n%s };\n", netaddr, prefix, dhcp6 ? radvd2 : radvd3); - VIR_FREE(netaddr); } =20 virBufferAddLit(&configbuf, "};\n"); @@ -1908,8 +1875,8 @@ networkRadvdConfWrite(virNetworkDriverStatePtr driver, { virNetworkDefPtr def =3D virNetworkObjGetDef(obj); int ret =3D -1; - char *configStr =3D NULL; - char *myConfigFile =3D NULL; + g_autofree char *configStr =3D NULL; + g_autofree char *myConfigFile =3D NULL; =20 if (!configFile) configFile =3D &myConfigFile; @@ -1937,8 +1904,6 @@ networkRadvdConfWrite(virNetworkDriverStatePtr driver, =20 ret =3D 0; cleanup: - VIR_FREE(configStr); - VIR_FREE(myConfigFile); return ret; } =20 @@ -1948,12 +1913,12 @@ networkStartRadvd(virNetworkDriverStatePtr driver, virNetworkObjPtr obj) { virNetworkDefPtr def =3D virNetworkObjGetDef(obj); - dnsmasqCapsPtr dnsmasq_caps =3D networkGetDnsmasqCaps(driver); + g_autoptr(dnsmasqCaps) dnsmasq_caps =3D networkGetDnsmasqCaps(driver); pid_t radvdPid; - char *pidfile =3D NULL; - char *radvdpidbase =3D NULL; - char *configfile =3D NULL; - virCommandPtr cmd =3D NULL; + g_autofree char *pidfile =3D NULL; + g_autofree char *radvdpidbase =3D NULL; + g_autofree char *configfile =3D NULL; + g_autoptr(virCommand) cmd =3D NULL; int ret =3D -1; =20 virNetworkObjSetRadvdPid(obj, -1); @@ -2026,11 +1991,6 @@ networkStartRadvd(virNetworkDriverStatePtr driver, =20 ret =3D 0; cleanup: - virObjectUnref(dnsmasq_caps); - virCommandFree(cmd); - VIR_FREE(configfile); - VIR_FREE(radvdpidbase); - VIR_FREE(pidfile); return ret; } =20 @@ -2040,14 +2000,13 @@ networkRefreshRadvd(virNetworkDriverStatePtr driver, virNetworkObjPtr obj) { virNetworkDefPtr def =3D virNetworkObjGetDef(obj); - dnsmasqCapsPtr dnsmasq_caps =3D networkGetDnsmasqCaps(driver); + g_autoptr(dnsmasqCaps) dnsmasq_caps =3D networkGetDnsmasqCaps(driver); g_autofree char *radvdpidbase =3D NULL; g_autofree char *pidfile =3D NULL; pid_t radvdPid; =20 /* Is dnsmasq handling RA? */ if (DNSMASQ_RA_SUPPORT(dnsmasq_caps)) { - virObjectUnref(dnsmasq_caps); if ((radvdpidbase =3D networkRadvdPidfileBasename(def->name)) && (pidfile =3D virPidFileBuildPath(driver->pidDir, radvdpidbase)= )) { /* radvd should not be running but in case it is */ @@ -2056,7 +2015,6 @@ networkRefreshRadvd(virNetworkDriverStatePtr driver, } return 0; } - virObjectUnref(dnsmasq_caps); =20 /* if there's no running radvd, just start it */ radvdPid =3D virNetworkObjGetRadvdPid(obj); @@ -2248,7 +2206,7 @@ static int networkSetIPv6Sysctls(virNetworkObjPtr obj) { virNetworkDefPtr def =3D virNetworkObjGetDef(obj); - char *field =3D NULL; + g_autofree char *field =3D NULL; int ret =3D -1; bool enableIPv6 =3D !!virNetworkDefGetIPByIndex(def, AF_INET6, 0); =20 @@ -2273,7 +2231,6 @@ networkSetIPv6Sysctls(virNetworkObjPtr obj) "on bridge %s"), field, def->bridge); goto cleanup; } - VIR_FREE(field); =20 /* The rest of the ipv6 sysctl tunables should always be set the * same, whether or not we're using ipv6 on this bridge. @@ -2282,6 +2239,7 @@ networkSetIPv6Sysctls(virNetworkObjPtr obj) /* Prevent guests from hijacking the host network by sending out * their own router advertisements. */ + VIR_FREE(field); field =3D g_strdup_printf(SYSCTL_PATH "/net/ipv6/conf/%s/accept_ra", def->bridge); =20 @@ -2290,11 +2248,11 @@ networkSetIPv6Sysctls(virNetworkObjPtr obj) _("cannot disable %s"), field); goto cleanup; } - VIR_FREE(field); =20 /* All interfaces used as a gateway (which is what this is, by * definition), must always have autoconf=3D0. */ + VIR_FREE(field); field =3D g_strdup_printf(SYSCTL_PATH "/net/ipv6/conf/%s/autoconf", de= f->bridge); =20 if (virFileWriteStr(field, "0", 0) < 0) { @@ -2305,7 +2263,6 @@ networkSetIPv6Sysctls(virNetworkObjPtr obj) =20 ret =3D 0; cleanup: - VIR_FREE(field); return ret; } =20 @@ -2384,7 +2341,8 @@ networkWaitDadFinish(virNetworkObjPtr obj) { virNetworkDefPtr def =3D virNetworkObjGetDef(obj); virNetworkIPDefPtr ipdef; - virSocketAddrPtr *addrs =3D NULL, addr =3D NULL; + g_autofree virSocketAddrPtr *addrs =3D NULL; + virSocketAddrPtr addr =3D NULL; size_t naddrs =3D 0; int ret =3D -1; =20 @@ -2399,7 +2357,6 @@ networkWaitDadFinish(virNetworkObjPtr obj) ret =3D (naddrs =3D=3D 0) ? 0 : virNetDevIPWaitDadFinish(addrs, naddrs= ); =20 cleanup: - VIR_FREE(addrs); VIR_DEBUG("Finished waiting for IPv6 DAD on network %s with status %d", def->name, ret); return ret; @@ -2416,9 +2373,9 @@ networkStartNetworkVirtual(virNetworkDriverStatePtr d= river, virErrorPtr save_err =3D NULL; virNetworkIPDefPtr ipdef; virNetDevIPRoutePtr routedef; - char *macTapIfName =3D NULL; + g_autofree char *macTapIfName =3D NULL; virMacMapPtr macmap; - char *macMapFile =3D NULL; + g_autofree char *macMapFile =3D NULL; int tapfd =3D -1; bool dnsmasqStarted =3D false; bool devOnline =3D false; @@ -2465,7 +2422,6 @@ networkStartNetworkVirtual(virNetworkDriverStatePtr d= river, VIR_NETDEV_TAP_CREATE_USE_MAC_F= OR_BRIDGE | VIR_NETDEV_TAP_CREATE_IFUP | VIR_NETDEV_TAP_CREATE_PERSIST) = < 0) { - VIR_FREE(macTapIfName); goto error; } } @@ -2581,9 +2537,6 @@ networkStartNetworkVirtual(virNetworkDriverStatePtr d= river, if (virNetDevBandwidthSet(def->bridge, def->bandwidth, true, true) < 0) goto error; =20 - VIR_FREE(macTapIfName); - VIR_FREE(macMapFile); - return 0; =20 error: @@ -2607,10 +2560,8 @@ networkStartNetworkVirtual(virNetworkDriverStatePtr = driver, if (macTapIfName) { VIR_FORCE_CLOSE(tapfd); ignore_value(virNetDevTapDelete(macTapIfName, NULL)); - VIR_FREE(macTapIfName); } virNetworkObjUnrefMacMap(obj); - VIR_FREE(macMapFile); =20 ignore_value(virNetDevBridgeDelete(def->bridge)); =20 @@ -2635,14 +2586,12 @@ networkShutdownNetworkVirtual(virNetworkDriverState= Ptr driver, =20 radvdPid =3D virNetworkObjGetRadvdPid(obj); if (radvdPid > 0) { - char *radvdpidbase; + g_autofree char *radvdpidbase =3D NULL; =20 kill(radvdPid, SIGTERM); /* attempt to delete the pidfile we created */ - if ((radvdpidbase =3D networkRadvdPidfileBasename(def->name))) { + if ((radvdpidbase =3D networkRadvdPidfileBasename(def->name))) virPidFileDelete(driver->pidDir, radvdpidbase); - VIR_FREE(radvdpidbase); - } } =20 dnsmasqPid =3D virNetworkObjGetDnsmasqPid(obj); @@ -2650,11 +2599,9 @@ networkShutdownNetworkVirtual(virNetworkDriverStateP= tr driver, kill(dnsmasqPid, SIGTERM); =20 if (def->mac_specified) { - char *macTapIfName =3D networkBridgeDummyNicName(def->bridge); - if (macTapIfName) { + g_autofree char *macTapIfName =3D networkBridgeDummyNicName(def->b= ridge); + if (macTapIfName) ignore_value(virNetDevTapDelete(macTapIfName, NULL)); - VIR_FREE(macTapIfName); - } } =20 ignore_value(virNetDevSetOnline(def->bridge, false)); @@ -2960,7 +2907,7 @@ networkShutdownNetwork(virNetworkDriverStatePtr drive= r, { virNetworkDefPtr def =3D virNetworkObjGetDef(obj); int ret =3D 0; - char *stateFile; + g_autofree char *stateFile =3D NULL; =20 VIR_INFO("Shutting down network '%s'", def->name); =20 @@ -2972,7 +2919,6 @@ networkShutdownNetwork(virNetworkDriverStatePtr drive= r, return -1; =20 unlink(stateFile); - VIR_FREE(stateFile); =20 switch ((virNetworkForwardType) def->forward.type) { =20 @@ -3245,7 +3191,6 @@ networkFindUnusedBridgeName(virNetworkObjListPtr nets, virNetworkDefPtr def) { int ret =3D -1, id =3D 0; - char *newname =3D NULL; const char *templ =3D "virbr%d"; const char *p; =20 @@ -3255,7 +3200,8 @@ networkFindUnusedBridgeName(virNetworkObjListPtr nets, templ =3D def->bridge; =20 do { - newname =3D g_strdup_printf(templ, id); + g_autofree char *newname =3D g_strdup_printf(templ, id); + /* check if this name is used in another libvirt network or * there is an existing device with that name. ignore errors * from virNetDevExists(), just in case it isn't implemented @@ -3264,11 +3210,10 @@ networkFindUnusedBridgeName(virNetworkObjListPtr ne= ts, if (!(virNetworkObjBridgeInUse(nets, newname, def->name) || virNetDevExists(newname) =3D=3D 1)) { VIR_FREE(def->bridge); /*could contain template */ - def->bridge =3D newname; + def->bridge =3D g_steal_pointer(&newname); ret =3D 0; goto cleanup; } - VIR_FREE(newname); } while (++id <=3D MAX_BRIDGE_ID); =20 virReportError(VIR_ERR_INTERNAL_ERROR, @@ -3276,8 +3221,6 @@ networkFindUnusedBridgeName(virNetworkObjListPtr nets, MAX_BRIDGE_ID); ret =3D 0; cleanup: - if (ret < 0) - VIR_FREE(newname); return ret; } =20 @@ -3421,7 +3364,7 @@ networkValidate(virNetworkDriverStatePtr driver, */ for (i =3D 0; i < def->forward.nifs; i++) { virNetworkForwardIfDefPtr iface =3D &def->forward.ifs[i]; - char *sysfs_path =3D NULL; + g_autofree char *sysfs_path =3D NULL; =20 switch ((virNetworkForwardHostdevDeviceType)iface->type) { case VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_NETDEV: @@ -3462,10 +3405,8 @@ networkValidate(virNetworkDriverStatePtr driver, _("device '%s' in network '%s' is not " "an SR-IOV Virtual Function"), sysfs_path, def->name); - VIR_FREE(sysfs_path); return -1; } - VIR_FREE(sysfs_path); break; } =20 @@ -4141,7 +4082,8 @@ networkSetAutostart(virNetworkPtr net, virNetworkDriverStatePtr driver =3D networkGetDriver(); virNetworkObjPtr obj; virNetworkDefPtr def; - char *configFile =3D NULL, *autostartLink =3D NULL; + g_autofree char *configFile =3D NULL; + g_autofree char *autostartLink =3D NULL; bool new_autostart; bool cur_autostart; int ret =3D -1; @@ -4198,8 +4140,6 @@ networkSetAutostart(virNetworkPtr net, ret =3D 0; =20 cleanup: - VIR_FREE(configFile); - VIR_FREE(autostartLink); virNetworkObjEndAPI(&obj); return ret; } @@ -4221,14 +4161,13 @@ networkGetDHCPLeases(virNetworkPtr net, long long currtime =3D 0; long long expirytime_tmp =3D -1; bool ipv6 =3D false; - char *lease_entries =3D NULL; - char *custom_lease_file =3D NULL; + 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; - virJSONValuePtr leases_array =3D NULL; + g_autoptr(virJSONValue) leases_array =3D NULL; virNetworkIPDefPtr ipdef_tmp =3D NULL; - virNetworkDHCPLeasePtr lease =3D NULL; virNetworkDHCPLeasePtr *leases_ret =3D NULL; virNetworkObjPtr obj; virNetworkDefPtr def; @@ -4317,7 +4256,7 @@ networkGetDHCPLeases(virNetworkPtr net, continue; =20 if (need_results) { - lease =3D g_new0(virNetworkDHCPLease, 1); + g_autoptr(virNetworkDHCPLease) lease =3D g_new0(virNetworkDHCP= Lease, 1); =20 lease->expirytime =3D expirytime_tmp; =20 @@ -4365,8 +4304,6 @@ networkGetDHCPLeases(virNetworkPtr net, } else { nleases++; } - - VIR_FREE(lease); } =20 if (leases_ret) { @@ -4378,13 +4315,7 @@ networkGetDHCPLeases(virNetworkPtr net, rv =3D nleases; =20 cleanup: - VIR_FREE(lease); - VIR_FREE(lease_entries); - VIR_FREE(custom_lease_file); - virJSONValueFree(leases_array); - virNetworkObjEndAPI(&obj); - return rv; =20 error: @@ -5577,7 +5508,7 @@ networkPortSetParameters(virNetworkPortPtr port, virNetworkDefPtr def; virNetworkPortDefPtr portdef; virNetDevBandwidthPtr bandwidth =3D NULL; - char *dir =3D NULL; + g_autofree char *dir =3D NULL; int ret =3D -1; size_t i; =20 @@ -5654,7 +5585,6 @@ networkPortSetParameters(virNetworkPortPtr port, cleanup: virNetDevBandwidthFree(bandwidth); virNetworkObjEndAPI(&obj); - VIR_FREE(dir); return ret; } =20 diff --git a/src/network/bridge_driver_linux.c b/src/network/bridge_driver_= linux.c index 5fc77785dc..58df15b5cf 100644 --- a/src/network/bridge_driver_linux.c +++ b/src/network/bridge_driver_linux.c @@ -218,7 +218,8 @@ void networkPostReloadFirewallRules(bool startup G_GNUC= _UNUSED) int networkCheckRouteCollision(virNetworkDefPtr def) { int ret =3D 0, len; - char *cur, *buf =3D NULL; + char *cur; + g_autofree char *buf =3D NULL; /* allow for up to 100000 routes (each line is 128 bytes) */ enum {MAX_ROUTE_SIZE =3D 128*100000}; =20 @@ -315,14 +316,13 @@ int networkCheckRouteCollision(virNetworkDefPtr def) =20 if ((r_addr.data.inet4.sin_addr.s_addr =3D=3D addr_val) && (r_mask.data.inet4.sin_addr.s_addr =3D=3D mask_val)) { - char *addr_str =3D virSocketAddrFormat(&r_addr); + g_autofree char *addr_str =3D virSocketAddrFormat(&r_addr); if (!addr_str) virResetLastError(); virReportError(VIR_ERR_INTERNAL_ERROR, _("Route address '%s' conflicts " "with IP address for '%s'"), NULLSTR(addr_str), iface); - VIR_FREE(addr_str); ret =3D -1; goto out; } @@ -330,7 +330,6 @@ int networkCheckRouteCollision(virNetworkDefPtr def) } =20 out: - VIR_FREE(buf); return ret; } =20 diff --git a/src/network/leaseshelper.c b/src/network/leaseshelper.c index 2b5fc0f442..732dd09610 100644 --- a/src/network/leaseshelper.c +++ b/src/network/leaseshelper.c @@ -82,8 +82,8 @@ VIR_ENUM_IMPL(virLeaseAction, int main(int argc, char **argv) { - char *pid_file =3D NULL; - char *custom_lease_file =3D NULL; + g_autofree char *pid_file =3D NULL; + g_autofree char *custom_lease_file =3D NULL; const char *ip =3D NULL; const char *mac =3D NULL; const char *leases_str =3D NULL; @@ -91,13 +91,13 @@ main(int argc, char **argv) const char *clientid =3D getenv("DNSMASQ_CLIENT_ID"); const char *interface =3D getenv("DNSMASQ_INTERFACE"); const char *hostname =3D getenv("DNSMASQ_SUPPLIED_HOSTNAME"); - char *server_duid =3D NULL; + g_autofree char *server_duid =3D NULL; int action =3D -1; int pid_file_fd =3D -1; int rv =3D EXIT_FAILURE; bool delete =3D false; - virJSONValuePtr lease_new =3D NULL; - virJSONValuePtr leases_array_new =3D NULL; + g_autoptr(virJSONValue) lease_new =3D NULL; + g_autoptr(virJSONValue) leases_array_new =3D NULL; =20 virSetErrorFunc(NULL, NULL); virSetErrorLogPriorityFunc(NULL); @@ -256,11 +256,5 @@ main(int argc, char **argv) if (pid_file_fd !=3D -1) virPidFileReleasePath(pid_file, pid_file_fd); =20 - VIR_FREE(pid_file); - VIR_FREE(server_duid); - VIR_FREE(custom_lease_file); - virJSONValueFree(lease_new); - virJSONValueFree(leases_array_new); - return rv; } --=20 2.25.4