{"record":{"id":"b7ff57f46951a23a","repo":"apache/cassandra","slug":"checksum-is-larger-than-32-bytes","errorCode":null,"errorMessage":"Checksum is larger than 32 bytes!","messagePattern":"Checksum is larger than 32 bytes!","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/io/util/Checksumed.java","lineNumber":39,"sourceCode":"import java.util.zip.Checksum;\n\npublic interface Checksumed\n{\n    Checksum checksum();\n\n    default long getAndResetChecksum()\n    {\n        Checksum c = checksum();\n        long v = c.getValue();\n        c.reset();\n        return v;\n    }\n\n    default int getValue32()\n    {\n        long v = checksum().getValue();\n        if (Long.numberOfLeadingZeros(v) < 32)\n            throw new IllegalStateException(\"Checksum is larger than 32 bytes!\");\n        return (int) v;\n    }\n\n    default int getValue32AndResetChecksum()\n    {\n        Checksum c = checksum();\n        long v = c.getValue();\n        if (Long.numberOfLeadingZeros(v) < 32)\n            throw new IllegalStateException(\"Checksum is larger than 32 bytes!\");\n        c.reset();\n        return (int) v;\n    }\n\n    default void resetChecksum()\n    {\n        checksum().reset();\n    }\n}","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/io/util/Checksumed.java#L21-L57","documentation":"getValue32 is a helper on Checksumed wrappers (e.g. CRC32-based checksums embedded in I/O streams) that returns the checksum truncated to an int. If the checksum value exceeds 32 bits (any bit set in the upper 32 bits) it cannot fit, so an IllegalStateException is thrown instead of silently truncating.","triggerScenarios":"Calling getValue32() on a Checksum implementation whose current accumulated value exceeds Integer range — only possible with a non-standard checksum algorithm (CRC32 variants normally fit in 32 bits); indicates the wrong Checksum instance was plugged in.","commonSituations":"Custom Checksum implementations (e.g. CRC64, xxHash-64) wired into a stream that expects a 32-bit CRC, misuse of the Checksumed API in custom tooling.","solutions":["Use a 32-bit checksum (java.util.zip.CRC32 / Adler32) wherever getValue32 is consumed","Mask to 32 bits explicitly if truncation is intended: (int) checksum().getValue()","Or use the full long via checksum().getValue() instead of getValue32"],"exampleFix":"// before\nint v = checksumed.getValue32(); // throws for 64-bit checksum\n// after\nlong v = checksumed.checksum().getValue(); int v32 = (int) v; // explicit truncate","handlingStrategy":"type-guard","validationCode":"long v = checksum().getValue(); if (Long.numberOfLeadingZeros(v) < 32) switchToLongChecksumPath(v);","typeGuard":"static boolean fitsInt(Checksum c) { return Long.numberOfLeadingZeros(c.getValue()) >= 32; }","tryCatchPattern":"try { int v = checksumed.getValue32(); } catch (IllegalStateException e) { long v = checksumed.checksum().getValue(); /* handle 64-bit */ }","preventionTips":["Use CRC32/Adler32 wherever the 32-bit API is consumed","Assert checksum width in unit tests for custom Checksum implementations","Prefer reading the full long value when unsure"],"tags":["io","checksum","internal"],"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"}