{"record":{"id":"44ed9ca9fa9a7572","repo":"apache/cassandra","slug":"the-duration-days-must-be-a-32-bits-integer-but-wa","errorCode":null,"errorMessage":"The duration days must be a 32 bits integer but was: %d","messagePattern":"The duration days must be a 32 bits integer but was: (.+?)","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/serializers/DurationSerializer.java","lineNumber":95,"sourceCode":"        }\n    }\n\n    public <V> void validate(V value, ValueAccessor<V> accessor) throws MarshalException\n    {\n        if (accessor.size(value) < 3)\n            throw new MarshalException(String.format(\"Expected at least 3 bytes for a duration (%d)\", accessor.size(value)));\n\n        try (DataInputBuffer in = new DataInputBuffer(accessor.toBuffer(value), true))  // FIXME: value input buffer\n        {\n            long monthsAsLong = in.readVInt();\n            long daysAsLong = in.readVInt();\n            long nanoseconds = in.readVInt();\n\n            if (!canBeCastToInt(monthsAsLong))\n                throw new MarshalException(String.format(\"The duration months must be a 32 bits integer but was: %d\",\n                                                         monthsAsLong));\n            if (!canBeCastToInt(daysAsLong))\n                throw new MarshalException(String.format(\"The duration days must be a 32 bits integer but was: %d\",\n                                                         daysAsLong));\n            int months = (int) monthsAsLong;\n            int days = (int) daysAsLong;\n\n            if (!((months >= 0 && days >= 0 && nanoseconds >= 0) || (months <= 0 && days <=0 && nanoseconds <=0)))\n                throw new MarshalException(String.format(\"The duration months, days and nanoseconds must be all of the same sign (%d, %d, %d)\",\n                                                         months, days, nanoseconds));\n        }\n        catch (IOException e)\n        {\n            // this should never happen with a DataInputBuffer\n            throw new AssertionError(\"Unexpected error\", e);\n        }\n    }\n\n    /**\n     * Checks that the specified {@code long} can be cast to an {@code int} without information lost.\n     *","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/serializers/DurationSerializer.java#L77-L113","documentation":"MarshalException raised by DurationSerializer.validate after decoding the three VInt components of a duration: the days component exceeds the 32-bit int range that the duration type guarantees (enforced by canBeCastToInt). The value being read was not written by a valid duration encoder or is corrupt.","triggerScenarios":"validate/deserialize of a duration whose days vint decodes to a value outside int range — corrupted bytes or hand-encoded vints with oversized day counts.","commonSituations":"Storage corruption; custom serialization writing days as a long vint beyond int bounds; malformed user input converted without range checks.","solutions":["Construct durations with Duration.newInstance ensuring days fits an int","Re-serialize via DurationSerializer rather than manual vint packing","Verify integrity of stored bytes if corruption is suspected","Validate user-provided durations before serializing"],"exampleFix":"// before\nlong days = 1L << 40; byte[] packed = packVInt(days); // out of int range\n// after\nDuration d = Duration.newInstance(0, (int) 30, 0);\nByteBuffer ok = DurationSerializer.instance.serialize(d);","handlingStrategy":"validation","validationCode":"public static boolean daysFitsInt(long days) {\n    return days >= Integer.MIN_VALUE && days <= Integer.MAX_VALUE;\n}","typeGuard":"public static boolean isSafeDayCount(long days) {\n    return daysFitsInt(days);\n}","tryCatchPattern":"try {\n    serializer.validate(buffer, accessor);\n} catch (MarshalException e) {\n    if (e.getMessage().contains(\"days must be a 32 bits\")) logger.error(\"days overflow\");\n}","preventionTips":["Compute day offsets with int-safe arithmetic before newInstance","Validate user input day counts","Avoid hand-encoded vints"],"tags":["serialization","cassandra","duration","value-out-of-range"],"backgroundTag":"value-out-of-range","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"}