qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: weijiang.yang@intel.com
To: qemu-devel@nongnu.org, dgilbert@redhat.com, quintela@redhat.com,
	peterx@redhat.com, wei.w.wang@intel.com
Cc: Yang Weijiang <weijiang.yang@intel.com>
Subject: [Qemu-devel] [PATCH v1] This patch enhances bitmap op function input check and ambiguous return.
Date: Wed, 19 Dec 2018 12:50:21 +0800	[thread overview]
Message-ID: <20181219045021.18479-1-weijiang.yang@intel.com> (raw)

From: Yang Weijiang <weijiang.yang@intel.com>

    BITMAP_LAST_WORD_MASK(nbits) returns 0xffffffff when "nbits=0",
    this leads to error returns in some functions. This patch also
    checks bits input in some slow bitmap op functions, if bits = 0,
    do early return to keep the dst data unchanged.

    this patch is a follow-up patch of :
    https://lists.gnu.org/archive/html/qemu-devel/2018-12/msg03510.html

Signed-off-by: Yang Weijiang <weijiang.yang@intel.com>
---
 include/qemu/bitmap.h | 24 ++++++++++++++++++++++++
 util/bitmap.c         | 24 ++++++++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git a/include/qemu/bitmap.h b/include/qemu/bitmap.h
index 509eeddece..b6ce0ed551 100644
--- a/include/qemu/bitmap.h
+++ b/include/qemu/bitmap.h
@@ -113,6 +113,10 @@ static inline void bitmap_zero(unsigned long *dst, long nbits)
 
 static inline void bitmap_fill(unsigned long *dst, long nbits)
 {
+    if (unlikely(!nbits)) {
+        return;
+    }
+
     size_t nlongs = BITS_TO_LONGS(nbits);
     if (!small_nbits(nbits)) {
         long len = (nlongs - 1) * sizeof(unsigned long);
@@ -174,6 +178,10 @@ static inline void bitmap_complement(unsigned long *dst,
                                      const unsigned long *src,
                                      long nbits)
 {
+    if (unlikely(!nbits)) {
+        return;
+    }
+
     if (small_nbits(nbits)) {
         *dst = ~(*src) & BITMAP_LAST_WORD_MASK(nbits);
     } else {
@@ -184,6 +192,10 @@ static inline void bitmap_complement(unsigned long *dst,
 static inline int bitmap_equal(const unsigned long *src1,
                                const unsigned long *src2, long nbits)
 {
+    if (unlikely(!nbits)) {
+        return 0;
+    }
+
     if (small_nbits(nbits)) {
         return ! ((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
     } else {
@@ -193,6 +205,10 @@ static inline int bitmap_equal(const unsigned long *src1,
 
 static inline int bitmap_empty(const unsigned long *src, long nbits)
 {
+    if (unlikely(!nbits)) {
+        return 0;
+    }
+
     if (small_nbits(nbits)) {
         return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
     } else {
@@ -202,6 +218,10 @@ static inline int bitmap_empty(const unsigned long *src, long nbits)
 
 static inline int bitmap_full(const unsigned long *src, long nbits)
 {
+    if (unlikely(!nbits)) {
+        return 0;
+    }
+
     if (small_nbits(nbits)) {
         return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
     } else {
@@ -212,6 +232,10 @@ static inline int bitmap_full(const unsigned long *src, long nbits)
 static inline int bitmap_intersects(const unsigned long *src1,
                                     const unsigned long *src2, long nbits)
 {
+    if (unlikely(!nbits)) {
+        return 0;
+    }
+
     if (small_nbits(nbits)) {
         return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
     } else {
diff --git a/util/bitmap.c b/util/bitmap.c
index cb618c65a5..64bb16a46a 100644
--- a/util/bitmap.c
+++ b/util/bitmap.c
@@ -42,6 +42,10 @@ int slow_bitmap_empty(const unsigned long *bitmap, long bits)
 {
     long k, lim = bits/BITS_PER_LONG;
 
+    if (unlikely(!bits)) {
+        return 0;
+    }
+
     for (k = 0; k < lim; ++k) {
         if (bitmap[k]) {
             return 0;
@@ -60,6 +64,10 @@ int slow_bitmap_full(const unsigned long *bitmap, long bits)
 {
     long k, lim = bits/BITS_PER_LONG;
 
+    if (unlikely(!bits)) {
+        return 0;
+    }
+
     for (k = 0; k < lim; ++k) {
         if (~bitmap[k]) {
             return 0;
@@ -80,6 +88,10 @@ int slow_bitmap_equal(const unsigned long *bitmap1,
 {
     long k, lim = bits/BITS_PER_LONG;
 
+    if (unlikely(!bits)) {
+        return 0;
+    }
+
     for (k = 0; k < lim; ++k) {
         if (bitmap1[k] != bitmap2[k]) {
             return 0;
@@ -116,6 +128,10 @@ int slow_bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
     long nr = BITS_TO_LONGS(bits);
     unsigned long result = 0;
 
+    if (unlikely(!bits)) {
+        return 0;
+    }
+
     for (k = 0; k < nr; k++) {
         result |= (dst[k] = bitmap1[k] & bitmap2[k]);
     }
@@ -342,6 +358,10 @@ int slow_bitmap_intersects(const unsigned long *bitmap1,
 {
     long k, lim = bits/BITS_PER_LONG;
 
+    if (unlikely(!bits)) {
+        return 0;
+    }
+
     for (k = 0; k < lim; ++k) {
         if (bitmap1[k] & bitmap2[k]) {
             return 1;
@@ -360,6 +380,10 @@ long slow_bitmap_count_one(const unsigned long *bitmap, long nbits)
 {
     long k, lim = nbits / BITS_PER_LONG, result = 0;
 
+    if (unlikely(!nbits)) {
+        return 0;
+    }
+
     for (k = 0; k < lim; k++) {
         result += ctpopl(bitmap[k]);
     }
-- 
2.17.1

                 reply	other threads:[~2018-12-19  4:48 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181219045021.18479-1-weijiang.yang@intel.com \
    --to=weijiang.yang@intel.com \
    --cc=dgilbert@redhat.com \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    --cc=wei.w.wang@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).