{"record":{"id":"3c29780dfbc79192","repo":"MyCATApache/Mycat-Server","slug":"tostring-buf","errorCode":null,"errorMessage":"toString(buf)","messagePattern":"toString\\(buf\\)","errorType":"validation","errorClass":"NumberFormatException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/memory/unsafe/utils/BytesTools.java","lineNumber":109,"sourceCode":"     * Convert a byte array  to a int value\n     * @param buf\n     * @return int\n     * @throws NumberFormatException\n     */\n\n    public static int getInt(byte[] buf) throws NumberFormatException {\n        return getInt(buf, 0, buf.length);\n    }\n\n    public static int getInt(byte[] buf, int offset, int endPos) throws NumberFormatException {\n        byte base = 10;\n\n        int s;\n        for(s = offset; s < endPos && Character.isWhitespace((char)buf[s]); ++s) {\n            ;\n        }\n        if(s == endPos) {\n            throw new NumberFormatException(toString(buf));\n        } else {\n            boolean negative = false;\n            if((char)buf[s] == 45) {\n                negative = true;\n                ++s;\n            } else if((char)buf[s] == 43) {\n                ++s;\n            }\n\n            int save = s;\n            int cutoff = 2147483647 / base;\n            int cutlim = 2147483647 % base;\n            if(negative) {\n                ++cutlim;\n            }\n\n            boolean overflow = false;\n","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/memory/unsafe/utils/BytesTools.java#L91-L127","documentation":"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).","triggerScenarios":"Calling BytesTools.getInt(buf, offset, endPos) where the slice contains only spaces/tabs/newlines or is zero-length, e.g. getInt(\"   \".getBytes(), 0, 3).","commonSituations":"Parsing empty or blank fields from delimited byte payloads, truncated network/protocol buffers, or CSV/row data where a numeric column is empty.","solutions":["Check the slice for at least one non-whitespace character before calling getInt.","Provide a default value when the field is blank (catch NumberFormatException and substitute).","Fix upstream serialization so numeric fields are never written as empty strings.","Trim/skip empty fields during protocol/row parsing instead of routing them to getInt."],"exampleFix":"// before\nint v = BytesTools.getInt(buf, off, end); // throws on blank\n// after\nString s = new String(buf, off, end - off).trim();\nint v = s.isEmpty() ? 0 : Integer.parseInt(s);","handlingStrategy":"try-catch","validationCode":"public static boolean isBlank(byte[] buf, int off, int end) {\n    for (int i = off; i < end; i++) {\n        if (!Character.isWhitespace((char) buf[i])) return false;\n    }\n    return true;\n}\n// skip the call when isBlank(buf, off, end)","typeGuard":"public static boolean parseableInt(byte[] buf, int off, int end) {\n    String s = new String(buf, off, end - off).trim();\n    return s.matches(\"[+-]?\\\\d+\") && s.length() <= 11;\n}","tryCatchPattern":"try {\n    value = BytesTools.getInt(buf, off, end);\n} catch (NumberFormatException e) {\n    logger.warn(\"blank/invalid int field: \" + e.getMessage());\n    value = DEFAULT_INT;\n}","preventionTips":["Trim and check emptiness of numeric fields before byte-level parsing","Handle blank delimited fields explicitly in row parsers","Fix producers so numeric fields are never serialized empty"],"tags":["java","parsing","number-format","bytes"],"backgroundTag":"invalid-number-format","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"}