From nobody Thu Jan 2 16:54:08 2025 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 1729166982884321.55846352963; Thu, 17 Oct 2024 05:09:42 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id AB523144C; Thu, 17 Oct 2024 08:09:41 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 250581285; Thu, 17 Oct 2024 08:09:25 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id AA50A1276; Thu, 17 Oct 2024 08:09:21 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 3238D1215 for ; Thu, 17 Oct 2024 08:09:19 -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-183-4P3NYjW1NiqK2pRiaL5pcw-1; Thu, 17 Oct 2024 08:09:17 -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 69EB81955F40 for ; Thu, 17 Oct 2024 12:09:16 +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 990D61955F45 for ; Thu, 17 Oct 2024 12:09:15 +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=1729166958; 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=aaqUvEGahrlU4O5mTCMTlWja6y6l+0qinNBF9IHFVXo=; b=DTKbTYydG40i2z40He8kAgqXBcO/4tU0eF1Qy4P/slVws2UafWzXQ/skujnNcjE7W26mMd AnYgyiW1drW4XFrA135Qm64ixGUlqijKYobsRoYMsqemTf7VlJNFIJx/KB1E9a6cGJOJtZ plBgpg/9S6CGq9by5tqT8bSKwAjuxWQ= X-MC-Unique: 4P3NYjW1NiqK2pRiaL5pcw-1 From: Peter Krempa To: devel@lists.libvirt.org Subject: [PATCH 1/4] virBitmapNewCopy: Honor sizes of either bitmap when doing memcpy() Date: Thu, 17 Oct 2024 14:09:09 +0200 Message-ID: <7212293c8ecceacad239952a52860f74f7d0568c.1729166910.git.pkrempa@redhat.com> 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: OZQGRDUBPZXEW3GAFOKNUMPVPAFBK4CR X-Message-ID-Hash: OZQGRDUBPZXEW3GAFOKNUMPVPAFBK4CR 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: 1729166983482116600 Content-Type: text/plain; charset="utf-8" 'virBitmapNewCopy()' allocates a new bitmap with the same number of bits but uses the internal allocation length as argument for the memcpy() operation to copy the bits. Due to bugs in other code these may not be the same resulting into a buffer overflow if the source is over-allocated. Use the buffer length of the target bitmap instead. Signed-off-by: Peter Krempa Reviewed-by: Jiri Denemark --- src/util/virbitmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c index b8d0352bb1..a1a8c5d126 100644 --- a/src/util/virbitmap.c +++ b/src/util/virbitmap.c @@ -582,7 +582,7 @@ virBitmapNewCopy(virBitmap *src) { virBitmap *dst =3D virBitmapNew(src->nbits); - memcpy(dst->map, src->map, src->map_len * sizeof(src->map[0])); + memcpy(dst->map, src->map, dst->map_len * sizeof(src->map[0])); return dst; } --=20 2.47.0 From nobody Thu Jan 2 16:54:08 2025 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 1729167020051513.2000743727964; Thu, 17 Oct 2024 05:10:20 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 87ED212B1; Thu, 17 Oct 2024 08:10:17 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 7B1811283; Thu, 17 Oct 2024 08:09:29 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 230271212; Thu, 17 Oct 2024 08:09:22 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 5215F121A for ; Thu, 17 Oct 2024 08:09:20 -0400 (EDT) Received: from mx-prod-mc-02.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-563-kkeQiDbGN7-I2_EIb5DXDA-1; Thu, 17 Oct 2024 08:09:18 -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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BE2F21955EA7 for ; Thu, 17 Oct 2024 12:09:17 +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 EE043195607C for ; Thu, 17 Oct 2024 12:09:16 +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=1729166959; 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=Koz4tLAnrB+3HHR0N4Q3Uxpxv5Mq+uiLMeeKxAPbG74=; b=iMJ5qYJ4XCEter9YVP9a/nknAXiGT6uQ9qnbycYAvo8pPokN0zUYaT4cgKknjnfBKJ0GOW udEGQxdkrxek56TuimkgvBvkJhFG6m4oWKekD5H6Gsgt7BhIeMZwVOy8hLmX/VgOpvvUNW 8JYPrHsI1xoU6kTJjVx6ThA9yQjIF3A= X-MC-Unique: kkeQiDbGN7-I2_EIb5DXDA-1 From: Peter Krempa To: devel@lists.libvirt.org Subject: [PATCH 2/4] virbitmap: Extract and reuse buffer size calculation into a function Date: Thu, 17 Oct 2024 14:09:10 +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: AASYGDT522FG26GA3CDK3AQHIEE5LUK2 X-Message-ID-Hash: AASYGDT522FG26GA3CDK3AQHIEE5LUK2 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: 1729167021488116600 Content-Type: text/plain; charset="utf-8" Calculating the number of element can come handy in multiple places, extract it from virBitmapNew. Signed-off-by: Peter Krempa Reviewed-by: Jiri Denemark --- src/util/virbitmap.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c index a1a8c5d126..7dc63da6db 100644 --- a/src/util/virbitmap.c +++ b/src/util/virbitmap.c @@ -49,6 +49,22 @@ struct _virBitmap { #define VIR_BITMAP_BIT(b) (1UL << VIR_BITMAP_BIT_OFFSET(b)) +/** + * Calculates and returns the number of elements in the bitmap buffer to f= it @bits. + */ +static size_t +virBitmapBuffsize(size_t nbits) +{ + if (SIZE_MAX - VIR_BITMAP_BITS_PER_UNIT < nbits) { + /* VIR_DIV_UP would overflow, let's overallocate by 1 entry instea= d of + * the potential overflow */ + return (nbits / VIR_BITMAP_BITS_PER_UNIT) + 1; + } + + return VIR_DIV_UP(nbits, VIR_BITMAP_BITS_PER_UNIT); +} + + /** * virBitmapNew: * @size: number of bits @@ -61,15 +77,7 @@ virBitmap * virBitmapNew(size_t size) { virBitmap *bitmap; - size_t sz; - - if (SIZE_MAX - VIR_BITMAP_BITS_PER_UNIT < size) { - /* VIR_DIV_UP would overflow, let's overallocate by 1 entry instea= d of - * the potential overflow */ - sz =3D (size / VIR_BITMAP_BITS_PER_UNIT) + 1; - } else { - sz =3D VIR_DIV_UP(size, VIR_BITMAP_BITS_PER_UNIT); - } + size_t sz =3D virBitmapBuffsize(size); bitmap =3D g_new0(virBitmap, 1); @@ -133,7 +141,7 @@ static void virBitmapExpand(virBitmap *map, size_t b) { - size_t new_len =3D VIR_DIV_UP(b + 1, VIR_BITMAP_BITS_PER_UNIT); + size_t new_len =3D virBitmapBuffsize(b + 1); /* resize the memory if necessary */ if (map->map_len < new_len) { --=20 2.47.0 From nobody Thu Jan 2 16:54:08 2025 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 1729167056514506.0269358368337; Thu, 17 Oct 2024 05:10:56 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 659AC1201; Thu, 17 Oct 2024 08:10:40 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 9ED0D1352; Thu, 17 Oct 2024 08:09:31 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 26EB71201; Thu, 17 Oct 2024 08:09:22 -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 889481218 for ; Thu, 17 Oct 2024 08:09:21 -0400 (EDT) Received: from mx-prod-mc-03.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-612-HFK0xM4OOl2VgKfjAwtB8w-1; Thu, 17 Oct 2024 08:09:19 -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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 144511955F41 for ; Thu, 17 Oct 2024 12:09:19 +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 4629E1955F43 for ; Thu, 17 Oct 2024 12:09:17 +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=1729166961; 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=LjGjKR+o+1wF5aupDudKXXUpr85CPrh4nUAz/iYKc7E=; b=fxkQY9w6k27YJWYFFPaGi8+poY3n/p1HdaK36IRy5M1y6z3OR07lBIiQ8eqlDQePU2t02H z/21MPaE8g+4V+6EJM5IdZCsDz8L/es034sfJfFQRRW22jwLDR/YsDieprVQFblQcgeIAJ lL6lVBpQDyxoLMzaWALgpG10ZQ6A4EY= X-MC-Unique: HFK0xM4OOl2VgKfjAwtB8w-1 From: Peter Krempa To: devel@lists.libvirt.org Subject: [PATCH 3/4] util: virbitmap: Extract clearing of unused bits at the end of the last unit Date: Thu, 17 Oct 2024 14:09:11 +0200 Message-ID: <5c9d2d797b271a3b13a15812286dbf6852bd9cb5.1729166910.git.pkrempa@redhat.com> 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: N7DC6CNKQ6DUUHBKIBT5CYXNRAQXHYB6 X-Message-ID-Hash: N7DC6CNKQ6DUUHBKIBT5CYXNRAQXHYB6 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: 1729167058126116600 Content-Type: text/plain; charset="utf-8" Extract the clearing of the traling bits from 'virBitmapSetAll' into a new helper. Signed-off-by: Peter Krempa Reviewed-by: Jiri Denemark --- src/util/virbitmap.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c index 7dc63da6db..35cf729a22 100644 --- a/src/util/virbitmap.c +++ b/src/util/virbitmap.c @@ -757,6 +757,19 @@ virBitmapSize(virBitmap *bitmap) } +/** + * Internal helper that clears the unused bits at the end of the last bitm= ap unit. + */ +static void +virBitmapClearTail(virBitmap *bitmap) +{ + size_t tail =3D bitmap->nbits % VIR_BITMAP_BITS_PER_UNIT; + + if (tail) + bitmap->map[bitmap->map_len - 1] &=3D -1UL >> (VIR_BITMAP_BITS_PER= _UNIT - tail); +} + + /** * virBitmapSetAll: * @bitmap: the bitmap @@ -765,15 +778,10 @@ virBitmapSize(virBitmap *bitmap) */ void virBitmapSetAll(virBitmap *bitmap) { - int tail =3D bitmap->nbits % VIR_BITMAP_BITS_PER_UNIT; - memset(bitmap->map, 0xff, bitmap->map_len * (VIR_BITMAP_BITS_PER_UNIT / CHAR_BIT)); - /* Ensure tail bits are clear. */ - if (tail) - bitmap->map[bitmap->map_len - 1] &=3D - -1UL >> (VIR_BITMAP_BITS_PER_UNIT - tail); + virBitmapClearTail(bitmap); } --=20 2.47.0 From nobody Thu Jan 2 16:54:08 2025 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 Reviewed-by: Jiri Denemark --- 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