Probably it's more clear to post the entire function here for a discussion:Let's check again, if bits == 0, bits % whatever == 0 as well, thus,
int __bitmap_weight(const unsigned long *bitmap, unsigned int bits)
{
unsigned int k, lim = bits/BITS_PER_LONG;
int w = 0;
for (k = 0; k < lim; k++)
w += hweight_long(bitmap[k]);
if (bits % BITS_PER_LONG)
==> w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
return w;
}
When the execution reaches "==>", isn't "k=lim"?
no execution. When bits < BITS_PER_LONG, the execution is fine (k = 0
and still 0).
When bits >= BITS_PER_LONG, but not aligned, k goes from 0 to lim and
last word is exactly the partially filled one. No problem here. Las
case if bits % BITS_PER_LONG == 0, but hey, we have a guard against
this.
So, where is the problem exactly?
Where they are? Can't you point out?OTOH, for nbits=0, there _is_ no last word (since there are no words atYes, some callers there don't check for nbits=0, that's why I think it is
all), so by the time you want to apply the result of
BITMAP_LAST_WORD_MASK(0) to anything, you already have a bug, probably
either having read or being about to write into bitmap[0], which you
cannot do. Please check that user-space port and see if there are bugs
of that kind.
better to offload that check to the macro. The macro itself can be robust to
handle all the cases.
nbits=64, means all the 64 bits need to maskThe point is macro mustn't be called when nbits==0.
The two are different cases, I'm not sure why we let the macro to return the
same value.