Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: always throw DivideByZeroException when BloomFilter is empty #3502

Merged
merged 3 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>();
}
}
}
Loading