From nobody Sun Feb 8 18:24:38 2026 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=1635873070; cv=none; d=zohomail.com; s=zohoarc; b=NQ0cDFSM0Va58sSyz1nYmy6r227FlvxN1zbKYuf1l8HaC+2alzwhjRJ/5ujgX6cgd1fQzZMC6SKCnWS61fCeYoGD9obpOHupBZslYjMZGt0REa9pD4wXBQPsZufQqhiY8J8r8GyjbUcvbSl3R06qYtqt98x1bG/r1rDm3LreBA8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1635873070; 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=bhXlERxxTLcpdpDAbprSjSXleuPHdIxxV1fPBi1MU+o=; b=HMWazaut7dlUwUMjDmOAtCQ2FM0bVBy2eG9+T+j/3drQSAs6raxStufBqggmDcN/ql6OwlbbyffCHYyvL0wp53khGBu/VWNHQIPKCVl8beriATC97LAz3NJlB4bQmU5WQCp7kPhLDgvIT372P7x+2aE0RnTMV8vh0mW+MxvebyA= 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) 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 1635873070890606.2544873718568; Tue, 2 Nov 2021 10:11:10 -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-295-3zdDAC8HNuq7Q7Bo5a8rSg-1; Tue, 02 Nov 2021 13:11:06 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0DDB610B7465; Tue, 2 Nov 2021 17:10: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 02F0E5D6B1; Tue, 2 Nov 2021 17:10:50 +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 96812180BAD1; Tue, 2 Nov 2021 17:10:50 +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 1A2HAlhj009452 for ; Tue, 2 Nov 2021 13:10:47 -0400 Received: by smtp.corp.redhat.com (Postfix) id 0934F7D492; Tue, 2 Nov 2021 17:10:47 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.27]) by smtp.corp.redhat.com (Postfix) with ESMTP id 36F4B7E5C3 for ; Tue, 2 Nov 2021 17:10:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635873069; 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=bhXlERxxTLcpdpDAbprSjSXleuPHdIxxV1fPBi1MU+o=; b=ElMgLVBfFZflTXq68dvPwuDuDnLK9+yj4i0c+L2h6jXjdGUbHpM1bvQuRx4yzsnG4gE3p1 Cor6gJJdchRHK7nznDg9Jn56IEAO2yYPW2PvRRItOuSrP/sqAebXyZry76vpaFchHZe3Kg Wm7ZVB5U4A5J945aUwPUBmxaHBrGbnk= X-MC-Unique: 3zdDAC8HNuq7Q7Bo5a8rSg-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 1/5] node_device_conf: Bring variables into loops Date: Tue, 2 Nov 2021 18:10:38 +0100 Message-Id: <1330d98604757e7de38d7a4626eb26345aed9656.1635872947.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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.15 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) X-ZM-MESSAGEID: 1635873073053100001 Content-Type: text/plain; charset="utf-8" I've noticed three functions inside node_device_conf.c, namely: - virNodeDeviceCapVPDParseCustomFields() - virNodeDeviceCapVPDParseReadOnlyFields() - virNodeDeviceCapVPDParseXML() that have strange attitude towards g_auto* variables. The first problem is that variables are declared at the top level despite being used inside a loop. The second problem is use of g_free() in combination with g_steal_pointer() even though we have VIR_FREE() which does exactly that. Bringing variable declarations into their respective loops allows us to make the code nicer. Signed-off-by: Michal Privoznik --- src/conf/node_device_conf.c | 46 ++++++++++++++----------------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/src/conf/node_device_conf.c b/src/conf/node_device_conf.c index e958367572..ca534dfbed 100644 --- a/src/conf/node_device_conf.c +++ b/src/conf/node_device_conf.c @@ -940,19 +940,20 @@ static int virNodeDeviceCapVPDParseCustomFields(xmlXPathContextPtr ctxt, virPCIVPDRes= ource *res, bool readOnly) { int nfields =3D -1; - g_autofree char *index =3D NULL, *value =3D NULL, *keyword =3D NULL; g_autofree xmlNodePtr *nodes =3D NULL; - xmlNodePtr orignode =3D NULL; size_t i =3D 0; =20 - orignode =3D ctxt->node; if ((nfields =3D virXPathNodeSet("./vendor_field[@index]", ctxt, &node= s)) < 0) { virReportError(VIR_ERR_XML_ERROR, "%s", _("failed to evaluate elements")); - ctxt->node =3D orignode; return -1; } for (i =3D 0; i < nfields; i++) { + g_autofree char *value =3D NULL; + g_autofree char *index =3D NULL; + VIR_XPATH_NODE_AUTORESTORE(ctxt) + g_autofree char *keyword =3D NULL; + ctxt->node =3D nodes[i]; if (!(index =3D virXPathStringLimit("string(./@index[1])", 2, ctxt= ))) { virReportError(VIR_ERR_XML_ERROR, "%s", @@ -966,21 +967,21 @@ virNodeDeviceCapVPDParseCustomFields(xmlXPathContextP= tr ctxt, virPCIVPDResource } keyword =3D g_strdup_printf("V%c", index[0]); virPCIVPDResourceUpdateKeyword(res, readOnly, keyword, value); - g_free(g_steal_pointer(&index)); - g_free(g_steal_pointer(&keyword)); - g_free(g_steal_pointer(&value)); } - g_free(g_steal_pointer(&nodes)); - ctxt->node =3D orignode; + VIR_FREE(nodes); =20 if (!readOnly) { if ((nfields =3D virXPathNodeSet("./system_field[@index]", ctxt, &= nodes)) < 0) { virReportError(VIR_ERR_XML_ERROR, "%s", _("failed to evaluate elements")); - ctxt->node =3D orignode; return -1; } for (i =3D 0; i < nfields; i++) { + g_autofree char *value =3D NULL; + g_autofree char *index =3D NULL; + g_autofree char *keyword =3D NULL; + VIR_XPATH_NODE_AUTORESTORE(ctxt); + ctxt->node =3D nodes[i]; if (!(index =3D virXPathStringLimit("string(./@index[1])", 2, = ctxt))) { virReportError(VIR_ERR_XML_ERROR, "%s", @@ -994,11 +995,7 @@ virNodeDeviceCapVPDParseCustomFields(xmlXPathContextPt= r ctxt, virPCIVPDResource } keyword =3D g_strdup_printf("Y%c", index[0]); virPCIVPDResourceUpdateKeyword(res, readOnly, keyword, value); - g_free(g_steal_pointer(&index)); - g_free(g_steal_pointer(&keyword)); - g_free(g_steal_pointer(&value)); } - ctxt->node =3D orignode; } =20 return 0; @@ -1009,8 +1006,6 @@ virNodeDeviceCapVPDParseReadOnlyFields(xmlXPathContex= tPtr ctxt, virPCIVPDResourc { const char *keywords[] =3D {"change_level", "manufacture_id", "serial_number", "part_number", NULL}; - g_autofree char *expression =3D NULL; - g_autofree char *result =3D NULL; size_t i =3D 0; =20 if (res =3D=3D NULL) @@ -1019,11 +1014,10 @@ virNodeDeviceCapVPDParseReadOnlyFields(xmlXPathCont= extPtr ctxt, virPCIVPDResourc res->ro =3D virPCIVPDResourceRONew(); =20 while (keywords[i]) { - expression =3D g_strdup_printf("string(./%s)", keywords[i]); - result =3D virXPathString(expression, ctxt); + g_autofree char *expression =3D g_strdup_printf("string(./%s)", ke= ywords[i]); + g_autofree char *result =3D virXPathString(expression, ctxt); + virPCIVPDResourceUpdateKeyword(res, true, keywords[i], result); - g_free(g_steal_pointer(&expression)); - g_free(g_steal_pointer(&result)); ++i; } if (virNodeDeviceCapVPDParseCustomFields(ctxt, res, true) < 0) @@ -1047,38 +1041,34 @@ virNodeDeviceCapVPDParseReadWriteFields(xmlXPathCon= textPtr ctxt, virPCIVPDResour static int virNodeDeviceCapVPDParseXML(xmlXPathContextPtr ctxt, virPCIVPDResource **r= es) { - xmlNodePtr orignode =3D NULL; g_autofree xmlNodePtr *nodes =3D NULL; int nfields =3D -1; - g_autofree char *access =3D NULL; size_t i =3D 0; g_autoptr(virPCIVPDResource) newres =3D g_new0(virPCIVPDResource, 1); =20 if (res =3D=3D NULL) return -1; =20 - orignode =3D ctxt->node; - if (!(newres->name =3D virXPathString("string(./name)", ctxt))) { virReportError(VIR_ERR_XML_ERROR, "%s", _("Could not read a device name from the element")); - ctxt->node =3D orignode; return -1; } =20 if ((nfields =3D virXPathNodeSet("./fields[@access]", ctxt, &nodes)) <= 0) { virReportError(VIR_ERR_XML_ERROR, "%s", _("no VPD elements with an access type attribute = found")); - ctxt->node =3D orignode; return -1; } =20 for (i =3D 0; i < nfields; i++) { + g_autofree char *access =3D NULL; + VIR_XPATH_NODE_AUTORESTORE(ctxt); + ctxt->node =3D nodes[i]; if (!(access =3D virXPathString("string(./@access[1])", ctxt))) { virReportError(VIR_ERR_XML_ERROR, "%s", _("VPD fields access type parsing has failed")); - ctxt->node =3D orignode; return -1; } =20 @@ -1099,9 +1089,7 @@ virNodeDeviceCapVPDParseXML(xmlXPathContextPtr ctxt, = virPCIVPDResource **res) access); return -1; } - g_free(g_steal_pointer(&access)); } - ctxt->node =3D orignode; =20 /* Replace the existing VPD representation if there is one already. */ if (*res !=3D NULL) --=20 2.32.0