[Bug] BitsArray rejects partial-byte clones but accepts bit lengths beyond backing capacity
Before Creating the Bug Report
-
[x] I found a bug, not just asking a question, which should be created in GitHub Discussions.
-
[x] I have searched the GitHub Issues and GitHub Discussions of this repository and believe that this is not a duplicate.
-
[x] I have confirmed that this bug belongs to the current repository, not other repositories of RocketMQ.
Runtime platform environment
Windows 11, local utility reproducer and native Maven filter-module tests. No broker deployment or message-loss claim.
RocketMQ version
develop, revision 5.5.1, commit 57c2dc04f78cc7e73e6b8c9436ee0fff0d0f46c3. The current develop source still has the same constructor.
JDK Version
OpenJDK 1.8.0_302 (GraalVM CE 21.2.0); also reproduced with Java 21 using Java 8-targeted utility/test classes. Native Maven validation used Java 8 and Maven 3.6.3.
Describe the Bug
The byte-array constructor rejects bitLength < bytes.length * 8 instead of rejecting a logical length larger than the backing capacity. As a result, any positive non-byte-aligned array created by create(int) fails to clone, while an impossible byte-array capacity is accepted. The null/empty/non-positive checks and defensive copy are otherwise present.
Steps to Reproduce
BitsArray.create(9).clone();
// IllegalArgumentException: BitLength is less than bytes.length() * 8
BitsArray.create(new byte[] {1, 0}, 9);
// Same exception despite sufficient capacity.
BitsArray.create(new byte[] {1}, 9).getBit(8);
// Constructor accepts it; subsequent access is out of bounds.
What Did You Expect to See?
Cloning a valid 9-bit array preserves its logical length, contents and independent backing storage. A byte-array constructor accepts a logical bit length within storage capacity and rejects lengths beyond that capacity.
What Did You See Instead?
Valid partial-byte reconstruction and clone throw IllegalArgumentException. A one-byte backing array can be declared as nine bits, allowing a later ArrayIndexOutOfBoundsException.
Additional Context
Duplicate checks: #10506 (merged) and #8664 (closed) fix position bounds, not this constructor capacity comparison. No matching open BitsArray PR or discussion was found.
A minimal correction and regression tests are prepared. The updated utility class has 28 tests: 3 failed before the fix and all 28 passed after on Java 8/21. Native Java 8 filter reactor: 110 tests passed, 0 failures/errors/skips, with Checkstyle and SpotBugs passing. BloomFilter normally rounds to whole-byte lengths, so no live broker failure or message-loss claim is made.
AI assistance: OpenAI Codex was used for investigation, implementation and tests.
Issue Evaluation
Category: type/bug | Status: Confirmed
The reported issue has been verified against the current codebase on the develop branch.
Root Cause: In the BitsArray(byte[], int) constructor (line 62), the capacity check condition is inverted: if (bitLength < bytes.length * Byte.SIZE) throws IllegalArgumentException. This rejects valid cases where the logical bit length is within capacity, while accepting impossible cases where it exceeds capacity.
Impact: filter module — BitsArray.create(9).clone() fails because 9 < 16 triggers the inverted check. Meanwhile BitsArray.create(new byte[]{1}, 9) is accepted despite 9 bits exceeding 1-byte capacity, risking later ArrayIndexOutOfBoundsException.
Severity: Low-Medium — utility constructor, affects BloomFilter and any code cloning partial-byte BitsArray instances. No broker message-path impact since BloomFilter normally uses byte-aligned lengths.
Verified reproduction:
BitsArray.create(9).clone()→IllegalArgumentException: BitLength is less than bytes.length() * 8BitsArray.create(new byte[]{1}, 9).getBit(8)→ accepted but out of bounds
Changing < to > on line 62 resolves this. An automated fix proposal may be generated. Reply /approve to proceed with PR generation.
Automated evaluation by RockteMQ-AI