From nobody Wed Apr 24 09:10:02 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.37 as permitted sender) client-ip=209.132.183.37; envelope-from=libvir-list-bounces@redhat.com; helo=mx5-phx2.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.37 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx5-phx2.redhat.com (mx5-phx2.redhat.com [209.132.183.37]) by mx.zohomail.com with SMTPS id 1487076598246123.44378433957911; Tue, 14 Feb 2017 04:49:58 -0800 (PST) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx5-phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1ECkgVk047640; Tue, 14 Feb 2017 07:46:42 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v1ECkfs4004161 for ; Tue, 14 Feb 2017 07:46:41 -0500 Received: from dnr.brq.redhat.com (dhcp129-180.brq.redhat.com [10.34.129.180]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1ECke8D008128 for ; Tue, 14 Feb 2017 07:46:41 -0500 From: =?UTF-8?q?J=C3=A1n=20Tomko?= To: libvir-list@redhat.com Date: Tue, 14 Feb 2017 13:46:36 +0100 Message-Id: <4a41cf18b1356118e64c431ab551f63e12d3fb64.1487076349.git.jtomko@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCHv2] util: fix off-by-one when expanding a bitmap 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" To make sure bit 'b' fits into the bitmap, we need to allocate b+1 bits, since we number from 0. Adjust the bitmap test to set a bit at a multiple of 16. That way the test fails without this fix, because the VIR_REALLOC call clears the newly added memory even if the original pointer has not changed. --- src/util/virbitmap.c | 2 +- tests/virbitmaptest.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) v2: use a multiple of 16 instead of just 8 to actually reproduce the failure in the test. diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c index 0c04f1a..1b47d74 100644 --- a/src/util/virbitmap.c +++ b/src/util/virbitmap.c @@ -187,7 +187,7 @@ int virBitmapSetBit(virBitmapPtr bitmap, size_t b) */ static int virBitmapExpand(virBitmapPtr map, size_t b) { - size_t new_len =3D VIR_DIV_UP(b, VIR_BITMAP_BITS_PER_UNIT); + size_t new_len =3D VIR_DIV_UP(b + 1, VIR_BITMAP_BITS_PER_UNIT); =20 /* resize the memory if necessary */ if (map->map_len < new_len) { diff --git a/tests/virbitmaptest.c b/tests/virbitmaptest.c index a17ef82..3ee07ff 100644 --- a/tests/virbitmaptest.c +++ b/tests/virbitmaptest.c @@ -639,15 +639,15 @@ test12(const void *opaque ATTRIBUTE_UNUSED) =20 TEST_MAP(0, ""); =20 - if (virBitmapSetBitExpand(map, 100) < 0) + if (virBitmapSetBitExpand(map, 128) < 0) goto cleanup; =20 - TEST_MAP(101, "100"); + TEST_MAP(129, "128"); =20 if (virBitmapClearBitExpand(map, 150) < 0) goto cleanup; =20 - TEST_MAP(151, "100"); + TEST_MAP(151, "128"); =20 virBitmapFree(map); if (virBitmapParseUnlimited("34,1023", &map) < 0) --=20 2.10.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list