apache/cassandra · error · IOException

Corrupt (negative) value length encountered

Error message

Corrupt (negative) value length encountered

What it means

AbstractType.read deserializes a value from a DataInputPlus. When the length is VInt-encoded, it is read as an unsigned 32-bit value; if that still decodes negative, the input stream is corrupt, and this IOException is thrown rather than attempting an impossible allocation/read.

Source

Thrown at src/java/org/apache/cassandra/db/marshal/AbstractType.java:661

        return read(ByteBufferAccessor.instance, in, maxValueSize);
    }

    public byte[] readArray(DataInputPlus in, int maxValueSize) throws IOException
    {
        return read(ByteArrayAccessor.instance, in, maxValueSize);
    }

    public <V> V read(ValueAccessor<V> accessor, DataInputPlus in, int maxValueSize) throws IOException
    {
        int length = valueLengthIfFixed;

        if (length >= 0)
            return accessor.read(in, length);
        else
        {
            int l = in.readUnsignedVInt32();
            if (l < 0)
                throw new IOException("Corrupt (negative) value length encountered");

            if (l > maxValueSize)
                throw new IOException(String.format("Corrupt value length %d encountered, as it exceeds the maximum of %d, " +
                                                    "which is set via max_value_size in cassandra.yaml",
                                                    l, maxValueSize));

            return accessor.read(in, l);
        }
    }

    public void skipValue(DataInputPlus in) throws IOException
    {
        int length = valueLengthIfFixed;
        if (length >= 0)
            in.skipBytesFully(length);
        else
            ByteBufferUtil.skipWithVIntLength(in);
    }

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Verify integrity of the source data (checksums, scrub) and re-read from a good replica.
  2. Ensure reader and writer use the same protocol version / length encoding (readWithVIntLength vs readLength).
  3. Re-sync the stream or drop the connection if framing is misaligned; cannot recover mid-stream.

Example fix

// ensure matched encoding
// before: reading a fixed-length stream with the VInt reader
Value v = type.readWithVIntLength(in);
// after
Value v = type.readBuffer(in, headerLength);
Defensive patterns

Strategy: try-catch

Try / catch

catch (IOException e) { log.error("corrupt value length in input stream", e); markCorrupt(streamSource); throw new CorruptDataException(e); }

Prevention

When it happens

Trigger: Calling read/readBuffer/readArray on a stream whose encoded VInt length decodes to a negative int — i.e. corrupted or misaligned serialization data where the length field bytes are not a valid unsigned VInt32.

Common situations: Reading from truncated or corrupted SSTables/commitlog segments; network desynchronization on streaming/messaging connections; a writer and reader disagreeing on the framing format (fixed vs VInt length).

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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