{"record":{"id":"b13831deef5b0c25","repo":"apache/pulsar","slug":"bitindex-0-bitindex","errorCode":null,"errorMessage":"bitIndex < 0: <bitIndex>","messagePattern":"bitIndex < 0: <bitIndex>","errorType":"validation","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/BitSetRecyclable.java","lineNumber":357,"sourceCode":"            throw new IndexOutOfBoundsException(\"fromIndex < 0: \" + fromIndex);\n        if (toIndex < 0)\n            throw new IndexOutOfBoundsException(\"toIndex < 0: \" + toIndex);\n        if (fromIndex > toIndex)\n            throw new IndexOutOfBoundsException(\"fromIndex: \" + fromIndex +\n                \" > toIndex: \" + toIndex);\n    }\n\n    /**\n     * Sets the bit at the specified index to the complement of its\n     * current value.\n     *\n     * @param  bitIndex the index of the bit to flip\n     * @throws IndexOutOfBoundsException if the specified index is negative\n     * @since  1.4\n     */\n    public void flip(int bitIndex) {\n        if (bitIndex < 0)\n            throw new IndexOutOfBoundsException(\"bitIndex < 0: \" + bitIndex);\n\n        int wordIndex = wordIndex(bitIndex);\n        expandTo(wordIndex);\n\n        words[wordIndex] ^= (1L << bitIndex);\n\n        recalculateWordsInUse();\n        checkInvariants();\n    }\n\n    /**\n     * Sets each bit from the specified {@code fromIndex} (inclusive) to the\n     * specified {@code toIndex} (exclusive) to the complement of its current\n     * value.\n     *\n     * @param  fromIndex index of the first bit to flip\n     * @param  toIndex index after the last bit to flip\n     * @throws IndexOutOfBoundsException if {@code fromIndex} is negative,","sourceCodeStart":339,"sourceCodeEnd":375,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/BitSetRecyclable.java#L339-L375","documentation":"BitSetRecyclable.flip(int bitIndex) requires a non-negative bit index; negative indices cannot map to a valid word/bit position, so the method throws IndexOutOfBoundsException immediately. This matches java.util.BitSet behavior. It is a caller argument error, not internal state corruption.","triggerScenarios":"Calling flip() with a negative bitIndex, typically a result of an uninitialized variable, an unset array lookup returning -1 (e.g. indexOf-style probes), or an arithmetic underflow computing the index.","commonSituations":"Using the result of a lookup that returns -1 for 'not found' directly as a bit index; batch index acknowledgment where a computed batch index went negative; integer overflow when multiplying message position components.","solutions":["Inspect where the negative index originates and fix the computation or lookup that produced -1/underflow","Guard the call: only flip when bitIndex >= 0","Validate protocol/batch-index values before they reach the bit set","Add a test for negative or 'not found' sentinel inputs"],"exampleFix":"// before\nint idx = findBatchIndex(msg); // may return -1\nbitSet.flip(idx);\n// after\nint idx = findBatchIndex(msg);\nif (idx >= 0) {\n    bitSet.flip(idx);\n}","handlingStrategy":"validation","validationCode":"if (bitIndex < 0) {\n    throw new IllegalArgumentException(\"bitIndex must be >= 0, got \" + bitIndex);\n}\nbitSet.flip(bitIndex);","typeGuard":"static boolean isValidBitIndex(int idx) { return idx >= 0; }","tryCatchPattern":"try {\n    bitSet.flip(bitIndex);\n} catch (IndexOutOfBoundsException e) {\n    LOG.warn(\"negative bitIndex {} passed to flip\", bitIndex, e);\n}","preventionTips":["Never use -1-returning lookup results directly as bit indices","Check for integer overflow/underflow in index arithmetic","Guard with idx >= 0 before mutating calls","Test sentinel inputs"],"tags":["java","bitset","index-out-of-bounds","negative-index"],"backgroundTag":"index-out-of-bounds","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}