{"record":{"id":"cfe8e48c76711b22","repo":"MyCATApache/Mycat-Server","slug":"tried-to-send-an-out-of-range-integer-as-a-2-byte","errorCode":null,"errorMessage":"Tried to send an out-of-range integer as a 2-byte value: ${val}","messagePattern":"Tried to send an out-of-range integer as a 2-byte value: (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/backend/postgresql/utils/PIOUtils.java","lineNumber":61,"sourceCode":"\t * @return\n\t */\n\tpublic static short redInteger2(ByteBuffer buffer, int offset) {\n\t\treturn buffer.getShort(offset);\n\t}\n\n\t/**\n\t * Sends a 2-byte integer (short) to the back end\n\t *\n\t * @param val\n\t *            the integer to be sent\n\t * @exception IOException\n\t *                if an I/O error occurs or <code>val</code> cannot be\n\t *                encoded in 2 bytes\n\t */\n\tpublic static void SendInteger2(int val, ByteBuffer buffer)\n\t\t\tthrows IOException {\n\t\tif (val < Short.MIN_VALUE || val > Short.MAX_VALUE) {\n\t\t\tthrow new IOException(\n\t\t\t\t\t\"Tried to send an out-of-range integer as a 2-byte value: \"\n\t\t\t\t\t\t\t+ val);\n\t\t}\n\n\t\tbyte[] _int2buf = new byte[2];\n\t\t_int2buf[0] = (byte) (val >>> 8);\n\t\t_int2buf[1] = (byte) val;\n\t\tbuffer.put(_int2buf);\n\t}\n\n\tpublic static void Send(byte[] encodedParam, ByteBuffer buffer) {\n\t\tbuffer.put(encodedParam);\n\t}\n\n\tpublic static void SendChar(int i, ByteBuffer buffer) {\n\t\tbuffer.put((byte) i);\n\t}\n","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/backend/postgresql/utils/PIOUtils.java#L43-L79","documentation":"PIOUtils.SendInteger2() encodes an int as a PostgreSQL protocol Int16 (2-byte) value. If val is below Short.MIN_VALUE (-32768) or above Short.MAX_VALUE (32767), it throws an IOException stating the integer is out of range for a 2-byte field.","triggerScenarios":"Calling SendInteger2 (directly or via packet builders) with a value >32767 or <-32768 — e.g. an oversized protocol field such as a parameter count, port-like field, or message length component passed to the wrong writer.","commonSituations":"Custom packet construction with wrong-width writer (using SendInteger2 where SendInteger4 is required); large column/parameter counts overflowing 2 bytes; protocol corruption feeding computed lengths into Int16 fields.","solutions":["Use SendInteger4/SendInteger8 for values that can exceed 32767 instead of SendInteger2","Clamp or validate the value against Short range before calling SendInteger2 and choose the correct width dynamically","Check the PostgreSQL protocol spec for the field being written to confirm the required width","If it comes from computed data (counts/lengths), log the value and fix the upstream computation that produced the oversized number"],"exampleFix":"// before\nPIOUtils.SendInteger2(paramCount, buffer);\n// after\nif (paramCount > Short.MAX_VALUE) { PIOUtils.SendInteger4(paramCount, buffer); } else { PIOUtils.SendInteger2(paramCount, buffer); }","handlingStrategy":"validation","validationCode":"if (val < Short.MIN_VALUE || val > Short.MAX_VALUE) {\n    throw new IllegalArgumentException(\"value \" + val + \" needs SendInteger4/8, not SendInteger2\");\n}\nPIOUtils.SendInteger2(val, buffer);","typeGuard":"boolean fitsInt16(int v) { return v >= Short.MIN_VALUE && v <= Short.MAX_VALUE; }","tryCatchPattern":"try { writePacket(...); } catch (IOException e) { if (e.getMessage().startsWith(\"Tried to send an out-of-range integer\")) { /* use wider writer or clamp */ } else { throw e; } }","preventionTips":["Check the PG protocol field width before choosing SendIntegerN","Validate computed counts/lengths against Short range","Prefer SendInteger4 for any potentially large field"],"tags":["postgresql","protocol","serialization","overflow"],"backgroundTag":"value-out-of-range","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}