Some of the other functions depend on the fact that unused bits and longs are always zero and it's less error-prone to clear it than fix the other functions. Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1540817 Signed-off-by: Martin Kletzander <mkletzan@xxxxxxxxxx> --- src/util/virbitmap.c | 13 +++++++++++++ tests/virbitmaptest.c | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c index b2c5c7a6a5ac..b32342024e19 100644 --- a/src/util/virbitmap.c +++ b/src/util/virbitmap.c @@ -1213,9 +1213,22 @@ void virBitmapShrink(virBitmapPtr map, size_t b) { + size_t nl = 0; + size_t nb = 0; + if (!map) return; if (map->max_bit >= b) map->max_bit = b; + + nl = map->max_bit / VIR_BITMAP_BITS_PER_UNIT; + nb = map->max_bit % VIR_BITMAP_BITS_PER_UNIT; + map->map[nl] &= ((1UL << nb) - 1); + + nl++; + if (nl < map->map_len) { + memset(map->map + nl, 0, + (map->map_len - nl) * (VIR_BITMAP_BITS_PER_UNIT / CHAR_BIT)); + } } diff --git a/tests/virbitmaptest.c b/tests/virbitmaptest.c index 9c0ffe70cb49..a3258dc0ebad 100644 --- a/tests/virbitmaptest.c +++ b/tests/virbitmaptest.c @@ -656,6 +656,11 @@ test12(const void *opaque ATTRIBUTE_UNUSED) TEST_MAP(1024, "34,1023"); + virBitmapShrink(map, 35); + TEST_MAP(35, "34"); + virBitmapShrink(map, 34); + TEST_MAP(34, ""); + ret = 0; cleanup: -- 2.16.1 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list