From: Peter Krempa Date: Thu, 17 Oct 2024 11:55:23 +0000 (+0200) Subject: util: bitmap: Rewrite virBitmapShrink using new helpers X-Git-Tag: v10.9.0-rc1~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7cbe9e94c443a6961dd1e4a5d474163405644281;p=thirdparty%2Flibvirt.git util: bitmap: Rewrite virBitmapShrink using new helpers 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 --- 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 = 0; - size_t nb = 0; - - if (!map) - return; - - if (map->nbits >= b) - map->nbits = b; - - nl = map->nbits / VIR_BITMAP_BITS_PER_UNIT; - nb = map->nbits % VIR_BITMAP_BITS_PER_UNIT; - - /* If we're at the end of the allocation the attempt to clear 'map->nbit' - * and further would be beyond the end of the bitmap */ - if (nl >= map->map_alloc) + if (!map || + map->nbits <= b) return; - map->map[nl] &= ((1UL << nb) - 1); - - toremove = map->map_alloc - (nl + 1); - - if (toremove == 0) - return; - - VIR_SHRINK_N(map->map, map->map_alloc, toremove); - - /* length needs to be fixed as well */ - map->map_len = map->map_alloc; + map->nbits = b; + map->map_len = virBitmapBuffsize(b); + map->map = g_renew(unsigned long, map->map, map->map_len); + map->map_alloc = map->map_len; + virBitmapClearTail(map); }