{"record":{"id":"c93939c4c4c48401","repo":"apache/dubbo","slug":"bytes2hex-length-0-length-is","errorCode":null,"errorMessage":"bytes2hex: length < 0, length is {}","messagePattern":"bytes2hex: length < 0, length is (.+?)","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java","lineNumber":402,"sourceCode":"     */\n    public static String bytes2hex(byte[] bs) {\n        return bytes2hex(bs, 0, bs.length);\n    }\n\n    /**\n     * to hex string.\n     *\n     * @param bs  byte array.\n     * @param off offset.\n     * @param len length.\n     * @return hex string.\n     */\n    public static String bytes2hex(byte[] bs, int off, int len) {\n        if (off < 0) {\n            throw new IndexOutOfBoundsException(\"bytes2hex: offset < 0, offset is \" + off);\n        }\n        if (len < 0) {\n            throw new IndexOutOfBoundsException(\"bytes2hex: length < 0, length is \" + len);\n        }\n        if (off + len > bs.length) {\n            throw new IndexOutOfBoundsException(\"bytes2hex: offset + length > array length.\");\n        }\n\n        byte b;\n        int r = off, w = 0;\n        char[] cs = new char[len * 2];\n        for (int i = 0; i < len; i++) {\n            b = bs[r++];\n            cs[w++] = BASE16[b >> 4 & MASK4];\n            cs[w++] = BASE16[b & MASK4];\n        }\n        return new String(cs);\n    }\n\n    /**\n     * from hex string.","sourceCodeStart":384,"sourceCodeEnd":420,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java#L384-L420","documentation":"Thrown by Bytes.bytes2hex(byte[], int, int) when the length argument is negative. The method allocates a char[ ] of size len*2 for the hex output, so a negative length is meaningless and is rejected up front. The check runs after the offset check, so a negative offset throws first.","triggerScenarios":"Calling Bytes.bytes2hex(bs, off, len) with len < 0. Common when len is computed as (end - start) where end < start, or when an uninitialized/decoded length field is negative.","commonSituations":"Length derived from a packet field whose value was corrupt or negative; arithmetic producing a negative remainder; passing a length variable defaulting to -1 as an 'unset' sentinel.","solutions":["Validate length >= 0 before calling bytes2hex.","Use bytes2hex(byte[]) for the whole array to avoid manual length computation.","Sanitize parsed length fields from untrusted input before use."],"exampleFix":"// before\nint len = endPos - startPos; // negative if endPos < startPos\nString hex = Bytes.bytes2hex(data, off, len); // throws [152]\n\n// after\nint len = Math.max(0, endPos - startPos);\nif (off < 0 || off + len > data.length) throw new IllegalArgumentException();\nString hex = Bytes.bytes2hex(data, off, len);","handlingStrategy":"validation","validationCode":"if (len < 0) throw new IllegalArgumentException(\"negative length: \" + len);\nString hex = Bytes.bytes2hex(bs, off, len);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use bytes2hex(byte[]) for full-array conversion.","Clamp computed lengths with Math.max(0, end - start).","Validate parsed length fields before use."],"tags":["bytes","hex","bounds-check","validation"],"backgroundTag":null,"analyzedSha":"3a3043227f5571d25eb2889de5bca22f2914843b","analyzedAt":"2026-08-14T00:43:19.853Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}