mirror of
https://github.com/systemd/systemd.git
synced 2026-08-05 07:30:30 +00:00
bitmap: fix bitmap_equal on bitmaps with unset bits
Given two bitmaps and the following code: Bitmap *a = bitmap_new(), *b = bitmap_new(); bitmap_set(a, 0); bitmap_unset(a, 0); These two bitmaps should now have the same bits set and they should be equal but bitmap_equal() will return false in this case because the bitmaps array in a is larger because of the bit which was previously set. Fix this by comparing only the bits which exists in both bitmaps and then check that the rest of the bits (if any) is all zero. This also adds test code for this issue.
This commit is contained in:
@@ -184,6 +184,9 @@ bool bitmap_iterate(Bitmap *b, Iterator *i, unsigned *n) {
|
||||
}
|
||||
|
||||
bool bitmap_equal(Bitmap *a, Bitmap *b) {
|
||||
size_t common_n_bitmaps;
|
||||
Bitmap *c;
|
||||
unsigned i;
|
||||
|
||||
if (!a ^ !b)
|
||||
return false;
|
||||
@@ -191,8 +194,14 @@ bool bitmap_equal(Bitmap *a, Bitmap *b) {
|
||||
if (!a)
|
||||
return true;
|
||||
|
||||
if (a->n_bitmaps != b->n_bitmaps)
|
||||
common_n_bitmaps = MIN(a->n_bitmaps, b->n_bitmaps);
|
||||
if (memcmp(a->bitmaps, b->bitmaps, sizeof(uint64_t) * common_n_bitmaps) != 0)
|
||||
return false;
|
||||
|
||||
return memcmp(a->bitmaps, b->bitmaps, sizeof(uint64_t) * a->n_bitmaps) == 0;
|
||||
c = a->n_bitmaps > b->n_bitmaps ? a : b;
|
||||
for (i = common_n_bitmaps; i < c->n_bitmaps; i++)
|
||||
if (c->bitmaps[i] != 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user