{"record":{"id":"2b4084b701b9cfea","repo":"apache/cassandra","slug":"expected-4-or-16-byte-inetaddress-got-s","errorCode":null,"errorMessage":"Expected 4 or 16 byte inetaddress; got %s","messagePattern":"Expected 4 or 16 byte inetaddress; got (.+?)","errorType":"validation","errorClass":"MarshalException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/serializers/InetAddressSerializer.java","lineNumber":63,"sourceCode":"    }\n\n    public ByteBuffer serialize(InetAddress value)\n    {\n        return value == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : ByteBuffer.wrap(value.getAddress());\n    }\n\n    public <V> void validate(V value, ValueAccessor<V> accessor) throws MarshalException\n    {\n        if (accessor.isEmpty(value))\n            return;\n\n        try\n        {\n            InetAddress.getByAddress(accessor.toArray(value));\n        }\n        catch (UnknownHostException e)\n        {\n            throw new MarshalException(String.format(\"Expected 4 or 16 byte inetaddress; got %s\", accessor.toHex(value)));\n        }\n    }\n\n    public String toString(InetAddress value)\n    {\n        return value == null ? \"\" : value.getHostAddress();\n    }\n\n    public Class<InetAddress> getType()\n    {\n        return InetAddress.class;\n    }\n\n    @Override\n    public boolean shouldQuoteCQLLiterals()\n    {\n        return true;\n    }","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/serializers/InetAddressSerializer.java#L45-L81","documentation":"InetAddressSerializer.validate() attempts InetAddress.getByAddress() on the raw bytes and converts any UnknownHostException into a MarshalException. Java only accepts 4-byte (IPv4) or 16-byte (IPv6) arrays as raw addresses, so any other length fails. This guarantees an inet column only holds well-formed IP addresses.","triggerScenarios":"Calling InetAddressSerializer.validate(value, accessor) (directly or via an InetType column) with bytes that are not 4 or 16 bytes long, or otherwise rejected by InetAddress.getByAddress (e.g. truncated buffer, a hostname string's bytes instead of address bytes).","commonSituations":"Writing a hostname string (e.g. \"example.com\") as bytes into an inet column instead of a resolved IP; writing an IPv6 address with a wrong/abbreviated byte length; ETL tools exporting addresses as text without resolution; client library serializing InetSocketAddress including port bytes.","solutions":["Resolve hostnames to an IP first (InetAddress.getByName) and serialize the resulting address bytes (4 or 16 bytes) rather than the hostname text.","Verify the byte length before writing: exactly 4 for IPv4 or 16 for IPv6.","Do not include port or scope bytes; serialize only InetAddress.getAddress().","If textual data is intended, change the column type to text instead of inet."],"exampleFix":"// before\nByteBuffer bytes = ByteBuffer.wrap(\"example.com\".getBytes());\n// after\nInetAddress addr = InetAddress.getByName(\"example.com\");\nByteBuffer bytes = ByteBuffer.wrap(addr.getAddress());","handlingStrategy":"validation","validationCode":"byte[] a = addr.getAddress(); if (a.length != 4 && a.length != 16) throw new IllegalArgumentException(\"inet needs 4 or 16 bytes\");","typeGuard":"boolean isValidInetBytes(java.net.InetAddress a) { byte[] b = a.getAddress(); return b.length == 4 || b.length == 16; }","tryCatchPattern":"try { inetType.validate(buf, accessor); } catch (org.apache.cassandra.exceptions.MarshalException e) { throw new IllegalArgumentException(\"bad inet value: \" + e.getMessage(), e); }","preventionTips":["Resolve hostnames with InetAddress.getByName before serializing; never write hostname text as bytes","Serialize InetAddress.getAddress() only — no ports, no textual forms","Unit-test serialization round-trips for both IPv4 and IPv6 values"],"tags":["cassandra","serialization","inet","type-validation"],"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"}