lib: bitmap: add missing mask in bitmap_and
authorRasmus Villemoes <linux@rasmusvillemoes.dk>
Wed, 6 Aug 2014 23:10:22 +0000 (16:10 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Thu, 7 Aug 2014 01:01:27 +0000 (18:01 -0700)
Apparently, bitmap_and is supposed to return whether the new bitmap is
empty.  But it didn't take potential garbage bits in the last word into
account.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
include/linux/bitmap.h
lib/bitmap.c

index 75df61d9ecfb786665a20cf44ae00e1e2a44d50a..3399a9ecd991dbb79cce63ec0e02cbb4461dd2f1 100644 (file)
@@ -191,7 +191,7 @@ static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
                        const unsigned long *src2, unsigned int nbits)
 {
        if (small_const_nbits(nbits))
-               return (*dst = *src1 & *src2) != 0;
+               return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
        return __bitmap_and(dst, src1, src2, nbits);
 }
 
index faaf7206d4cf30adb268d329beb8c2814ddf760f..ce2ec80bf4315e55057b4514e5435af454ca8cac 100644 (file)
@@ -185,11 +185,14 @@ int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
                                const unsigned long *bitmap2, unsigned int bits)
 {
        unsigned int k;
-       unsigned int nr = BITS_TO_LONGS(bits);
+       unsigned int lim = bits/BITS_PER_LONG;
        unsigned long result = 0;
 
-       for (k = 0; k < nr; k++)
+       for (k = 0; k < lim; k++)
                result |= (dst[k] = bitmap1[k] & bitmap2[k]);
+       if (bits % BITS_PER_LONG)
+               result |= (dst[k] = bitmap1[k] & bitmap2[k] &
+                          BITMAP_LAST_WORD_MASK(bits));
        return result != 0;
 }
 EXPORT_SYMBOL(__bitmap_and);