{"record":{"id":"c6cc58d0b2287d57","repo":"MyCATApache/Mycat-Server","slug":"array-has-wrong-size","errorCode":null,"errorMessage":"Array has wrong size: ","messagePattern":"Array has wrong size: ","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/memory/unsafe/utils/BytesTools.java","lineNumber":612,"sourceCode":"    /**\n     * Convert a boolean to a byte array. True becomes -1\n     * and false becomes 0.\n     *\n     * @param b value\n     * @return <code>b</code> encoded in a byte array.\n     */\n    public static byte [] toBytes(final boolean b) {\n        return new byte[] { b ? (byte) -1 : (byte) 0 };\n    }\n\n    /**\n     * Reverses {@link #toBytes(boolean)}\n     * @param b array\n     * @return True or false.\n     */\n    public static boolean toBoolean(final byte [] b) {\n        if (b.length != 1) {\n            throw new IllegalArgumentException(\"Array has wrong size: \" + b.length);\n        }\n        return b[0] != (byte) 0;\n    }\n\n    /**\n     * Convert a long value to a byte array using big-endian.\n     *\n     * @param val value to convert\n     * @return the byte array\n     */\n    public static byte[] toBytes(long val) {\n        byte [] b = new byte[8];\n        for (int i = 7; i > 0; i--) {\n            b[i] = (byte) val;\n            val >>>= 8;\n        }\n        b[0] = (byte) val;\n        return b;","sourceCodeStart":594,"sourceCodeEnd":630,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/memory/unsafe/utils/BytesTools.java#L594-L630","documentation":"BytesTools.toBoolean(byte[]) is the inverse of toBytes(boolean) and therefore requires an array of exactly one byte. The library throws this IllegalArgumentException when the array length is not 1, to prevent misreading longer or empty buffers as booleans.","triggerScenarios":"Calling BytesTools.toBoolean with a zero-length array, or with a multi-byte buffer (e.g. a slice of a larger byte array, a serialized record, or a UTF-8 encoded string).","commonSituations":"Deserializing a byte stream and passing a whole row/buffer instead of the single-byte boolean field; reading a boolean column from HBase-style storage with wrong offset; empty byte[] returned from a store.","solutions":["Slice the array so you pass exactly the 1-byte boolean field: toBoolean(Arrays.copyOfRange(buf, offset, offset + 1)).","Check b.length == 1 before calling; handle empty arrays explicitly.","If the source is an encoded string or multi-byte encoding, decode it with the correct codec instead of reading raw bytes as a boolean."],"exampleFix":"// before\nboolean flag = BytesTools.toBoolean(rowBytes);\n// after\nif (rowBytes != null && rowBytes.length == 1) {\n  boolean flag = BytesTools.toBoolean(rowBytes);\n}","handlingStrategy":"type-guard","validationCode":"boolean isSingleByte(byte[] b) { return b != null && b.length == 1; }","typeGuard":"static boolean isSingleByte(byte[] b) {\n  return b != null && b.length == 1;\n}\n// usage: if (isSingleByte(buf)) boolean v = BytesTools.toBoolean(buf);","tryCatchPattern":"try {\n  flag = BytesTools.toBoolean(buf);\n} catch (IllegalArgumentException e) {\n  flag = false; // or handle bad-length buffer explicitly\n}","preventionTips":["Slice buffers to the exact field offset before converting scalar types.","Never pass whole serialized records or strings as raw bytes to scalar converters.","Check array length for every fixed-width decode (1 for boolean, 8 for long, etc.)."],"tags":["java","bytes","deserialization"],"backgroundTag":"invalid-argument-value","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}