{"record":{"id":"3c0ed0eb4dc91525","repo":"apache/cassandra","slug":"eof-after-copied-bytes-out-of-len","errorCode":null,"errorMessage":"EOF after \" + copied + \" bytes out of \" + len","messagePattern":"EOF after \" \\+ copied \\+ \" bytes out of \" \\+ len","errorType":"exception","errorClass":"EOFException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/io/util/RebufferingInputStream.java","lineNumber":128,"sourceCode":"    {\n        int offset = dst.position();\n        int len = dst.limit() - offset;\n\n        int copied = 0;\n        while (copied < len)\n        {\n            int position = buffer.position();\n            int remaining = buffer.limit() - position;\n\n            if (remaining == 0)\n            {\n                reBuffer();\n\n                position = buffer.position();\n                remaining = buffer.limit() - position;\n\n                if (remaining == 0)\n                    throw new EOFException(\"EOF after \" + copied + \" bytes out of \" + len);\n            }\n\n            int toCopy = min(len - copied, remaining);\n            FastByteOperations.copy(buffer, position, dst, offset + copied, toCopy);\n            buffer.position(position + toCopy);\n            copied += toCopy;\n        }\n    }\n\n    @DontInline\n    protected long readPrimitiveSlowly(int bytes) throws IOException\n    {\n        long result = 0;\n        for (int i = 0; i < bytes; i++)\n            result = (result << 8) | (readByte() & 0xFFL);\n        return result;\n    }\n","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/io/util/RebufferingInputStream.java#L110-L146","documentation":"The bulk variant RebufferingInputStream.readFully(DataOutputBuffer dst/byte[], off, len) copies bytes across rebuffer() cycles until it has all len bytes; when a rebuffer yields zero remaining bytes before the request is satisfied it throws EOFException('EOF after <copied> bytes out of <len>'). It means the stream ended mid-read, i.e. truncated data.","triggerScenarios":"Reading a record larger than what physically remains in the stream — reading past the last entry of a commit-log segment, deserializing a partition whose declared size exceeds the file, or copying a blob whose length field is corrupt so the copy loop hits EOF partway.","commonSituations":"Corrupted length prefixes in sstable/commit-log data causing oversized reads; truncated files from unclean shutdown or failed disk; reading a file still being written; version mismatch making the reader expect more bytes than the writer produced.","solutions":["Sanity-check the declared length against remaining file bytes before the bulk readFully","Try-catch EOFException and treat the record as corrupt/truncated — stop or resync per protocol","Verify the data file (sstableverify/scrub, commit log replay truncation handling) to locate the corruption","Ensure readers only see fully-synced files and match serialization versions between reader and writer"],"exampleFix":"// before\ndob = new DataOutputBuffer();\nin.readFully(dob, (int) declaredSize); // EOF mid-copy on corrupt length\n// after\nlong readable = fileLength - in.getPosition();\nif (declaredSize > readable) {\n    throw new CorruptFileException(\"declared size \" + declaredSize + \" exceeds remaining \" + readable);\n}\nin.readFully(dob, (int) declaredSize);","handlingStrategy":"try-catch","validationCode":"long readable = fileLength - currentPosition; if (declaredSize > readable) { throw new CorruptFileException(\"size \" + declaredSize + \" > remaining \" + readable); }","typeGuard":"boolean fits = declaredSize >= 0 && declaredSize <= (fileLength - currentPosition);","tryCatchPattern":"try { in.readFully(dob, len); } catch (EOFException e) { logger.warn(\"Truncated record: \" + e.getMessage()); /* mark corrupt, resync */ }","preventionTips":["Validate length prefixes against file size before bulk reads","Catch EOFException at record boundaries and resynchronize","Run sstableverify/scrub on suspicious files","Keep serialization versions in sync between writer and reader"],"tags":["io","eof","deserialization"],"backgroundTag":"unexpected-eof","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"}