This is an automated email from the ASF dual-hosted git repository. leerho pushed a commit to branch 9.1.X in repository https://gitbox.apache.org/repos/asf/datasketches-java.git
commit e004666395cebe0bf1eb2f71916dac9fb045c526 Author: Lee Rhodes <[email protected]> AuthorDate: Thu Oct 29 14:54:06 2026 -0700 Bloom: check the image size in heapify() For a non-empty image, check that the remaining bytes hold the bit array. An empty image still allocates the configured bit array, as designed. Co-Authored-By: Claude Opus 5.5 <[email protected]> --- .../filters/bloomfilter/HeapBitArray.java | 4 ++ .../bloomfilter/BloomFilterCorruptImageTest.java | 67 ++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/main/java/org/apache/datasketches/filters/bloomfilter/HeapBitArray.java b/src/main/java/org/apache/datasketches/filters/bloomfilter/HeapBitArray.java index cb0e22727..0abd43f43 100644 --- a/src/main/java/org/apache/datasketches/filters/bloomfilter/HeapBitArray.java +++ b/src/main/java/org/apache/datasketches/filters/bloomfilter/HeapBitArray.java @@ -78,6 +78,10 @@ final class HeapBitArray extends BitArray { // will be -1 if dirty final long numBitsSet = posSeg.getLong(); + if (posSeg.getRemaining() < ((long) numLongs * Long.BYTES)) { + throw new SketchesArgumentException("Possible corruption: bit array of " + numLongs + + " longs does not fit in the remaining " + posSeg.getRemaining() + " bytes"); + } final long[] data = new long[numLongs]; posSeg.getLongArray(data, 0, numLongs); return new HeapBitArray(numBitsSet, data); diff --git a/src/test/java/org/apache/datasketches/filters/bloomfilter/BloomFilterCorruptImageTest.java b/src/test/java/org/apache/datasketches/filters/bloomfilter/BloomFilterCorruptImageTest.java new file mode 100644 index 000000000..14250be1f --- /dev/null +++ b/src/test/java/org/apache/datasketches/filters/bloomfilter/BloomFilterCorruptImageTest.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.datasketches.filters.bloomfilter; + +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.expectThrows; + +import java.lang.foreign.MemorySegment; + +import org.apache.datasketches.common.AllocationTracker; +import org.apache.datasketches.common.ByteArrayUtil; +import org.apache.datasketches.common.SketchesArgumentException; +import org.testng.annotations.Test; + +/** + * Checks that corrupt BloomFilter images are rejected, or handled, without large allocations. + */ +public class BloomFilterCorruptImageTest { + private static final int NUM_LONGS_INT = 16; + private static final int LARGE = 1 << 28; //2 GB of longs + + @Test + public void checkNonEmptyArrayBeyondImage() { + final BloomFilter bf = BloomFilterBuilder.createBySize(1000, 3, 123L); + bf.update(1L); + final byte[] bytes = bf.toByteArray(); + ByteArrayUtil.putIntLE(bytes, NUM_LONGS_INT, LARGE); + final long allocated = AllocationTracker.allocatedBytes(() -> BloomFilter.heapify(MemorySegment.ofArray(bytes))); + assertTrue(allocated < (1 << 20), "allocated " + allocated); + expectThrows(SketchesArgumentException.class, () -> BloomFilter.heapify(MemorySegment.ofArray(bytes))); + } + + @SuppressWarnings("deprecation") + @Test + public void checkEmptyImageIsUsable() { + final BloomFilter bf = BloomFilter.heapify(MemorySegment.ofArray(BloomFilterBuilder.createBySize(1000, 3, 123L).toByteArray())); + assertFalse(bf.query(1L)); + bf.update(1L); + assertTrue(bf.query(1L)); + final BloomFilter bf2 = BloomFilter.heapify(MemorySegment.ofArray(bf.toByteArray())); + assertTrue(bf2.query(1L)); + final BloomFilter empty = BloomFilter.heapify(MemorySegment.ofArray(BloomFilterBuilder.createBySize(1000, 3, 123L).toByteArray())); + empty.union(bf); + assertTrue(empty.query(1L)); + final BloomFilter empty2 = BloomFilter.heapify(MemorySegment.ofArray(BloomFilterBuilder.createBySize(1000, 3, 123L).toByteArray())); + empty2.invert(); + assertTrue(empty2.query(2L)); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
