apache/cassandra · error · InvalidTypeException

Invalid bytes for inet value, got bytes

Error message

Invalid bytes for inet value, got  bytes

What it means

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.

Source

Thrown at src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java:1534

        }

        @Override
        public ByteBuffer serialize(InetAddress value, ProtocolVersion protocolVersion)
        {
            return value == null ? null : ByteBuffer.wrap(value.getAddress());
        }

        @Override
        public InetAddress deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion)
        {
            if (bytes == null || bytes.remaining() == 0) return null;
            try
            {
                return InetAddress.getByAddress(Bytes.getArray(bytes));
            }
            catch (UnknownHostException e)
            {
                throw new InvalidTypeException(
                "Invalid bytes for inet value, got " + bytes.remaining() + " bytes");
            }
        }
    }

    /**
     * This codec maps a CQL {@link DataType#tinyint()} to a Java {@link Byte}.
     */
    private static class TinyIntCodec extends PrimitiveByteCodec
    {

        private static final TinyIntCodec instance = new TinyIntCodec();

        private TinyIntCodec()
        {
            super(tinyint());
        }

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Verify the column type is 'inet' and the payload is 4 or 16 bytes; log bytes.remaining() to see the actual size.
  2. Use the codec matching the real type (e.g. BytesType/ByteBuffer for blobs, UUIDCodec for uuids).
  3. Fix upstream writers to store properly sized addresses.
  4. If deserializing from a custom format, pad/truncate appropriately only after confirming the source encoding.

Example fix

// before
InetAddress a = new InetCodec().deserialize(bytes, ProtocolVersion.V4);
// after
int n = bytes.remaining();
if (n != 4 && n != 16)
    throw new IllegalArgumentException("Expected 4 or 16 bytes for inet, got " + n);
InetAddress a = new InetCodec().deserialize(bytes, ProtocolVersion.V4);
Defensive patterns

Strategy: validation

Validate before calling

int n = bytes == null ? 0 : bytes.remaining();
if (n != 0 && n != 4 && n != 16)
    throw new IllegalArgumentException("inet needs 4 or 16 bytes, got " + n);

Type guard

boolean isInetSized(ByteBuffer b) { int n = b == null ? 0 : b.remaining(); return n == 0 || n == 4 || n == 16; }

Try / catch

try { return codec.deserialize(bytes, protocolVersion); }
catch (InvalidTypeException e) {
    log.warn("Inet deserialization failed, {} bytes", bytes.remaining());
    return null;
}

Prevention

When it happens

Trigger: 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.

Common situations: Schema mismatches after ALTER TYPE changes, storing raw byte blobs and misreading them as inet, or legacy migration data with non-standard address lengths.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/c32dc3736e2cd809. Report an issue: GitHub.