[PATCH 10/15] virBitmapNewQuiet: Don't fail on unlikely overflow scenario

Peter Krempa pkrempa at redhat.com
Fri Oct 2 07:43:04 UTC 2020


Modify the condition which would make virBitmapNewQuiet fail to possibly
overallocate by 1 rather than failing.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/util/virbitmap.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c
index 5df7ea7838..a49019f884 100644
--- a/src/util/virbitmap.c
+++ b/src/util/virbitmap.c
@@ -55,8 +55,7 @@ struct _virBitmap {
  *
  * Allocate a bitmap capable of containing @size bits.
  *
- * Returns a pointer to the allocated bitmap or NULL if memory cannot be
- * allocated. Does not report libvirt errors.
+ * Returns a pointer to the allocated bitmap.
  */
 virBitmapPtr
 virBitmapNewQuiet(size_t size)
@@ -64,10 +63,13 @@ virBitmapNewQuiet(size_t size)
     virBitmapPtr bitmap;
     size_t sz;

-    if (SIZE_MAX - VIR_BITMAP_BITS_PER_UNIT < size)
-        return NULL;
-
-    sz = VIR_DIV_UP(size, VIR_BITMAP_BITS_PER_UNIT);
+    if (SIZE_MAX - VIR_BITMAP_BITS_PER_UNIT < size) {
+        /* VIR_DIV_UP would overflow, let's overallocate by 1 entry instead of
+         * the potential overflow */
+        sz = (size / VIR_BITMAP_BITS_PER_UNIT) + 1;
+    } else {
+        sz = VIR_DIV_UP(size, VIR_BITMAP_BITS_PER_UNIT);
+    }

     bitmap = g_new0(virBitmap, 1);

-- 
2.26.2




More information about the libvir-list mailing list