Skip to content

Commit fd7c405

Browse files
fix array OOBE in blocked bloom filter when top 4 bits of hash are set (seed dependent behaviour)
1 parent a34c126 commit fd7c405

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

fastfilter/src/main/java/org/fastfilter/bloom/BlockedBloom.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
public class BlockedBloom implements Filter {
1313

1414
public static BlockedBloom construct(long[] keys, int bitsPerKey) {
15-
long n = keys.length;
16-
BlockedBloom f = new BlockedBloom((int) n, bitsPerKey);
15+
int n = keys.length;
16+
BlockedBloom f = new BlockedBloom(n, bitsPerKey);
1717
for(long x : keys) {
1818
f.add(x);
1919
}
@@ -34,7 +34,7 @@ public long getBitCount() {
3434
this.seed = Hash.randomSeed();
3535
long bits = (long) entryCount * bitsPerKey;
3636
this.buckets = (int) bits / 64;
37-
data = new long[(int) (buckets + 16)];
37+
data = new long[(buckets + 16)];
3838
}
3939

4040
@Override
@@ -50,7 +50,7 @@ public void add(long key) {
5050
long m1 = (1L << hash) | (1L << (hash >> 6));
5151
long m2 = (1L << (hash >> 12)) | (1L << (hash >> 18));
5252
data[start] |= m1;
53-
data[start + 1 + (int) (hash >>> 60)] |= m2;
53+
data[start + (int) (hash >>> 60)] |= m2;
5454
}
5555

5656
@Override
@@ -59,7 +59,7 @@ public boolean mayContain(long key) {
5959
int start = Hash.reduce((int) hash, buckets);
6060
hash = hash ^ Long.rotateLeft(hash, 32);
6161
long a = data[start];
62-
long b = data[start + 1 + (int) (hash >>> 60)];
62+
long b = data[start + (int) (hash >>> 60)];
6363
long m1 = (1L << hash) | (1L << (hash >> 6));
6464
long m2 = (1L << (hash >> 12)) | (1L << (hash >> 18));
6565
return ((m1 & a) == m1) && ((m2 & b) == m2);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.fastfilter.bloom;
2+
3+
import org.fastfilter.utils.Hash;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertTrue;
7+
8+
public class BlockedBloomTest {
9+
10+
@Test
11+
public void testCreateSmallBlockedBloomFilter() {
12+
Hash.setSeed(872153271794238865L);
13+
BlockedBloom filter = BlockedBloom.construct(new long[]{1, 2, 3}, 8);
14+
assertTrue(filter.mayContain(1));
15+
assertTrue(filter.mayContain(2));
16+
assertTrue(filter.mayContain(3));
17+
}
18+
19+
}

0 commit comments

Comments
 (0)