{"record":{"id":"1d5dc8d6f2f58452","repo":"apache/incubator-seata","slug":"could-not-convert-size-to-byte-length","errorCode":null,"errorMessage":"could not convert '{size}' to byte length","messagePattern":"could not convert '(.+?)' to byte length","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"common/src/main/java/org/apache/seata/common/util/SizeUtil.java","lineNumber":33,"sourceCode":" * limitations under the License.\n */\npackage org.apache.seata.common.util;\n\npublic class SizeUtil {\n    private static final long RADIX = 1024;\n    /**\n     * case size to byte length\n     * example:\n     *   2k => 2 * 1024\n     *   2m => 2 * 1024 * 1024\n     *   2g => 2 * 1024 * 1024 * 1024\n     *   2t => 2 * 1024 * 1024 * 1024 * 1024\n     * @param size the string size with unit\n     * @return the byte length\n     */\n    public static long size2Long(String size) {\n        if (null == size || size.length() <= 1) {\n            throw new IllegalArgumentException(\"could not convert '\" + size + \"' to byte length\");\n        }\n\n        String size2Lower = size.toLowerCase();\n        char unit = size2Lower.charAt(size.length() - 1);\n        long number;\n        try {\n            number = NumberUtils.toLong(size2Lower.substring(0, size.length() - 1));\n        } catch (NumberFormatException | NullPointerException ex) {\n            throw new IllegalArgumentException(\"could not convert '\" + size + \"' to byte length\");\n        }\n\n        switch (unit) {\n            case 'k':\n                return number * RADIX;\n            case 'm':\n                return number * RADIX * RADIX;\n            case 'g':\n                return number * RADIX * RADIX * RADIX;","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/apache/incubator-seata/blob/e01f97c6db397165050caa6764020410c2c8199a/common/src/main/java/org/apache/seata/common/util/SizeUtil.java#L15-L51","documentation":"Thrown by SizeUtil.size2Long when the input size string is null or has length <= 1, so it cannot possibly contain a number plus a unit. The library requires at least two characters: a numeric part and one of the unit characters k/m/g/t.","triggerScenarios":"Calling SizeUtil.size2Long(size) with null, \"\", or a single character like \"k\" or \"5\" (no unit). The check is done before any parsing, purely on length.","commonSituations":"Configuration properties for max request/body sizes left empty or set to a bare number without a unit (e.g. maxCommitRetryTimeoutInSeconds-style size keys like 'maxMessageSize=8' instead of '8k'); property placeholder resolving to null at runtime; copy-paste truncation in config files.","solutions":["Provide a value with both number and unit, e.g. '2k', '64m', '1g'.","Check the corresponding config key is actually set and the placeholder resolved (not null/empty).","Default the property in code when it may be absent instead of passing null through."],"exampleFix":"// before\nlong max = SizeUtil.size2Long(null); // IllegalArgumentException\nlong max = SizeUtil.size2Long(\"8\"); // no unit\n\n// after\nlong max = SizeUtil.size2Long(\"8k\");","handlingStrategy":"validation","validationCode":"private static final Pattern SIZE = Pattern.compile(\"^\\\\d+[kKmMgGtT]$\");\n\nboolean valid(String size) {\n    return size != null && SIZE.matcher(size).matches();\n}\n\nlong parse(String size) {\n    if (!valid(size)) throw new IllegalArgumentException(\"Bad size: \" + size);\n    return SizeUtil.size2Long(size);\n}","typeGuard":null,"tryCatchPattern":"try {\n    return SizeUtil.size2Long(size);\n} catch (IllegalArgumentException e) {\n    LOGGER.warn(\"Invalid size '{}', falling back to default {}\", size, defaultSize);\n    return defaultSize;\n}","preventionTips":["Validate size config with a strict regex at startup, fail fast with the offending key name.","Document supported format (integer + k/m/g/t) next to every size property.","Never pass unresolved placeholders to size2Long — check for null first."],"tags":["validation","configuration","size-parsing"],"backgroundTag":null,"analyzedSha":"e01f97c6db397165050caa6764020410c2c8199a","analyzedAt":"2026-08-14T10:23:53.097Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}