{"record":{"id":"c32dc3736e2cd809","repo":"apache/cassandra","slug":"invalid-bytes-for-inet-value-got-bytes","errorCode":null,"errorMessage":"Invalid bytes for inet value, got  bytes","messagePattern":"Invalid bytes for inet value, got  bytes","errorType":"validation","errorClass":"InvalidTypeException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java","lineNumber":1534,"sourceCode":"        }\n\n        @Override\n        public ByteBuffer serialize(InetAddress value, ProtocolVersion protocolVersion)\n        {\n            return value == null ? null : ByteBuffer.wrap(value.getAddress());\n        }\n\n        @Override\n        public InetAddress deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion)\n        {\n            if (bytes == null || bytes.remaining() == 0) return null;\n            try\n            {\n                return InetAddress.getByAddress(Bytes.getArray(bytes));\n            }\n            catch (UnknownHostException e)\n            {\n                throw new InvalidTypeException(\n                \"Invalid bytes for inet value, got \" + bytes.remaining() + \" bytes\");\n            }\n        }\n    }\n\n    /**\n     * This codec maps a CQL {@link DataType#tinyint()} to a Java {@link Byte}.\n     */\n    private static class TinyIntCodec extends PrimitiveByteCodec\n    {\n\n        private static final TinyIntCodec instance = new TinyIntCodec();\n\n        private TinyIntCodec()\n        {\n            super(tinyint());\n        }\n","sourceCodeStart":1516,"sourceCodeEnd":1552,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java#L1516-L1552","documentation":"Thrown by InetCodec.deserialize when converting the raw bytes to an InetAddress via InetAddress.getByAddress fails with UnknownHostException. This happens when the byte array length is not 4 (IPv4) or 16 (IPv6), since Java only accepts those lengths as raw IP addresses.","triggerScenarios":"Calling InetCodec.deserialize(bytes, protocolVersion) with a ByteBuffer whose remaining byte count is not 4 or 16, e.g. deserializing a varint, uuid, or truncated value as inet.","commonSituations":"Schema mismatches after ALTER TYPE changes, storing raw byte blobs and misreading them as inet, or legacy migration data with non-standard address lengths.","solutions":["Verify the column type is 'inet' and the payload is 4 or 16 bytes; log bytes.remaining() to see the actual size.","Use the codec matching the real type (e.g. BytesType/ByteBuffer for blobs, UUIDCodec for uuids).","Fix upstream writers to store properly sized addresses.","If deserializing from a custom format, pad/truncate appropriately only after confirming the source encoding."],"exampleFix":"// before\nInetAddress a = new InetCodec().deserialize(bytes, ProtocolVersion.V4);\n// after\nint n = bytes.remaining();\nif (n != 4 && n != 16)\n    throw new IllegalArgumentException(\"Expected 4 or 16 bytes for inet, got \" + n);\nInetAddress a = new InetCodec().deserialize(bytes, ProtocolVersion.V4);","handlingStrategy":"validation","validationCode":"int n = bytes == null ? 0 : bytes.remaining();\nif (n != 0 && n != 4 && n != 16)\n    throw new IllegalArgumentException(\"inet needs 4 or 16 bytes, got \" + n);","typeGuard":"boolean isInetSized(ByteBuffer b) { int n = b == null ? 0 : b.remaining(); return n == 0 || n == 4 || n == 16; }","tryCatchPattern":"try { return codec.deserialize(bytes, protocolVersion); }\ncatch (InvalidTypeException e) {\n    log.warn(\"Inet deserialization failed, {} bytes\", bytes.remaining());\n    return null;\n}","preventionTips":["Verify column type is inet before decoding.","Never decode blobs/uuids/varints with InetCodec.","Check schema consistency across environments.","Log byte length on failure."],"tags":["serialization","inet","bytebuffer","deserialization"],"backgroundTag":"type-mismatch","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"}