{"record":{"id":"aa84a08a5eb013c1","repo":"apache/cassandra","slug":"hash-of-size-d-encountered-expecting-d-or-d","errorCode":null,"errorMessage":"Hash of size %d encountered, expecting %d or %d","messagePattern":"Hash of size (.+?) encountered, expecting (.+?) or (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/utils/MerkleTree.java","lineNumber":1102,"sourceCode":"                xorOntoLeft(hash, partitionHash);\n\n            sizeOfRange += partitionSize;\n            partitionsInRange += 1;\n        }\n\n        static OnHeapLeaf deserializeWithoutIdent(DataInputPlus in) throws IOException\n        {\n            int size = in.readByte();\n            switch (size)\n            {\n                case HASH_SIZE:\n                    byte[] hash = new byte[HASH_SIZE];\n                    in.readFully(hash);\n                    return new OnHeapLeaf(hash);\n                case 0:\n                    return new OnHeapLeaf();\n                default:\n                    throw new IllegalStateException(format(\"Hash of size %d encountered, expecting %d or %d\", size, HASH_SIZE, 0));\n            }\n        }\n\n        int serializeOffHeap(ByteBuffer buffer, IPartitioner p)\n        {\n            if (buffer.remaining() < OffHeapLeaf.maxOffHeapSize())\n                throw new IllegalStateException(\"Insufficient remaining bytes to deserialize a Leaf node off-heap\");\n\n            if (hash.length != HASH_SIZE)\n                throw new IllegalArgumentException(\"Hash of unexpected size when serializing a Leaf off-heap: \" + hash.length);\n\n            final int position = buffer.position();\n            buffer.put(hash);\n            return ~position;\n        }\n\n        @Override\n        public String toString()","sourceCodeStart":1084,"sourceCodeEnd":1120,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/utils/MerkleTree.java#L1084-L1120","documentation":"When deserializing an on-heap MerkleTree leaf, the hash length is read first and must be either HASH_SIZE or 0 (empty leaf). Any other size triggers IllegalStateException('Hash of size %d encountered, expecting %d or %d'), meaning the serialized leaf is malformed or produced by an incompatible format.","triggerScenarios":"Reading an on-heap leaf whose length byte is neither 0 nor the fixed HASH_SIZE — corrupt stream, wrong read position, or a tree written with a different hash size.","commonSituations":"Repair streaming across Cassandra versions where the tree hash size changed; reading a corrupted or hand-modified MerkleTree payload; desynced deserialization of the tree stream.","solutions":["Verify all nodes use a Cassandra version with the same HASH_SIZE","Regenerate the MerkleTree by rerunning repair validation","Check the reported size value against corruption/truncation of the stream","Re-serialize from source data rather than re-reading the damaged payload"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    leaf = OnHeapLeaf.deserializeWithoutIdent(in);\n} catch (IllegalStateException e) {\n    if (e.getMessage().startsWith(\"Hash of size\")) {\n        logger.error(\"Malformed leaf hash length in tree stream: {}\", e.getMessage());\n        // treat tree as corrupt; request re-validation from the source node\n    }\n    throw e;\n}","preventionTips":["Match Cassandra versions across nodes (HASH_SIZE must agree)","Do not hand-edit or transform serialized MerkleTree payloads","Keep a single read path for the tree stream to avoid position desync","Re-serialize from data instead of re-reading damaged payloads"],"tags":["serialization","merkle-tree","repair","corruption"],"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"}