From nobody Sun Feb 8 07:22:25 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=1635873075; cv=none; d=zohomail.com; s=zohoarc; b=kNZpDi9JrJ4CHaTltjk+xqlRIsOPry6xh/NwDMie5wHCzveibckI3WVsnEcAj2HmTSHgGZ/RvWpWh7dqjiiIc2BXiOsYfQC47y3bxSHeG8q70quIlsGh4D8KGcooMVn2edfU9HEcwKf/I8ZnGn4Sn3xQDhFODSGMyqs8M0hEF+Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1635873075; 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=AHZ7XXAWcS2E8TZobZBNux8yD4VtBIJC90QUVTP8M9w=; b=mGbepqCR4N8Nf7jH2YiWRZ7Q8/1KR0KK2Gx0Hk2dTp4lQ306JuqyriMPiaq6CWn7CvXYzDR1AHfsHN77pxfp8MMC+FUacvnedkdA2o44DxzozDW4OOosENwIoT1izJ6qQ3mG0/gtmkdN4Ytq5o7JdB7SZsAFe/A1Nfodj1tfbIs= 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 1635873075101335.6606291364974; Tue, 2 Nov 2021 10:11:15 -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-443-CRjZ8FN-OkqNsetkOFRqUg-1; Tue, 02 Nov 2021 13:11:12 -0400 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 547721006AA2; Tue, 2 Nov 2021 17:11:00 +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 39FBB101E591; Tue, 2 Nov 2021 17:11:00 +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 0B2B7180BAD1; Tue, 2 Nov 2021 17:11:00 +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 1A2HAlKO009459 for ; Tue, 2 Nov 2021 13:10:48 -0400 Received: by smtp.corp.redhat.com (Postfix) id CFEF67D552; 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 202AE7DDCE 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=1635873074; 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=AHZ7XXAWcS2E8TZobZBNux8yD4VtBIJC90QUVTP8M9w=; b=Jl6MuJBTgCQXU2pFaCu8dE91g7CEBthHrnpz0Fb3nZNv9vFfz86kMPz9tXAOAea/NPS7yv iywho/FLkd0bciGcvNMDpCt+rwtytBvESc6l+XefQQK+3uiOIwJecSish4wUjeIJX/bsGa nH4lsYgWnPBg/38G7rffJ5oc/t7IV/s= X-MC-Unique: CRjZ8FN-OkqNsetkOFRqUg-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 2/5] virpcivpd: Bring variables into loops Date: Tue, 2 Nov 2021 18:10:39 +0100 Message-Id: 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.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) X-ZM-MESSAGEID: 1635873978042100001 Content-Type: text/plain; charset="utf-8" I've noticed one function inside virpcivpd.c, namely virPCIVPDParseVPDLargeResourceFields() that declares some variables at the top level even though they are used only inside a loop in which they have to be freed explicitly. Bringing variable declarations into the loop allows us to make the code nicer. Signed-off-by: Michal Privoznik --- src/util/virpcivpd.c | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/src/util/virpcivpd.c b/src/util/virpcivpd.c index d8f2a43cde..9af0566d19 100644 --- a/src/util/virpcivpd.c +++ b/src/util/virpcivpd.c @@ -456,10 +456,6 @@ bool virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, uint16_t resPos, uint1= 6_t resDataLen, bool readOnly, uint8_t *csum, virPCIV= PDResource *res) { - g_autofree char *fieldKeyword =3D NULL; - g_autofree char *fieldValue =3D NULL; - virPCIVPDResourceFieldValueFormat fieldFormat =3D VIR_PCI_VPD_RESOURCE= _FIELD_VALUE_FORMAT_LAST; - /* A buffer of up to one resource record field size (plus a zero byte)= is needed. */ g_autofree uint8_t *buf =3D g_malloc0(PCI_VPD_MAX_FIELD_SIZE + 1); uint16_t fieldDataLen =3D 0, bytesToRead =3D 0; @@ -473,6 +469,10 @@ virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, ui= nt16_t resPos, uint16_t re * just occupy 3 header bytes. In the in case of the RW field this may= mean that * no more space is left in the section. */ while (fieldPos + 3 <=3D resPos + resDataLen) { + virPCIVPDResourceFieldValueFormat fieldFormat =3D VIR_PCI_VPD_RESO= URCE_FIELD_VALUE_FORMAT_LAST; + g_autofree char *fieldKeyword =3D NULL; + g_autofree char *fieldValue =3D NULL; + /* Keyword resources consist of keywords (2 ASCII bytes per the sp= ec) and 1-byte length. */ if (virPCIVPDReadVPDBytes(vpdFileFd, buf, 3, fieldPos, csum) !=3D = 3) { /* Invalid field encountered which means the resource itself i= s invalid too. Report @@ -548,8 +548,6 @@ virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, uin= t16_t resPos, uint16_t re /* Skip fields with invalid values - this is safe assuming= field length is * correctly specified. */ VIR_DEBUG("A value for field %s contains invalid character= s", fieldKeyword); - g_free(g_steal_pointer(&fieldKeyword)); - g_free(g_steal_pointer(&fieldValue)); continue; } } else if (fieldFormat =3D=3D VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FOR= MAT_RESVD) { @@ -559,19 +557,13 @@ virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, u= int16_t resPos, uint16_t re return false; } hasChecksum =3D true; - g_free(g_steal_pointer(&fieldKeyword)); - g_free(g_steal_pointer(&fieldValue)); break; } else if (fieldFormat =3D=3D VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FOR= MAT_RDWR) { /* Skip the read-write space since it is used for indication o= nly. */ hasRW =3D true; - g_free(g_steal_pointer(&fieldKeyword)); - g_free(g_steal_pointer(&fieldValue)); break; } else if (fieldFormat =3D=3D VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FOR= MAT_LAST) { /* Skip unknown fields */ - g_free(g_steal_pointer(&fieldKeyword)); - g_free(g_steal_pointer(&fieldValue)); continue; } else { fieldValue =3D g_malloc(fieldDataLen); @@ -591,9 +583,6 @@ virPCIVPDParseVPDLargeResourceFields(int vpdFileFd, uin= t16_t resPos, uint16_t re _("Could not update the VPD resource keyword: %= s"), fieldKeyword); return false; } - /* No longer need those since copies were made during the keyword = update. */ - g_free(g_steal_pointer(&fieldKeyword)); - g_free(g_steal_pointer(&fieldValue)); } =20 /* May have exited the loop prematurely in case RV or RW were encounte= red and --=20 2.32.0