{"record":{"id":"03d0e2324c5f9494","repo":"MyCATApache/Mycat-Server","slug":"negative-size-value-size-must-be-positive","errorCode":null,"errorMessage":"Negative size value. Size must be positive: ","messagePattern":"Negative size value\\. Size must be positive: ","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/memory/unsafe/utils/ByteUnit.java","lineNumber":55,"sourceCode":"  // 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); }\n  public long toMiB(long d) { return convertTo(d, MiB); }\n  public long toGiB(long d) { return convertTo(d, GiB); }\n  public long toTiB(long d) { return convertTo(d, TiB); }\n  public long toPiB(long d) { return convertTo(d, PiB); }\n\n  private final long multiplier;\n}\n","sourceCodeStart":37,"sourceCodeEnd":68,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/memory/unsafe/utils/ByteUnit.java#L37-L68","documentation":"ByteUnit.toBytes() converts a quantity in the given unit to bytes. The library throws this IllegalArgumentException when the input value is negative, because memory sizes must be non-negative; a negative size would corrupt downstream allocation logic.","triggerScenarios":"Calling any ByteUnit conversion helper (e.g. toBytes) with a negative long, typically a size computed from configuration, subtraction (a - b where b > a), or an uninitialized/overflowed variable.","commonSituations":"Memory config values that are negative due to subtraction of two memory budgets, parsing negative numbers from properties files, or off-by-one arithmetic when computing free memory.","solutions":["Print and inspect the value passed to toBytes; trace where the negative number originates (usually a subtraction or bad config).","Clamp or validate sizes at the configuration boundary before conversion: if (size < 0) throw or default.","If a negative value is legitimately possible, restructure the computation rather than converting it."],"exampleFix":"// before\nlong bytes = BYTE.toBytes(used - total);\n// after\nlong delta = used - total;\nlong bytes = delta < 0 ? 0 : BYTE.toBytes(delta);","handlingStrategy":"validation","validationCode":"if (value < 0) throw new IllegalArgumentException(\"Size must be positive, got: \" + value);\nlong bytes = unit.toBytes(value);","typeGuard":null,"tryCatchPattern":"try {\n  long bytes = unit.toBytes(value);\n} catch (IllegalArgumentException e) {\n  // log offending value and fall back to a default size\n  bytes = defaultBytes;\n}","preventionTips":["Validate all memory sizes at config-load time, before any conversion.","Be careful with subtractions between memory budgets; clamp at zero.","Add assertions in debug builds that all size variables are non-negative."],"tags":["java","memory","argument-validation"],"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"}