Skip to content

Commit

Permalink
fix: always throw DivideByZeroException when BloomFilter is empty (
Browse files Browse the repository at this point in the history
#3502)

* fix: always throw divided by zero when BloomFilter is empty

* fix: always throw divided by zero when BloomFilter is empty
  • Loading branch information
nan01ab authored Sep 29, 2024
1 parent 9287c66 commit 33d0913
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Neo/Cryptography/BloomFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ public class BloomFilter
/// <summary>
/// Initializes a new instance of the <see cref="BloomFilter"/> class.
/// </summary>
/// <param name="m">The size of the bit array used by the bloom filter.</param>
/// <param name="k">The number of hash functions used by the bloom filter.</param>
/// <param name="m">The size of the bit array used by the bloom filter, and must be greater than 0.</param>
/// <param name="k">The number of hash functions used by the bloom filter, and must be greater than 0.</param>
/// <param name="nTweak">Used to generate the seeds of the murmur hash functions.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="k"/> or <paramref name="m"/> is less than or equal to 0.</exception>
public BloomFilter(int m, int k, uint nTweak)
{
if (k < 0 || m < 0) throw new ArgumentOutOfRangeException();
if (k <= 0 || m <= 0) throw new ArgumentOutOfRangeException();
seeds = Enumerable.Range(0, k).Select(p => (uint)p * 0xFBA4C795 + nTweak).ToArray();
bits = new BitArray(m)
{
Expand All @@ -58,13 +59,14 @@ public BloomFilter(int m, int k, uint nTweak)
/// <summary>
/// Initializes a new instance of the <see cref="BloomFilter"/> class.
/// </summary>
/// <param name="m">The size of the bit array used by the bloom filter.</param>
/// <param name="k">The number of hash functions used by the bloom filter.</param>
/// <param name="m">The size of the bit array used by the bloom filter, and must be greater than 0.</param>
/// <param name="k">The number of hash functions used by the bloom filter, and must be greater than 0.</param>
/// <param name="nTweak">Used to generate the seeds of the murmur hash functions.</param>
/// <param name="elements">The initial elements contained in this <see cref="BloomFilter"/> object.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="k"/> or <paramref name="m"/> is less than or equal to 0.</exception>
public BloomFilter(int m, int k, uint nTweak, ReadOnlyMemory<byte> elements)
{
if (k < 0 || m < 0) throw new ArgumentOutOfRangeException();
if (k <= 0 || m <= 0) throw new ArgumentOutOfRangeException();
seeds = Enumerable.Range(0, k).Select(p => (uint)p * 0xFBA4C795 + nTweak).ToArray();
bits = new BitArray(elements.ToArray())
{
Expand Down
11 changes: 11 additions & 0 deletions tests/Neo.UnitTests/Cryptography/UT_BloomFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,16 @@ public void TestGetBits()
foreach (byte value in result)
value.Should().Be(0);
}

[TestMethod]
public void TestInvalidArguments()
{
uint nTweak = 123456;
Action action = () => new BloomFilter(0, 3, nTweak);
action.Should().Throw<ArgumentOutOfRangeException>();

action = () => new BloomFilter(3, 0, nTweak);
action.Should().Throw<ArgumentOutOfRangeException>();
}
}
}

0 comments on commit 33d0913

Please sign in to comment.