{"id":"c1384aacb1d301b1","repo":"apache/kafka","slug":"about-value-value-does-not-fit-in-a-16-bit-c1384a","errorCode":null,"errorMessage":"${about}: value ${value} does not fit in a 16-bit unsigned integer.","messagePattern":"(.+?): value (.+?) does not fit in a 16-bit unsigned integer\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/common/protocol/MessageUtil.java","lineNumber":98,"sourceCode":"            throw new RuntimeException(about + \": value \" + value +\n                \" does not fit in an 8-bit signed integer.\");\n        }\n        return (byte) value;\n    }\n\n    public static short jsonNodeToShort(JsonNode node, String about) {\n        int value = jsonNodeToInt(node, about);\n        if ((value < Short.MIN_VALUE) || (value > Short.MAX_VALUE)) {\n            throw new RuntimeException(about + \": value \" + value +\n                \" does not fit in a 16-bit signed integer.\");\n        }\n        return (short) value;\n    }\n\n    public static int jsonNodeToUnsignedShort(JsonNode node, String about) {\n        int value = jsonNodeToInt(node, about);\n        if (value < 0 || value > UNSIGNED_SHORT_MAX) {\n            throw new RuntimeException(about + \": value \" + value +\n                \" does not fit in a 16-bit unsigned integer.\");\n        }\n        return value;\n    }\n\n    public static long jsonNodeToUnsignedInt(JsonNode node, String about) {\n        long value = jsonNodeToLong(node, about);\n        if (value < 0 || value > UNSIGNED_INT_MAX) {\n            throw new RuntimeException(about + \": value \" + value +\n                    \" does not fit in a 32-bit unsigned integer.\");\n        }\n        return value;\n    }\n\n    public static int jsonNodeToInt(JsonNode node, String about) {\n        if (node.isInt()) {\n            return node.asInt();\n        }","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/common/protocol/MessageUtil.java#L80-L116","documentation":"Thrown by MessageUtil.jsonNodeToUnsignedShort as a RuntimeException when the parsed integer is < 0 or > UNSIGNED_SHORT_MAX (65535). It guards JSON-driven deserialization of a protocol field typed as an unsigned 16-bit integer; values outside [0, 65535] cannot be stored losslessly in a Java int-as-unsigned-short and are rejected. Used by message fields that pack a 16-bit unsigned quantity (e.g. some api-key/size-like fields).","triggerScenarios":"A JSON field mapped to an unsigned-short protocol message field receives a negative integer or a value > 65535; jsonNodeToInt parses it and the bounds check throws.","commonSituations":"Passing a signed-negative or overly large port/size/id via JSON tooling; assuming the field is a 32-bit int when the protocol declares it uint16; typo producing a five/six-digit value beyond 65535.","solutions":["Find the field named in 'about' and bring its value into [0, 65535].","If the value is legitimately larger, change the field's target type (e.g. to int) in the message definition and regenerate the protocol classes.","For negative inputs, check whether the upstream producer of the JSON mishandled a signed/unsigned conversion."],"exampleFix":"// before\n{ \"clientId\": 70000 }\n\n// after\n{ \"clientId\": 12 }","handlingStrategy":"validation","validationCode":"// MessageUtil.jsonNodeToUnsignedShort requires [0, 65535].\nint value = node.asInt();\nif (value < 0 || value > MessageUtil.UNSIGNED_SHORT_MAX) {\n    throw new IllegalArgumentException(about + \": value \" + value + \" outside uint16 range [0,65535]\");\n}\nint u16 = MessageUtil.jsonNodeToUnsignedShort(node, about);","typeGuard":"static boolean isValidUnsignedShort(int v) {\n    return v >= 0 && v <= MessageUtil.UNSIGNED_SHORT_MAX; // 65535\n}","tryCatchPattern":"try {\n    int u16 = MessageUtil.jsonNodeToUnsignedShort(node, about);\n} catch (RuntimeException e) {\n    log.error(\"{}: uint16 field out of range\", about, e);\n    rejectMessage(about, e);\n}","preventionTips":["Unsigned 16-bit fields must be in [0, 65535]; reject negatives explicitly at the JSON boundary.","Typical Kafka uint16 fields: port numbers, request counts; validate against this range in your schema.","Use MessageUtil.UNSIGNED_SHORT_MAX as the upper bound constant rather than hardcoding 65535."],"tags":["protocol","json","serialization","out-of-range","unsigned"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}