{"record":{"id":"e5f3873c385b1854","repo":"apache/cassandra","slug":"invalid-size-for-a-counter-context","errorCode":null,"errorMessage":"Invalid size for a counter context","messagePattern":"Invalid size for a counter context","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/db/context/CounterContext.java","lineNumber":687,"sourceCode":"        accessor.putShort(cleared, 0, (short) globalShardIndexes.size());\n        for (int i = 0; i < globalShardIndexes.size(); i++)\n            accessor.putShort(cleared, HEADER_SIZE_LENGTH + i * HEADER_ELT_LENGTH, globalShardIndexes.get(i));\n\n        int origHeaderLength = headerLength(context, accessor);\n        accessor.copyTo(context,\n                        origHeaderLength,\n                        cleared,\n                        accessor,\n                        headerLength(cleared, accessor),\n                        accessor.size(context) - origHeaderLength);\n\n        return cleared;\n    }\n\n    public <V> void validateContext(V context, ValueAccessor<V> accessor) throws MarshalException\n    {\n        if ((accessor.size(context) - headerLength(context, accessor)) % STEP_LENGTH != 0)\n            throw new MarshalException(\"Invalid size for a counter context\");\n    }\n\n    /**\n     * Returns the clock and the count associated with the local counter id, or (0, 0) if no such shard is present.\n     */\n    public ClockAndCount getLocalClockAndCount(ByteBuffer context)\n    {\n        return getClockAndCountOf(context, CounterId.getLocalId());\n    }\n\n    /**\n     * Returns the count associated with the local counter id, or 0 if no such shard is present.\n     */\n    public long getLocalCount(ByteBuffer context)\n    {\n        return getLocalClockAndCount(context).count;\n    }\n","sourceCodeStart":669,"sourceCodeEnd":705,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/db/context/CounterContext.java#L669-L705","documentation":"MarshalException raised by CounterContext.validateContext when a serialized counter context blob's size, after removing its header, is not a multiple of STEP_LENGTH. Counter contexts are sequences of fixed-size shards, so any other length means the bytes are corrupt or were not produced by the counter context serializer.","triggerScenarios":"Reading or validating a counter column whose serialized bytes were truncated, manually crafted, corrupted on disk, or produced by an incompatible/buggy writer.","commonSituations":"Corrupt SSTable or commit log segments, hand-editing serialized values, deserializing non-counter data as counters, data damage from failed hardware or bad migration tooling.","solutions":["Repair/scrub the affected table (nodetool scrub) to drop or fix corrupt counter cells","Restore the corrupted data from backup or an intact replica","Ensure reads use the counter type, not a blob cast of unrelated bytes"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"boolean validCounterContext(ByteBuffer buf) { return (buf.remaining() - CounterContext.instance().headerLength(buf)) % 16 == 0; }","typeGuard":null,"tryCatchPattern":"try { CounterContext.instance().validateContext(ctx, accessor); } catch (MarshalException e) { repairOrScrub(keyspace, table); }","preventionTips":["Never hand-edit serialized counter bytes","Run regular repairs and scrub corrupted SSTables","Ensure all writers serialize counters through the official serializer"],"tags":["cassandra","counters","corruption","serialization"],"backgroundTag":"schema-validation-failed","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"}