{"record":{"id":"3b3755cfd8f18485","repo":"apache/cassandra","slug":"expected-exactly-d-bytes-but-was-d","errorCode":null,"errorMessage":"Expected exactly %d bytes, but was %d","messagePattern":"Expected exactly (.+?) bytes, but was (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/db/marshal/AbstractType.java","lineNumber":587,"sourceCode":"\n    // This assumes that no empty values are passed\n    public void writeValue(ByteBuffer value, DataOutputPlus out) throws IOException\n    {\n        writeValue(value, ByteBufferAccessor.instance, out);\n    }\n\n    // This assumes that no empty values are passed\n    public  <V> void writeValue(V value, ValueAccessor<V> accessor, DataOutputPlus out) throws IOException\n    {\n        assert !isNull(value, accessor) : \"bytes should not be null for type \" + this;\n        int expectedValueLength = valueLengthIfFixed;\n        if (expectedValueLength >= 0)\n        {\n            int actualValueLength = accessor.size(value);\n            if (actualValueLength == expectedValueLength)\n                accessor.write(value, out);\n            else\n                throw new IOException(String.format(\"Expected exactly %d bytes, but was %d\",\n                                                    expectedValueLength, actualValueLength));\n        }\n        else\n        {\n            accessor.writeWithVIntLength(value, out);\n        }\n    }\n\n    public  <V> void writeValue(IndexedValueHolder<V> valueHolder, int i, ValueAccessor<V> accessor, DataOutputPlus out) throws IOException\n    {\n        assert !valueHolder.isNull(i) : \"bytes should not be null for type \" + this;\n        int expectedValueLength = valueLengthIfFixed;\n        if (expectedValueLength >= 0)\n        {\n            int actualValueLength = valueHolder.size(i);\n            if (actualValueLength == expectedValueLength)\n                accessor.write(valueHolder, i, out);\n            else","sourceCodeStart":569,"sourceCodeEnd":605,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/db/marshal/AbstractType.java#L569-L605","documentation":"AbstractType.writeValue serializes a value to an output stream; when the caller supplies an expectedValueLength, the actual byte-buffer size must match exactly. A mismatch means the caller's recorded length disagrees with the real serialized size, so the write is aborted with this IOException.","triggerScenarios":"Calling writeValue (directly or via writeValueSkippingNullAndEmpty) with expectedValueLength >= 0 that differs from value.remaining()/size — e.g. a pre-computed serialized size from stale or corrupted metadata.","commonSituations":"Streaming/mutation-forwarding code paths where sizes were computed before mutation; custom compaction or repair tooling copying values with wrong length headers; deserialized value mutated after size was captured.","solutions":["Pass expectedValueLength = -1 so the value is written with its actual (VInt-encoded) length.","Recompute the size with AbstractType.valueLength(value) / value.remaining() immediately before writing.","Check for value mutation (slicing, duplicate, reuse of ByteBuffers) between size computation and write."],"exampleFix":"// before\ntype.writeValue(value, out, staleSize);\n// after\ntype.writeValue(value, out, value.remaining()); // or -1 to write actual length","handlingStrategy":"try-catch","validationCode":"if (expectedValueLength >= 0 && value.remaining() != expectedValueLength) throw new IllegalStateException(\"length mismatch: \" + value.remaining() + \" != \" + expectedValueLength);","typeGuard":null,"tryCatchPattern":"catch (IOException e) { throw new StreamingException(\"value length mismatch while writing\", e); }","preventionTips":["Pass -1 as expectedValueLength unless the length is freshly computed.","Recompute value.remaining() immediately before writeValue.","Avoid mutating ByteBuffers after capturing their size."],"tags":["io","serialization","internal-invariant"],"backgroundTag":"internal-invariant-violation","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}