{"record":{"id":"38c5f681310f0912","repo":"YunaiV/ruoyi-vue-pro","slug":"error-38c5f6","errorCode":null,"errorMessage":"不支持的长度字段长度: {}","messagePattern":"不支持的长度字段长度: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/codec/length/IotTcpLengthFieldFrameCodec.java","lineNumber":140,"sourceCode":"        // 写入消息体\n        buffer.appendBytes(data);\n        return buffer;\n    }\n\n    /**\n     * 从 Buffer 中读取长度字段\n     */\n    @SuppressWarnings(\"EnhancedSwitchMigration\")\n    private int readLength(Buffer buffer, int offset, int length) {\n        switch (length) {\n            case 1:\n                return buffer.getUnsignedByte(offset);\n            case 2:\n                return buffer.getUnsignedShort(offset);\n            case 4:\n                return buffer.getInt(offset);\n            default:\n                throw new IllegalArgumentException(\"不支持的长度字段长度: \" + length);\n        }\n    }\n\n    /**\n     * 向 Buffer 中写入长度字段\n     */\n    private void writeLength(Buffer buffer, int length, int fieldLength) {\n        switch (fieldLength) {\n            case 1:\n                buffer.appendByte((byte) length);\n                break;\n            case 2:\n                buffer.appendShort((short) length);\n                break;\n            case 4:\n                buffer.appendInt(length);\n                break;\n            default:","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/YunaiV/ruoyi-vue-pro/blob/0418084e222612af2fc1141f566af454f9236ab1/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/codec/length/IotTcpLengthFieldFrameCodec.java#L122-L158","documentation":"readLength() decodes the length field using Vert.x Buffer accessors and supports only 1-byte (unsigned), 2-byte (unsigned short), and 4-byte (signed int) widths. Any other lengthFieldLength (0, 3, 5, 6, 8...) throws IllegalArgumentException at decode time. The constructor only Asserts the config value is non-null, not that it is in {1,2,4}, so a bad value survives until the first frame arrives.","triggerScenarios":"config.lengthFieldLength set to a value other than 1, 2, or 4 (e.g. 3 for a 24-bit length field, 0, or a typo); the first inbound frame triggers readLength and fails.","commonSituations":"Device uses a non-standard 3-byte length field; config typo (e.g. 24 entered instead of being unsupported); misunderstanding that the codec only supports 1/2/4.","solutions":["Set lengthFieldLength to 1, 2, or 4 (whichever matches the device's length-field byte width).","If the device truly uses a 3-byte (24-bit) or other width, pre/post-process the stream or extend readLength() with a custom case.","Add constructor validation rejecting lengthFieldLength not in {1,2,4} so misconfiguration fails fast at startup, not on first frame."],"exampleFix":"// before: constructor only checks non-null\nAssert.notNull(config.getLengthFieldLength(), \"lengthFieldLength 不能为空\");\n\n// after: also validate the allowed set\nint lfl = config.getLengthFieldLength();\nAssert.isTrue(lfl == 1 || lfl == 2 || lfl == 4,\n    \"lengthFieldLength 必须是 1、2 或 4\");","handlingStrategy":"validation","validationCode":"// validate lengthFieldLength at construction so misconfiguration fails at startup\nint lfl = config.getLengthFieldLength();\nif (lfl == null || (lfl != 1 && lfl != 2 && lfl != 4)) {\n    throw new IllegalArgumentException(\"lengthFieldLength 必须是 1、2 或 4, 当前: \" + lfl);\n}","typeGuard":"boolean isSupportedLengthFieldLength(Integer lfl) {\n    return lfl != null && (lfl == 1 || lfl == 2 || lfl == 4);\n}","tryCatchPattern":null,"preventionTips":["Add constructor range validation for lengthFieldLength (and the other codec params).","Set lengthFieldLength from the device datasheet's length-field byte width (1, 2, or 4).","If the device uses a non-standard width, preprocess the stream or extend readLength() with a custom case."],"tags":["tcp","codec","config","length-field"],"backgroundTag":null,"analyzedSha":"0418084e222612af2fc1141f566af454f9236ab1","analyzedAt":"2026-08-14T00:56:18.412Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}