{"record":{"id":"451c7dfae75bdb13","repo":"apache/cassandra","slug":"expected-4-or-0-byte-value-for-a-float-d","errorCode":null,"errorMessage":"Expected 4 or 0 byte value for a float (%d)","messagePattern":"Expected 4 or 0 byte value for a float \\((.+?)\\)","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/serializers/FloatSerializer.java","lineNumber":46,"sourceCode":"    public static final FloatSerializer instance = new FloatSerializer();\n\n    public <V> Float deserialize(V value, ValueAccessor<V> accessor)\n    {\n        if (accessor.isEmpty(value))\n            return null;\n\n        return accessor.toFloat(value);\n    }\n\n    public ByteBuffer serialize(Float value)\n    {\n        return (value == null) ? ByteBufferUtil.EMPTY_BYTE_BUFFER : ByteBufferUtil.bytes(value);\n    }\n\n    public <V> void validate(V value, ValueAccessor<V> accessor) throws MarshalException\n    {\n        if (accessor.size(value) != 4 && !accessor.isEmpty(value))\n            throw new MarshalException(String.format(\"Expected 4 or 0 byte value for a float (%d)\", accessor.size(value)));\n    }\n\n    public String toString(Float value)\n    {\n        return value == null ? \"\" : String.valueOf(value);\n    }\n\n    public Class<Float> getType()\n    {\n        return Float.class;\n    }\n}\n","sourceCodeStart":28,"sourceCodeEnd":59,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/serializers/FloatSerializer.java#L28-L59","documentation":"FloatSerializer.validate() enforces that a serialized float occupies exactly 4 bytes (or 0 bytes for the empty-value case). Any other length means the bytes are not a valid 32-bit IEEE-754 float, so a MarshalException is thrown. This protects against deserializing garbage as a float.","triggerScenarios":"Calling FloatSerializer.validate(value, accessor) (directly or via a FloatType column validation) with a buffer whose size is not 4 and not empty, e.g. an 8-byte double or a 1-byte blob written into a float column.","commonSituations":"Application writes a Java double (8 bytes) or ByteBuffer of wrong length into a float column; a migration or ETL tool serializes with the wrong type; driver protocol mismatch writing blob bytes into float-typed columns; hand-rolled serialization writing an extra byte.","solutions":["Ensure the value is written as a 32-bit float: use FloatType serializers or ByteBufferUtil.bytes(float), not bytes(double).","Check the client-side field type; convert double to float before writing if the column is float.","Fix the column type in the schema (double vs float) to match what the application actually writes.","Validate the byte length before writing: buffer.remaining() must be 4 (or 0 for empty)."],"exampleFix":"// before\nByteBuffer bytes = ByteBuffer.allocate(8).putDouble(3.14d);\n// after\nByteBuffer bytes = ByteBuffer.allocate(4).putFloat((float) 3.14d);","handlingStrategy":"validation","validationCode":"if (buf != null && buf.remaining() != 4 && buf.remaining() != 0) throw new IllegalArgumentException(\"float needs exactly 4 bytes, got \" + buf.remaining());","typeGuard":"boolean isValidFloatBytes(java.nio.ByteBuffer b) { return b == null || b.remaining() == 0 || b.remaining() == 4; }","tryCatchPattern":"try { floatSerializer.validate(buf, accessor); } catch (org.apache.cassandra.exceptions.MarshalException e) { log.error(\"invalid float bytes: {}\", e.getMessage()); throw new IllegalArgumentException(e); }","preventionTips":["Always serialize floats via the type's serializer or putFloat, never putDouble","Add a remaining()==4 assertion in any custom serialization pipeline","Keep column type (float vs double) in sync with the application field type"],"tags":["cassandra","serialization","type-validation","float"],"backgroundTag":"invalid-argument-format","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}