[libvirt] [PATCHv2] util: fix off-by-one when expanding a bitmap

Ján Tomko posted 1 patch 7 years, 2 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/4a41cf18b1356118e64c431ab551f63e12d3fb64.1487076349.git.jtomko@redhat.com
src/util/virbitmap.c  | 2 +-
tests/virbitmaptest.c | 6 +++---
2 files changed, 4 insertions(+), 4 deletions(-)
[libvirt] [PATCHv2] util: fix off-by-one when expanding a bitmap
Posted by Ján Tomko 7 years, 2 months ago
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 = VIR_DIV_UP(b, VIR_BITMAP_BITS_PER_UNIT);
+    size_t new_len = VIR_DIV_UP(b + 1, VIR_BITMAP_BITS_PER_UNIT);
 
     /* 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)
 
     TEST_MAP(0, "");
 
-    if (virBitmapSetBitExpand(map, 100) < 0)
+    if (virBitmapSetBitExpand(map, 128) < 0)
         goto cleanup;
 
-    TEST_MAP(101, "100");
+    TEST_MAP(129, "128");
 
     if (virBitmapClearBitExpand(map, 150) < 0)
         goto cleanup;
 
-    TEST_MAP(151, "100");
+    TEST_MAP(151, "128");
 
     virBitmapFree(map);
     if (virBitmapParseUnlimited("34,1023", &map) < 0)
-- 
2.10.2

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCHv2] util: fix off-by-one when expanding a bitmap
Posted by Michal Privoznik 7 years, 2 months ago
On 02/14/2017 01:46 PM, Ján Tomko wrote:
> 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(-)

ACK

Michal

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list