]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: bitmap - complete fix for bitshift overflow 614/head
authorTom Gundersen <teg@jklm.no>
Fri, 17 Jul 2015 16:30:41 +0000 (18:30 +0200)
committerTom Gundersen <teg@jklm.no>
Fri, 17 Jul 2015 16:59:16 +0000 (18:59 +0200)
The bug found by David existed in several places, fix them all. Also
extend the tests to cover these cases.

src/basic/bitmap.c
src/test/test-bitmap.c

index c5039fd22f2623e8ae49eaac13c27c0dd9a8915d..0747749d13b88a5451d853ee112a0ba14234acaf 100644 (file)
@@ -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)
index 77db784a94ad9cce504048cde55961d31aa23cb7..96deeded7e500663d9dd166d5f8539ca6b3ce4e4 100644 (file)
@@ -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);