{"record":{"id":"bb8a15b7cd37a689","repo":"MyCATApache/Mycat-Server","slug":"conversion-of-exceeds-long-max-value-in-try-a-l","errorCode":null,"errorMessage":"Conversion of  exceeds Long.MAX_VALUE in . Try a larger unit (e.g. MiB instead of KiB)","messagePattern":"Conversion of  exceeds Long\\.MAX_VALUE in \\. Try a larger unit \\(e\\.g\\. MiB instead of KiB\\)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/memory/unsafe/utils/ByteUnit.java","lineNumber":42,"sourceCode":"  TiB ((long) Math.pow(1024L, 4L)),\n  PiB ((long) Math.pow(1024L, 5L)), ;\n\n  ByteUnit(long multiplier) {\n    this.multiplier = multiplier;\n  }\n\n  // Interpret the provided number (d) with suffix (u) as this unit type.\n  // E.g. KiB.interpret(1, MiB) interprets 1MiB as its KiB representation = 1024k\n  public long convertFrom(long d, ByteUnit u) {\n    return u.convertTo(d, this);\n  }\n\n  // Convert the provided number (d) interpreted as this unit type to unit type (u).\n  public long convertTo(long d, ByteUnit u) {\n    if (multiplier > u.multiplier) {\n      long ratio = multiplier / u.multiplier;\n      if (Long.MAX_VALUE / ratio < d) {\n        throw new IllegalArgumentException(\"Conversion of \" + d + \" exceeds Long.MAX_VALUE in \"\n          + name() + \". Try a larger unit (e.g. MiB instead of KiB)\");\n      }\n      return d * ratio;\n    } else {\n      // Perform operations in this order to avoid potential overflow\n      // when computing d * multiplier\n      return d / (u.multiplier / multiplier);\n    }\n  }\n\n  public double toBytes(long d) {\n    if (d < 0) {\n      throw new IllegalArgumentException(\"Negative size value. Size must be positive: \" + d);\n    }\n    return d * multiplier;\n  }\n\n  public long toKiB(long d) { return convertTo(d, KiB); }","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/memory/unsafe/utils/ByteUnit.java#L24-L60","documentation":"ByteUnit.convertTo converts a value d from this unit to unit u. When converting downward (this unit has a larger multiplier than u) the result would be d * ratio, which can overflow a long; if Long.MAX_VALUE / ratio < d, this IllegalArgumentException is thrown advising a larger unit. Message shows the offending value and unit name.","triggerScenarios":"Calling e.g. byteUnit.toKiB(veryLargeLong) or convertTo on a byte count whose KiB representation exceeds Long.MAX_VALUE, e.g. new ByteUnit(\"byte\").convertTo(Long.MAX_VALUE, ByteUnit.KiB) — reached via toKiB/toMiB/toGiB/toTiB/toPiB helpers.","commonSituations":"Computing memory sizes from unvalidated configuration numbers, multiplying config values before conversion, Long.MAX_VALUE-sized inputs from misread metrics.","solutions":["Express the size in a larger unit first (e.g. convert to MiB/GiB instead of KiB) so the ratio shrinks","Validate/sanity-check the input value against the expected magnitude before converting","Catch IllegalArgumentException around the conversion and fall back to a bigger unit"],"exampleFix":"// before\nlong kib = ByteUnit.GiB.toKiB(hugeByteCount); // overflows\n// after\nlong mib = ByteUnit.GiB.toMiB(hugeByteCount); // larger unit avoids overflow","handlingStrategy":"try-catch","validationCode":"static long safeConvert(ByteUnit from, ByteUnit to, long d) {\n  long ratio = from.multiplier / to.multiplier;\n  if (from.multiplier > to.multiplier && Long.MAX_VALUE / ratio < d)\n    throw new IllegalArgumentException(d + \" too large to convert to \" + to);\n  return d * ratio;\n}","typeGuard":null,"tryCatchPattern":"long converted;\ntry {\n  converted = ByteUnit.GiB.toKiB(bytes);\n} catch (IllegalArgumentException e) {\n  converted = ByteUnit.GiB.toMiB(bytes); // fall back to larger unit\n  log.warn(\"Overflow converting to KiB; using MiB instead\");\n}","preventionTips":["Prefer larger units (MiB/GiB) when values may be huge","Validate magnitude of config-provided byte counts before conversion","Compute in long with explicit overflow checks when combining sizes"],"tags":["java","byteunit","overflow","conversion"],"backgroundTag":"argument-out-of-range","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"}