[Qemu-devel] [PATCH 2/2] bitmap: fix bitmap_count_one

Wei Wang posted 2 patches 7 years, 3 months ago
[Qemu-devel] [PATCH 2/2] bitmap: fix bitmap_count_one
Posted by Wei Wang 7 years, 3 months ago
Since "nbits = 0" is not applicable to the BITMAP_LAST_WORD_MASK macro,
callers need to avoid passing "nbits = 0" to this macro, which generates
incorrect results.

Signed-off-by: Wei Wang <wei.w.wang@intel.com>
CC: Dr. David Alan Gilbert <dgilbert@redhat.com>
CC: Juan Quintela <quintela@redhat.com>
CC: Peter Xu <peterx@redhat.com>
---
 include/qemu/bitmap.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/qemu/bitmap.h b/include/qemu/bitmap.h
index f53c640..e45e9c0 100644
--- a/include/qemu/bitmap.h
+++ b/include/qemu/bitmap.h
@@ -223,7 +223,7 @@ static inline int bitmap_intersects(const unsigned long *src1,
 static inline long bitmap_count_one(const unsigned long *bitmap, long nbits)
 {
     if (small_nbits(nbits)) {
-        return ctpopl(*bitmap & BITMAP_LAST_WORD_MASK(nbits));
+        return nbits ? ctpopl(*bitmap & BITMAP_LAST_WORD_MASK(nbits)) : 0;
     } else {
         return slow_bitmap_count_one(bitmap, nbits);
     }
-- 
2.7.4