summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Gundersen <teg@jklm.no>2015-07-17 18:30:41 +0200
committerTom Gundersen <teg@jklm.no>2015-07-17 18:59:16 +0200
commitcdf6f5ae0465a8fb25d2afc758911985cc542a07 (patch)
treefd6a99b9ee96ffdd378d46d16770cedb02b96ae0
parent511ed7991737dc567bef51a8051a1a577fa18e05 (diff)
basic: bitmap - complete fix for bitshift overflow
The bug found by David existed in several places, fix them all. Also extend the tests to cover these cases.
-rw-r--r--src/basic/bitmap.c14
-rw-r--r--src/test/test-bitmap.c8
2 files changed, 15 insertions, 7 deletions
diff --git a/src/basic/bitmap.c b/src/basic/bitmap.c
index c5039fd22f..0747749d13 100644
--- a/src/basic/bitmap.c
+++ b/src/basic/bitmap.c
@@ -69,7 +69,7 @@ int bitmap_ensure_allocated(Bitmap **b) {
}
int bitmap_set(Bitmap *b, unsigned n) {
- long long bitmask;
+ long long unsigned bitmask;
unsigned offset;
assert(b);
@@ -87,7 +87,7 @@ int bitmap_set(Bitmap *b, unsigned n) {
b->n_bitmaps = offset + 1;
}
- bitmask = 1 << BITMAP_NUM_TO_REM(n);
+ bitmask = 1ULL << BITMAP_NUM_TO_REM(n);
b->bitmaps[offset] |= bitmask;
@@ -95,7 +95,7 @@ int bitmap_set(Bitmap *b, unsigned n) {
}
void bitmap_unset(Bitmap *b, unsigned n) {
- long long bitmask;
+ long long unsigned bitmask;
unsigned offset;
assert(b);
@@ -105,13 +105,13 @@ void bitmap_unset(Bitmap *b, unsigned n) {
if (offset >= b->n_bitmaps)
return;
- bitmask = 1 << BITMAP_NUM_TO_REM(n);
+ bitmask = 1ULL << BITMAP_NUM_TO_REM(n);
b->bitmaps[offset] &= ~bitmask;
}
bool bitmap_isset(Bitmap *b, unsigned n) {
- long long bitmask;
+ long long unsigned bitmask;
unsigned offset;
if (!b || !b->bitmaps)
@@ -122,7 +122,7 @@ bool bitmap_isset(Bitmap *b, unsigned n) {
if (offset >= b->n_bitmaps)
return false;
- bitmask = 1 << BITMAP_NUM_TO_REM(n);
+ bitmask = 1ULL << BITMAP_NUM_TO_REM(n);
return !!(b->bitmaps[offset] & bitmask);
}
@@ -149,7 +149,7 @@ void bitmap_clear(Bitmap *b) {
}
bool bitmap_iterate(Bitmap *b, Iterator *i, unsigned *n) {
- long long bitmask;
+ long long unsigned bitmask;
unsigned offset, rem;
if (!b || i->idx == BITMAP_END)
diff --git a/src/test/test-bitmap.c b/src/test/test-bitmap.c
index 77db784a94..96deeded7e 100644
--- a/src/test/test-bitmap.c
+++ b/src/test/test-bitmap.c
@@ -58,6 +58,14 @@ int main(int argc, const char *argv[]) {
assert_se(bitmap_isset(b, 256) == false);
assert_se(bitmap_isclear(b) == true);
+ assert_se(bitmap_set(b, 32) == 0);
+ bitmap_unset(b, 0);
+ assert_se(bitmap_isset(b, 32) == true);
+ bitmap_unset(b, 32);
+
+ BITMAP_FOREACH(n, NULL, it)
+ assert_not_reached("NULL bitmap");
+
assert_se(bitmap_set(b, 0) == 0);
assert_se(bitmap_set(b, 1) == 0);
assert_se(bitmap_set(b, 256) == 0);