pulsar icon indicating copy to clipboard operation
pulsar copied to clipboard

[Bug] LongBitmap range contains returns true for a missing value across a container boundary

Open void-ptr974 opened this issue 1 month ago • 2 comments

Search before reporting

  • [x] I searched existing issues and pull requests and did not find a report of this reproduction.

User environment

  • Apache Pulsar master checkout: 633511a812f4607a8d10c62ab4c8c50de36ae1ad.
  • The relevant production implementation, interface and dependency version file are unchanged at the subsequently fetched origin/master commit 0dfc5d88dcff93c9eaffff71f3eef6e91f22ad84.
  • RoaringBitmap 1.6.20; Temurin OpenJDK 25.0.2; macOS x86_64.
  • Reproduced against unchanged production sources at the component level. Released Pulsar versions were not evaluated.

Issue description

LongBitmap.contains(from, to) promises to return true only when all values in the half-open range [from, to) are present. A range crossing a bitmap container boundary can return true even though it includes an absent value.

For the set {131071}, the range [131071, 131073) contains both 131071 and 131072. The single-value query correctly reports that 131072 is absent, but the range query returns true.

Reproducing the issue

Add this method to the existing pulsar-common/src/test/java/org/apache/pulsar/common/util/collections/LongBitmapTest.java, together with import static org.assertj.core.api.Assertions.assertThat;:

@Test
public void testRangeContainsRejectsMissingValueAfterContainerBoundary() {
    LongBitmap bitmap = LongBitmaps.create();
    bitmap.add(131_071);

    assertThat(bitmap.contains(131_072)).isFalse(); // passes
    assertThat(bitmap.contains(131_071, 131_073)).isFalse(); // fails
}

Run:

./gradlew :pulsar-common:test \
  --tests 'org.apache.pulsar.common.util.collections.LongBitmapTest.testRangeContainsRejectsMissingValueAfterContainerBoundary' \
  -PtestRetryCount=0

Expected: the range query returns false.

Actual:

Expecting value to be false but was true

An independent Java main using the same Pulsar API reproduces the incorrect result with both java -da and java -ea. No broker, concurrency or injected internal state is required.

Additional information and impact

  • Range query contract.
  • The wrapper delegates directly to RoaringBitmap.contains(from, to).
  • This is an ordinary-value false positive, distinct from the uint32 upper-bound false negative already noted in LongBitmapTest.testUint32BoundaryRange.
  • I did not find an active production caller of the two-argument LongBitmap.contains(from, to) overload in the inspected source. Cursor membership and delayed-message indexing use single-value queries. This report establishes an API contract violation, not a demonstrated broker incident or message loss.

Are you willing to submit a PR?

  • [ ] I'm willing to submit a PR!

void-ptr974 avatar Oct 24 '26 03:10 void-ptr974

Anyone is welcome to pick this up and submit a fix. Please leave a comment here if you'd like to work on it so contributors can coordinate.

void-ptr974 avatar Oct 24 '26 03:10 void-ptr974

I'd like to work on this.

The wrapper delegates straight to RoaringBitmap.contains(from, to) (ConcurrentRoaringBitmap#contains), so the false positive comes from RoaringBitmap's cross-container range check. I plan to fix it on the Pulsar side, e.g. by checking rangeCardinality(from, to) == to - from under the read lock, add the regression test from this issue, and report the root cause upstream to RoaringBitmap. I'll handle #27089 in the same area too.

LiuYC3213687107 avatar Oct 24 '26 15:10 LiuYC3213687107