Skip to content

Commit

Permalink
hbitmap: Fix merge when b is empty, and result is not an alias of a
Browse files Browse the repository at this point in the history
Nobody calls the function like this currently, but we neither prohibit
or cope with this behavior. I decided to make the function cope with it.

Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Message-id: 20190709232550.10724-8-jsnow@redhat.com
Signed-off-by: John Snow <jsnow@redhat.com>
  • Loading branch information
jnsnow committed Aug 16, 2019
1 parent cf0cd29 commit 3bde4b0
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions util/hbitmap.c
Expand Up @@ -785,8 +785,9 @@ bool hbitmap_can_merge(const HBitmap *a, const HBitmap *b)
}

/**
* Given HBitmaps A and B, let A := A (BITOR) B.
* Bitmap B will not be modified.
* Given HBitmaps A and B, let R := A (BITOR) B.
* Bitmaps A and B will not be modified,
* except when bitmap R is an alias of A or B.
*
* @return true if the merge was successful,
* false if it was not attempted.
Expand All @@ -801,7 +802,13 @@ bool hbitmap_merge(const HBitmap *a, const HBitmap *b, HBitmap *result)
}
assert(hbitmap_can_merge(b, result));

if (hbitmap_count(b) == 0) {
if ((!hbitmap_count(a) && result == b) ||
(!hbitmap_count(b) && result == a)) {
return true;
}

if (!hbitmap_count(a) && !hbitmap_count(b)) {
hbitmap_reset_all(result);
return true;
}

Expand Down

0 comments on commit 3bde4b0

Please sign in to comment.