From nobody Mon Feb 9 16:34:45 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=1617285381; cv=none; d=zohomail.com; s=zohoarc; b=T/ne6PUCRYw7yOXLeO4c245Nx+mXwBCFZDdlPlwmLv4d20qr7Yr3OI5VpA/SoFF3vr9nTP7r8d2NGGRDu0jV/j98vM3oMEbsqhbCSSfbC9XIejRKCQIhuvuL7DJauDLz73EW1rNfnlrmNfS8cPQgSlCvB3+0lOzWp5/zDSzG0fY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1617285381; 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=0u/12PA83XwYeIkd0BMC0CtE23hUjmD3oagIG0QqmgE=; b=MfBV2ZBTQ5py+BMAH3dH2oLvdRNdZ/HIqDE2IQXS4yewI7g8MbaHKMoBWyG/NqK99QMnOlD+1eQDbeCQSdg7fVCBoT/fsR3trDz0MnKDpWzxLT8oaN+xb6eJZIwE3rqGO5mU+izmapKAZRu3CrtIzNyomuwdPQnpXIFnmDlL0yw= 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 1617285381992953.8996212596555; Thu, 1 Apr 2021 06:56:21 -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-369-joJFYZXqMoO-3LDIILDB0w-1; Thu, 01 Apr 2021 09:55:18 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EDE0819251BF; Thu, 1 Apr 2021 13:55:11 +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 B85A96B8F7; Thu, 1 Apr 2021 13:55:11 +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 555CE4A703; Thu, 1 Apr 2021 13:55:11 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 131DsBbn023544 for ; Thu, 1 Apr 2021 09:54:11 -0400 Received: by smtp.corp.redhat.com (Postfix) id 80B6D5C237; Thu, 1 Apr 2021 13:54:11 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.40.208.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id D90AE71C5A for ; Thu, 1 Apr 2021 13:54:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617285381; 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=0u/12PA83XwYeIkd0BMC0CtE23hUjmD3oagIG0QqmgE=; b=Bv7YzzQCXt0SYotq2MNw38EfS89Oq2ijmx4M3wWaY+mJzGa+jjGG0IEcQyu0diIUfTMeIN +QruKlg6GKiJeiv5VBUYAGUheO1Y9eI4teS64siwNa3VWcvjTy3PYpC57Ca8dmm399xQ4t l825NvcIfkXsaQfGFv+P5EkF2fXXsFw= X-MC-Unique: joJFYZXqMoO-3LDIILDB0w-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 26/39] virResctrlGetCacheInfo: Restrict variable scope and use automatic freeing Date: Thu, 1 Apr 2021 15:52:25 +0200 Message-Id: <860253e6b3154c18cf5875326a98e115df83c4e1.1617285119.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 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.16 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" Move variables into the loop which uses them and use automatic freeing for temporarily allocated variables. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- src/util/virresctrl.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c index 8458db96bc..fbc8e9c766 100644 --- a/src/util/virresctrl.c +++ b/src/util/virresctrl.c @@ -526,18 +526,19 @@ static int virResctrlGetCacheInfo(virResctrlInfoPtr resctrl, DIR *dirp) { - char *endptr =3D NULL; - char *tmp_str =3D NULL; - int ret =3D -1; int rv =3D -1; - int type =3D 0; + int ret =3D -1; struct dirent *ent =3D NULL; - unsigned int level =3D 0; - virBitmapPtr tmp_map =3D NULL; - virResctrlInfoPerLevelPtr i_level =3D NULL; - virResctrlInfoPerTypePtr i_type =3D NULL; while ((rv =3D virDirRead(dirp, &ent, SYSFS_RESCTRL_PATH "/info")) > 0= ) { + g_autofree char *cbm_mask_str =3D NULL; + g_autoptr(virBitmap) cbm_mask_map =3D NULL; + char *endptr =3D NULL; + int type =3D 0; + unsigned int level =3D 0; + virResctrlInfoPerLevelPtr i_level =3D NULL; + g_autofree virResctrlInfoPerTypePtr i_type =3D NULL; + VIR_DEBUG("Parsing info type '%s'", ent->d_name); if (ent->d_name[0] !=3D 'L') continue; @@ -570,7 +571,7 @@ virResctrlGetCacheInfo(virResctrlInfoPtr resctrl, goto cleanup; } - rv =3D virFileReadValueString(&tmp_str, + rv =3D virFileReadValueString(&cbm_mask_str, SYSFS_RESCTRL_PATH "/info/%s/cbm_mask", ent->d_name); @@ -585,19 +586,15 @@ virResctrlGetCacheInfo(virResctrlInfoPtr resctrl, if (rv < 0) goto cleanup; - virStringTrimOptionalNewline(tmp_str); + virStringTrimOptionalNewline(cbm_mask_str); - tmp_map =3D virBitmapNewString(tmp_str); - VIR_FREE(tmp_str); - if (!tmp_map) { + if (!(cbm_mask_map =3D virBitmapNewString(cbm_mask_str))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Cannot parse cbm_mask from resctrl cache inf= o")); goto cleanup; } - i_type->bits =3D virBitmapCountBits(tmp_map); - virBitmapFree(tmp_map); - tmp_map =3D NULL; + i_type->bits =3D virBitmapCountBits(cbm_mask_map); rv =3D virFileReadValueUint(&i_type->min_cbm_bits, SYSFS_RESCTRL_PATH "/info/%s/min_cbm_bit= s", @@ -635,7 +632,6 @@ virResctrlGetCacheInfo(virResctrlInfoPtr resctrl, ret =3D 0; cleanup: - VIR_FREE(i_type); return ret; } --=20 2.29.2