MyCATApache/Mycat-Server · error · NumberFormatException

toString(buf)

Error message

toString(buf)

What it means

BytesTools.getInt parses an int from a byte range after skipping leading whitespace. If the entire range [offset, endPos) is whitespace (or empty), no digits exist to parse, so it throws NumberFormatException whose message is the buffer rendered via toString(buf).

Solutions

  1. Check the slice for at least one non-whitespace character before calling getInt.
  2. Provide a default value when the field is blank (catch NumberFormatException and substitute).
  3. Fix upstream serialization so numeric fields are never written as empty strings.
  4. Trim/skip empty fields during protocol/row parsing instead of routing them to getInt.

Example fix

// before
int v = BytesTools.getInt(buf, off, end); // throws on blank
// after
String s = new String(buf, off, end - off).trim();
int v = s.isEmpty() ? 0 : Integer.parseInt(s);
Defensive patterns

Strategy: try-catch

Validate before calling

public static boolean isBlank(byte[] buf, int off, int end) {
    for (int i = off; i < end; i++) {
        if (!Character.isWhitespace((char) buf[i])) return false;
    }
    return true;
}
// skip the call when isBlank(buf, off, end)

Type guard

public static boolean parseableInt(byte[] buf, int off, int end) {
    String s = new String(buf, off, end - off).trim();
    return s.matches("[+-]?\\d+") && s.length() <= 11;
}

Try / catch

try {
    value = BytesTools.getInt(buf, off, end);
} catch (NumberFormatException e) {
    logger.warn("blank/invalid int field: " + e.getMessage());
    value = DEFAULT_INT;
}

Prevention

When it happens

Trigger: Calling BytesTools.getInt(buf, offset, endPos) where the slice contains only spaces/tabs/newlines or is zero-length, e.g. getInt(" ".getBytes(), 0, 3).

Common situations: Parsing empty or blank fields from delimited byte payloads, truncated network/protocol buffers, or CSV/row data where a numeric column is empty.

Related errors


AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11). Data as JSON: /api/errors/3c29780dfbc79192. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/memory/unsafe/utils/BytesTools.java:109

     * Convert a byte array  to a int value
     * @param buf
     * @return int
     * @throws NumberFormatException
     */

    public static int getInt(byte[] buf) throws NumberFormatException {
        return getInt(buf, 0, buf.length);
    }

    public static int getInt(byte[] buf, int offset, int endPos) throws NumberFormatException {
        byte base = 10;

        int s;
        for(s = offset; s < endPos && Character.isWhitespace((char)buf[s]); ++s) {
            ;
        }
        if(s == endPos) {
            throw new NumberFormatException(toString(buf));
        } else {
            boolean negative = false;
            if((char)buf[s] == 45) {
                negative = true;
                ++s;
            } else if((char)buf[s] == 43) {
                ++s;
            }

            int save = s;
            int cutoff = 2147483647 / base;
            int cutlim = 2147483647 % base;
            if(negative) {
                ++cutlim;
            }

            boolean overflow = false;

View on GitHub (pinned to 65f8d8beb7)