]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
bitmap: make bitmap_clear free the bitmap array 810/head
authorMartin Mikkelsen <mamikk@mamikk.no>
Fri, 31 Jul 2015 17:01:34 +0000 (19:01 +0200)
committerMartin Mikkelsen <mamikk@mamikk.no>
Fri, 31 Jul 2015 17:08:26 +0000 (19:08 +0200)
Given two bitmaps and the following code:

  Bitmap *a = bitmap_new(), *b = bitmap_new();
  bitmap_set(a, 1);
  bitmap_clear(a);
  bitmap_set(a, 0);
  bitmap_set(b, 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 while
bitmap_clear() resets the number of elements in the array it does not
clear the array and bitmap_set() expects the array to be cleared.
GREEDY_REALLOC0 looks at the allocated size and not the actual size so
it does not clear any memory.

Fix this by freeing the allocated memory and resetting the whole Bitmap
to an initial state in bitmap_clear().

This also adds test code for this issue.

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

index e7e19b3b6696dfcd82d86d0738e5caf2cbfcf550..7ea3357031b75ba1e2c03649ea531f282b0d9ae3 100644 (file)
@@ -145,7 +145,10 @@ bool bitmap_isclear(Bitmap *b) {
 void bitmap_clear(Bitmap *b) {
         assert(b);
 
+        free(b->bitmaps);
+        b->bitmaps = NULL;
         b->n_bitmaps = 0;
+        b->bitmaps_allocated = 0;
 }
 
 bool bitmap_iterate(Bitmap *b, Iterator *i, unsigned *n) {
index cb9e4a41f6e2c6aab197477998a9072f2cedf531..ff221177456f3d7f2f2e5d379c293155c0617e86 100644 (file)
@@ -111,5 +111,13 @@ int main(int argc, const char *argv[]) {
         bitmap_unset(b, 0);
         assert_se(bitmap_equal(b, b2));
 
+        assert_se(bitmap_set(b, 1) == 0);
+        bitmap_clear(b);
+        assert_se(bitmap_equal(b, b2));
+
+        assert_se(bitmap_set(b, 0) == 0);
+        assert_se(bitmap_set(b2, 0) == 0);
+        assert_se(bitmap_equal(b, b2));
+
         return 0;
 }