{"record":{"id":"1e3c9902ef462440","repo":"pinpoint-apm/pinpoint","slug":"string-is-longer-then-target-length-of-bytes","errorCode":null,"errorMessage":"String is longer then target length of bytes.","messagePattern":"String is longer then target length of bytes\\.","errorType":"exception","errorClass":"StringIndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"commons/src/main/java/com/navercorp/pinpoint/common/util/BytesUtils.java","lineNumber":555,"sourceCode":"        System.arraycopy(data, 0, buf, 1, data.length);\n        return buf;\n    }\n\n    public static byte[] toBytes(final String value) {\n        return value == null ? null : value.getBytes(UTF8_CHARSET);\n    }\n\n    public static byte[] toFixedLengthBytes(final String str, final int length) {\n        if (length < 0) {\n            throw new ArrayIndexOutOfBoundsException(length);\n        }\n        final byte[] b1 = toBytes(str);\n        if (b1 == null) {\n            return new byte[length];\n        }\n\n        if (b1.length > length) {\n            throw new StringIndexOutOfBoundsException(\"String is longer then target length of bytes.\");\n        }\n        byte[] b = new byte[length];\n        System.arraycopy(b1, 0, b, 0, b1.length);\n\n        return b;\n    }\n\n    /**\n     * Range : 0 ~ 255\n     */\n    public static byte toUnsignedByte(int value) {\n        if (value < UNSIGNED_BYTE_MIN || value > UNSIGNED_BYTE_MAX) {\n            throw new IllegalArgumentException(\"UnsignedByte Out of Range (0~255)\");\n        }\n        return (byte) (value & 0xff);\n    }\n\n","sourceCodeStart":537,"sourceCodeEnd":573,"githubUrl":"https://github.com/pinpoint-apm/pinpoint/blob/744c3d3075e595656abb1ae331ad2c0e4c9eb996/commons/src/main/java/com/navercorp/pinpoint/common/util/BytesUtils.java#L537-L573","documentation":"BytesUtils.toFixedLengthBytes converts a string to a fixed-length byte array by copying its UTF-8/encoded bytes into a buffer of the requested length. If the encoded string is longer than the target length, the copy would truncate data, so it throws StringIndexOutOfBoundsException instead of silently truncating.","triggerScenarios":"Calling BytesUtils.toFixedLengthBytes(str, length) where str's encoded byte length exceeds length, e.g. toFixedLengthBytes(\"agent-long-name-1234567890\", 24) or multibyte characters inflating byte length beyond the limit.","commonSituations":"Writing Pinpoint fixed-width storage fields (agent name, application name) with values longer than the schema's fixed byte size, or non-ASCII names where byte length exceeds character length assumptions.","solutions":["Shorten the input string so its encoded byte length fits the target length.","Truncate explicitly before calling, accounting for multibyte characters (truncate on bytes, not chars).","Increase the fixed length if the schema allows longer values."],"exampleFix":"// before\nbyte[] fixed = BytesUtils.toFixedLengthBytes(agentName, 24);\n// after\nString safe = agentName;\nwhile (safe.getBytes(StandardCharsets.UTF_8).length > 24) {\n    safe = safe.substring(0, safe.length() - 1);\n}\nbyte[] fixed = BytesUtils.toFixedLengthBytes(safe, 24);","handlingStrategy":"type-guard","validationCode":"int byteLen = str.getBytes(StandardCharsets.UTF_8).length;\nif (byteLen > length) {\n    throw new IllegalArgumentException(\"string needs \" + byteLen + \" bytes, limit \" + length);\n}","typeGuard":"boolean fitsFixedLength(String s, int len) { return s == null || s.getBytes(StandardCharsets.UTF_8).length <= len; }","tryCatchPattern":"try {\n    return BytesUtils.toFixedLengthBytes(str, length);\n} catch (StringIndexOutOfBoundsException e) {\n    log.warn(\"Value too long for fixed field ({} bytes max)\", length);\n    return truncateToBytes(str, length);\n}","preventionTips":["Compute limits in bytes, not characters, for non-ASCII input","Enforce max length at input/config validation time","Add schema checks that alert when stored names approach the byte limit"],"tags":["bytes","encoding","string-length","fixed-buffer"],"backgroundTag":"value-out-of-range","analyzedSha":"744c3d3075e595656abb1ae331ad2c0e4c9eb996","analyzedAt":"2026-09-07T18:48:45.289Z","contentChangedAt":"2026-09-07T18:48:45.289Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}