From nobody Mon Feb 9 10:40:19 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1729167062666919.751477828502; Thu, 17 Oct 2024 05:11:02 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 784621291; Thu, 17 Oct 2024 08:11:01 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 3DB6D1341; Thu, 17 Oct 2024 08:09:32 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 395D81212; Thu, 17 Oct 2024 08:09:23 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id B71EE1201 for ; Thu, 17 Oct 2024 08:09:22 -0400 (EDT) Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-646-JFyKjCG4Mz20o0H46ooDKQ-1; Thu, 17 Oct 2024 08:09:21 -0400 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 574D91956089 for ; Thu, 17 Oct 2024 12:09:20 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.45.242.4]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 889951956086 for ; Thu, 17 Oct 2024 12:09:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: *** X-Spam-Status: No, score=3.0 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,RCVD_IN_SBL_CSS, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729166962; h=from:from: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; bh=l1NjLQkgoR8F5Yyy5e+tlcNJGy3mT6fWKgocLXC4yIM=; b=W4UufmEBdbuzLs0NcTccLTrQfKXxjTEO9+X0JmyJ2k7DschxAjRvKK66OLBCOMXJBXzxOt cWqi7VxYmGanEnRYKCdsY7az2e/PN8u5hXuN09Rmu45l2M2RRf3NV3jhZgqJZW0JujPH3A TN4HyLsTMhRJrO+VqirHwxz7bEmk5N8= X-MC-Unique: JFyKjCG4Mz20o0H46ooDKQ-1 From: Peter Krempa To: devel@lists.libvirt.org Subject: [PATCH 4/4] util: bitmap: Rewrite virBitmapShrink using new helpers Date: Thu, 17 Oct 2024 14:09:12 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: ADJAHU734D7262YUNGATO2EW4MTD3ECY X-Message-ID-Hash: ADJAHU734D7262YUNGATO2EW4MTD3ECY X-MailFrom: pkrempa@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1729167064033116600 Content-Type: text/plain; charset="utf-8" Rather than reimplement everything manually use virBitmapBuffsize to find the current number of units, realloc the buffer and clear the tail using virBitmapClearTail(). This fixes a corner case where the buffer would be over-allocated by one unit when shrinking to the boundary of the unit size. Signed-off-by: Peter Krempa --- src/util/virbitmap.c | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c index 35cf729a22..138c1ac5af 100644 --- a/src/util/virbitmap.c +++ b/src/util/virbitmap.c @@ -1187,33 +1187,13 @@ void virBitmapShrink(virBitmap *map, size_t b) { - size_t toremove; - size_t nl =3D 0; - size_t nb =3D 0; - - if (!map) - return; - - if (map->nbits >=3D b) - map->nbits =3D b; - - nl =3D map->nbits / VIR_BITMAP_BITS_PER_UNIT; - nb =3D map->nbits % VIR_BITMAP_BITS_PER_UNIT; - - /* If we're at the end of the allocation the attempt to clear 'map->nb= it' - * and further would be beyond the end of the bitmap */ - if (nl >=3D map->map_alloc) + if (!map || + map->nbits <=3D b) return; - map->map[nl] &=3D ((1UL << nb) - 1); - - toremove =3D map->map_alloc - (nl + 1); - - if (toremove =3D=3D 0) - return; - - VIR_SHRINK_N(map->map, map->map_alloc, toremove); - - /* length needs to be fixed as well */ - map->map_len =3D map->map_alloc; + map->nbits =3D b; + map->map_len =3D virBitmapBuffsize(b); + map->map =3D g_renew(unsigned long, map->map, map->map_len); + map->map_alloc =3D map->map_len; + virBitmapClearTail(map); } --=20 2.47.0