apache/cassandra · error · CorruptSSTableException

Invalid negative chunk index %d with position %d

Error message

Invalid negative chunk index %d with position %d

What it means

chunkFor(position) computes a chunk index as 8 * (position / chunkLength) into the offset array. A negative index means the requested file position was negative, which is impossible for valid data; the writer treats it as corruption of the compressed file and throws CorruptSSTableException with the position and computed index. A too-large index similarly throws (idx >= chunkOffsetsSize).

Source

Thrown at src/java/org/apache/cassandra/io/compress/CompressionMetadata.java:338

        }
    }

    /**
     * Get a chunk of compressed data (offset, length) corresponding to given position
     *
     * @param position Position in the file.
     * @return pair of chunk offset and length.
     */
    public Chunk chunkFor(long position)
    {
        // position of the chunk
        long idx = 8 * (position / parameters.chunkLength());

        if (idx >= chunkOffsetsSize)
            throw new CorruptSSTableException(new EOFException(), chunksIndexFile);

        if (idx < 0)
            throw new CorruptSSTableException(new IllegalArgumentException(String.format("Invalid negative chunk index %d with position %d", idx, position)),
                                              chunksIndexFile);

        long chunkOffset = chunkOffsets.getLong(idx);
        long nextChunkOffset = (idx + 8 == chunkOffsetsSize)
                                ? compressedFileLength
                                : chunkOffsets.getLong(idx + 8);

        return new Chunk(chunkOffset, (int) (nextChunkOffset - chunkOffset - 4)); // "4" bytes reserved for checksum
    }

    public long getDataOffsetForChunkOffset(long chunkOffset)
    {
        long l = 0;
        long h = (chunkOffsetsSize >> 3) - 1;
        long idx, offset;

        while (l <= h)
        {

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Validate the file length and positions with sstablemetadata / sstable validate on the SSTable
  2. Scrub or discard the corrupt SSTable and rebuild data via nodetool repair
  3. Compare the compression index contents against the data file size on disk
  4. If seen programmatically, assert position >= 0 before querying CompressionMetadata.chunkFor

Example fix

// before
CompressionMetadata.Chunk c = metadata.chunkFor(suspectPosition); // throws if negative
// after
if (suspectPosition < 0 || suspectPosition >= metadata.compressedFileLength)
    throw new IllegalArgumentException("position out of range: " + suspectPosition);
CompressionMetadata.Chunk c = metadata.chunkFor(suspectPosition);
Defensive patterns

Strategy: validation

Validate before calling

// Java: range-check position before chunkFor
static boolean isValidPosition(CompressionMetadata md, long pos) {
    return pos >= 0 && pos < md.compressedFileLength;
}

Type guard

static boolean safeChunkFor(CompressionMetadata md, long pos) {
    return md != null && pos >= 0 && pos < md.compressedFileLength;
}

Try / catch

try { return metadata.chunkFor(pos); }
catch (CorruptSSTableException e) { validateSSTable(path); throw e; }

Prevention

When it happens

Trigger: Calling chunkFor (directly or via chunk/upperChunkEnd/lowerChunkStart) with a negative position — typically from a corrupted truncated-file length, a bogus lastFlushOffset/bufferOffset, or a corrupt CompressionMetadata feeding invalid positions.

Common situations: Underlying data corruption producing negative file offsets; bugs or corruption in stored metadata (compressedFileLength/offsets) after a crash; passing a manually reconstructed position from a damaged file.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/034613e85cfb4259. Report an issue: GitHub.