History

bitmap: Fix return values to be unsigned

Both nodemask and bitmap routines had mixed return values that provided potentially signed return values that could never happen. This was leading to the compiler getting confusing about the range of possible return values (it was thinking things could be negative where they could not be). In preparation for fixing nodemask, fix all the bitmap routines that should be returning unsigned (or bool) values.

Kees Cook committed 4 years ago

Commit 005f1701 parent d603fd84 files · +47 −45Browse files

Unified · Split

4 files
  1. include/linux/bitmap.h +13 −12
  2. lib/bitmap.c +15 −15
  3. tools/include/linux/bitmap.h +9 −8
  4. tools/lib/bitmap.c +10 −10
include/linux/bitmap.h+13−12
@@ -134,8 +134,8 @@ unsigned long *devm_bitmap_zalloc(struct device *dev, · 133 unmodified lines
134 * lib/bitmap.c provides these functions:134 * lib/bitmap.c provides these functions:
135 */135 */
136 136
137int __bitmap_equal(const unsigned long *bitmap1,137bool __bitmap_equal(const unsigned long *bitmap1,
138 const unsigned long *bitmap2, unsigned int nbits);138 const unsigned long *bitmap2, unsigned int nbits);
139bool __pure __bitmap_or_equal(const unsigned long *src1,139bool __pure __bitmap_or_equal(const unsigned long *src1,
140 const unsigned long *src2,140 const unsigned long *src2,
141 const unsigned long *src3,141 const unsigned long *src3,
@@ -159,10 +159,10 @@ int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1, · 17 unmodified lines
159void __bitmap_replace(unsigned long *dst,159void __bitmap_replace(unsigned long *dst,
160 const unsigned long *old, const unsigned long *new,160 const unsigned long *old, const unsigned long *new,
161 const unsigned long *mask, unsigned int nbits);161 const unsigned long *mask, unsigned int nbits);
162int __bitmap_intersects(const unsigned long *bitmap1,162bool __bitmap_intersects(const unsigned long *bitmap1,
163 const unsigned long *bitmap2, unsigned int nbits);163 const unsigned long *bitmap2, unsigned int nbits);
164int __bitmap_subset(const unsigned long *bitmap1,164bool __bitmap_subset(const unsigned long *bitmap1,
165 const unsigned long *bitmap2, unsigned int nbits);165 const unsigned long *bitmap2, unsigned int nbits);
166int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);166int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
167void __bitmap_set(unsigned long *map, unsigned int start, int len);167void __bitmap_set(unsigned long *map, unsigned int start, int len);
168void __bitmap_clear(unsigned long *map, unsigned int start, int len);168void __bitmap_clear(unsigned long *map, unsigned int start, int len);
@@ -353,8 +353,8 @@ static inline void bitmap_complement(unsigned long *dst, const unsigned long *sr · 184 unmodified lines
353#endif353#endif
354#define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)354#define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
355 355
356static inline int bitmap_equal(const unsigned long *src1,356static inline bool bitmap_equal(const unsigned long *src1,
357 const unsigned long *src2, unsigned int nbits)357 const unsigned long *src2, unsigned int nbits)
358{358{
359 if (small_const_nbits(nbits))359 if (small_const_nbits(nbits))
360 return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));360 return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
@@ -384,8 +384,9 @@ static inline bool bitmap_or_equal(const unsigned long *src1, · 23 unmodified lines
384 return !(((*src1 | *src2) ^ *src3) & BITMAP_LAST_WORD_MASK(nbits));384 return !(((*src1 | *src2) ^ *src3) & BITMAP_LAST_WORD_MASK(nbits));
385}385}
386 386
387static inline int bitmap_intersects(const unsigned long *src1,387static inline bool bitmap_intersects(const unsigned long *src1,
388 const unsigned long *src2, unsigned int nbits)388 const unsigned long *src2,
389 unsigned int nbits)
389{390{
390 if (small_const_nbits(nbits))391 if (small_const_nbits(nbits))
391 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;392 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
@@ -393,8 +394,8 @@ static inline int bitmap_intersects(const unsigned long *src1, · 1 unmodified line
393 return __bitmap_intersects(src1, src2, nbits);394 return __bitmap_intersects(src1, src2, nbits);
394}395}
395 396
396static inline int bitmap_subset(const unsigned long *src1,397static inline bool bitmap_subset(const unsigned long *src1,
397 const unsigned long *src2, unsigned int nbits)398 const unsigned long *src2, unsigned int nbits)
398{399{
399 if (small_const_nbits(nbits))400 if (small_const_nbits(nbits))
400 return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));401 return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
lib/bitmap.c+15−15
@@ -45,19 +45,19 @@ · 44 unmodified lines
45 * for the best explanations of this ordering.45 * for the best explanations of this ordering.
46 */46 */
47 47
48int __bitmap_equal(const unsigned long *bitmap1,48bool __bitmap_equal(const unsigned long *bitmap1,
49 const unsigned long *bitmap2, unsigned int bits)49 const unsigned long *bitmap2, unsigned int bits)
50{50{
51 unsigned int k, lim = bits/BITS_PER_LONG;51 unsigned int k, lim = bits/BITS_PER_LONG;
52 for (k = 0; k < lim; ++k)52 for (k = 0; k < lim; ++k)
53 if (bitmap1[k] != bitmap2[k])53 if (bitmap1[k] != bitmap2[k])
54 return 0;54 return false;
55 55
56 if (bits % BITS_PER_LONG)56 if (bits % BITS_PER_LONG)
57 if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))57 if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
58 return 0;58 return false;
59 59
60 return 1;60 return true;
61}61}
62EXPORT_SYMBOL(__bitmap_equal);62EXPORT_SYMBOL(__bitmap_equal);
63 63
@@ -303,33 +303,33 @@ void __bitmap_replace(unsigned long *dst, · 239 unmodified lines
303}303}
304EXPORT_SYMBOL(__bitmap_replace);304EXPORT_SYMBOL(__bitmap_replace);
305 305
306int __bitmap_intersects(const unsigned long *bitmap1,306bool __bitmap_intersects(const unsigned long *bitmap1,
307 const unsigned long *bitmap2, unsigned int bits)307 const unsigned long *bitmap2, unsigned int bits)
308{308{
309 unsigned int k, lim = bits/BITS_PER_LONG;309 unsigned int k, lim = bits/BITS_PER_LONG;
310 for (k = 0; k < lim; ++k)310 for (k = 0; k < lim; ++k)
311 if (bitmap1[k] & bitmap2[k])311 if (bitmap1[k] & bitmap2[k])
312 return 1;312 return true;
313 313
314 if (bits % BITS_PER_LONG)314 if (bits % BITS_PER_LONG)
315 if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))315 if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
316 return 1;316 return true;
317 return 0;317 return false;
318}318}
319EXPORT_SYMBOL(__bitmap_intersects);319EXPORT_SYMBOL(__bitmap_intersects);
320 320
321int __bitmap_subset(const unsigned long *bitmap1,321bool __bitmap_subset(const unsigned long *bitmap1,
322 const unsigned long *bitmap2, unsigned int bits)322 const unsigned long *bitmap2, unsigned int bits)
323{323{
324 unsigned int k, lim = bits/BITS_PER_LONG;324 unsigned int k, lim = bits/BITS_PER_LONG;
325 for (k = 0; k < lim; ++k)325 for (k = 0; k < lim; ++k)
326 if (bitmap1[k] & ~bitmap2[k])326 if (bitmap1[k] & ~bitmap2[k])
327 return 0;327 return false;
328 328
329 if (bits % BITS_PER_LONG)329 if (bits % BITS_PER_LONG)
330 if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))330 if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
331 return 0;331 return false;
332 return 1;332 return true;
333}333}
334EXPORT_SYMBOL(__bitmap_subset);334EXPORT_SYMBOL(__bitmap_subset);
335 335
tools/include/linux/bitmap.h+9−8
@@ -16,11 +16,11 @@ void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, · 15 unmodified lines
16 const unsigned long *bitmap2, int bits);16 const unsigned long *bitmap2, int bits);
17int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,17int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
18 const unsigned long *bitmap2, unsigned int bits);18 const unsigned long *bitmap2, unsigned int bits);
19int __bitmap_equal(const unsigned long *bitmap1,19bool __bitmap_equal(const unsigned long *bitmap1,
20 const unsigned long *bitmap2, unsigned int bits);20 const unsigned long *bitmap2, unsigned int bits);
21void bitmap_clear(unsigned long *map, unsigned int start, int len);21void bitmap_clear(unsigned long *map, unsigned int start, int len);
22int __bitmap_intersects(const unsigned long *bitmap1,22bool __bitmap_intersects(const unsigned long *bitmap1,
23 const unsigned long *bitmap2, unsigned int bits);23 const unsigned long *bitmap2, unsigned int bits);
24 24
25#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))25#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
26#define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))26#define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
@@ -162,8 +162,8 @@ static inline int bitmap_and(unsigned long *dst, const unsigned long *src1, · 135 unmodified lines
162#define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)162#define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
163#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)163#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
164 164
165static inline int bitmap_equal(const unsigned long *src1,165static inline bool bitmap_equal(const unsigned long *src1,
166 const unsigned long *src2, unsigned int nbits)166 const unsigned long *src2, unsigned int nbits)
167{167{
168 if (small_const_nbits(nbits))168 if (small_const_nbits(nbits))
169 return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));169 return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
@@ -173,8 +173,9 @@ static inline int bitmap_equal(const unsigned long *src1, · 3 unmodified lines
173 return __bitmap_equal(src1, src2, nbits);173 return __bitmap_equal(src1, src2, nbits);
174}174}
175 175
176static inline int bitmap_intersects(const unsigned long *src1,176static inline bool bitmap_intersects(const unsigned long *src1,
177 const unsigned long *src2, unsigned int nbits)177 const unsigned long *src2,
178 unsigned int nbits)
178{179{
179 if (small_const_nbits(nbits))180 if (small_const_nbits(nbits))
180 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;181 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
tools/lib/bitmap.c+10−10
@@ -72,31 +72,31 @@ int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1, · 71 unmodified lines
72 return result != 0;72 return result != 0;
73}73}
74 74
75int __bitmap_equal(const unsigned long *bitmap1,75bool __bitmap_equal(const unsigned long *bitmap1,
76 const unsigned long *bitmap2, unsigned int bits)76 const unsigned long *bitmap2, unsigned int bits)
77{77{
78 unsigned int k, lim = bits/BITS_PER_LONG;78 unsigned int k, lim = bits/BITS_PER_LONG;
79 for (k = 0; k < lim; ++k)79 for (k = 0; k < lim; ++k)
80 if (bitmap1[k] != bitmap2[k])80 if (bitmap1[k] != bitmap2[k])
81 return 0;81 return false;
82 82
83 if (bits % BITS_PER_LONG)83 if (bits % BITS_PER_LONG)
84 if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))84 if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
85 return 0;85 return false;
86 86
87 return 1;87 return true;
88}88}
89 89
90int __bitmap_intersects(const unsigned long *bitmap1,90bool __bitmap_intersects(const unsigned long *bitmap1,
91 const unsigned long *bitmap2, unsigned int bits)91 const unsigned long *bitmap2, unsigned int bits)
92{92{
93 unsigned int k, lim = bits/BITS_PER_LONG;93 unsigned int k, lim = bits/BITS_PER_LONG;
94 for (k = 0; k < lim; ++k)94 for (k = 0; k < lim; ++k)
95 if (bitmap1[k] & bitmap2[k])95 if (bitmap1[k] & bitmap2[k])
96 return 1;96 return true;
97 97
98 if (bits % BITS_PER_LONG)98 if (bits % BITS_PER_LONG)
99 if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))99 if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits))
100 return 1;100 return true;
101 return 0;101 return false;
102}102}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t