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.
It's enough to zero out one piece of the map since we're calling realloc() to
get rid of the rest (and updating map_len).
Resolves:
https://bugzilla.redhat.com/show_bug.cgi?id=1540817
Signed-off-by: Martin Kletzander <mkletzan(a)redhat.com>
---
src/conf/domain_conf.c | 4 +++-
src/util/virbitmap.c | 30 ++++++++++++++++++++++--------
src/util/virbitmap.h | 2 +-
src/util/virresctrl.c | 3 ++-
tests/virbitmaptest.c | 8 ++++++++
5 files changed, 36 insertions(+), 11 deletions(-)
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 01d168eb875b..e827b2a810f7 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -18453,7 +18453,9 @@ virDomainCachetuneDefParse(virDomainDefPtr def,
/* We need to limit the bitmap to number of vCPUs. If there's nothing left,
* then we can just clean up and return 0 immediately */
- virBitmapShrink(vcpus, def->maxvcpus);
+ if (virBitmapShrink(vcpus, def->maxvcpus) < 0)
+ goto cleanup;
+
if (virBitmapIsAllClear(vcpus)) {
ret = 0;
goto cleanup;
diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c
index b2c5c7a6a5ac..33cae2f30569 100644
--- a/src/util/virbitmap.c
+++ b/src/util/virbitmap.c
@@ -1201,21 +1201,35 @@ virBitmapSubtract(virBitmapPtr a,
/**
* virBitmapShrink:
* @map: Pointer to bitmap
- * @b: last bit position to be excluded from bitmap
+ * @b: Size to reduce the bitmap to
*
- * Resizes the bitmap so that no more than @b bits will fit into it. Nothing
- * will change if the size is already smaller than @b.
- *
- * NB: Does not adjust the map->map_len so that a subsequent virBitmapExpand
- * doesn't necessarily need to reallocate.
+ * Reduces the bitmap to size @b. Nothing will change if the size is already
+ * smaller than or equal to @b.
*/
-void
+int
virBitmapShrink(virBitmapPtr map,
size_t b)
{
+ size_t nl = 0;
+ size_t nb = 0;
+
if (!map)
- return;
+ return 0;
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)
+ return 0;
+
+ if (VIR_REALLOC_N(map->map, nl) < 0)