{"record":{"id":"a92ed04669e0336d","repo":"apache/flink","slug":"multiplier-must-be-0","errorCode":null,"errorMessage":"multiplier must be >= 0","messagePattern":"multiplier must be >= 0","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"flink-core-api/src/main/java/org/apache/flink/configuration/MemorySize.java","lineNumber":209,"sourceCode":"    public int compareTo(MemorySize that) {\n        return Long.compare(this.bytes, that.bytes);\n    }\n\n    // ------------------------------------------------------------------------\n    //  Calculations\n    // ------------------------------------------------------------------------\n\n    public MemorySize add(MemorySize that) {\n        return new MemorySize(Math.addExact(this.bytes, that.bytes));\n    }\n\n    public MemorySize subtract(MemorySize that) {\n        return new MemorySize(Math.subtractExact(this.bytes, that.bytes));\n    }\n\n    public MemorySize multiply(double multiplier) {\n        if (multiplier < 0) {\n            throw new IllegalArgumentException(\"multiplier must be >= 0\");\n        }\n\n        BigDecimal product =\n                BigDecimal.valueOf(this.bytes).multiply(BigDecimal.valueOf(multiplier));\n        if (product.compareTo(BigDecimal.valueOf(Long.MAX_VALUE)) > 0) {\n            throw new ArithmeticException(\"long overflow\");\n        }\n        return new MemorySize(product.longValue());\n    }\n\n    public MemorySize divide(long by) {\n        if (by < 0) {\n            throw new IllegalArgumentException(\"divisor must be != 0\");\n        }\n        return new MemorySize(bytes / by);\n    }\n\n    // ------------------------------------------------------------------------","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core-api/src/main/java/org/apache/flink/configuration/MemorySize.java#L191-L227","documentation":"MemorySize.multiply(double multiplier) throws IllegalArgumentException when the multiplier is negative, because memory sizes cannot be negative. The check runs before the BigDecimal multiplication, so any negative factor fails immediately even if the magnitude is tiny.","triggerScenarios":"size.multiply(-0.5); passing a fraction computed as (used - limit)/limit that goes negative; feeding a percentage/100 where the percentage was negative.","commonSituations":"Sizing derived from relative deltas (e.g. scaling a memory segment by a change factor that dips below zero); configuration code that accepts user-supplied fractions without range validation.","solutions":["Pass a non-negative multiplier; for 'shrink' semantics use a positive fraction < 1","Validate user-supplied fractions/percentages are >= 0 before use","If negative scaling is meaningful in your domain, clamp to 0 explicitly rather than relying on the exception"],"exampleFix":"// before\nMemorySize scaled = base.multiply(deltaFactor); // deltaFactor may be -0.25\n\n// after\ndouble factor = Math.max(0, deltaFactor);\nMemorySize scaled = base.multiply(factor);","handlingStrategy":"validation","validationCode":"if (multiplier < 0) {\n    throw new IllegalArgumentException(\"multiplier must be >= 0, got \" + multiplier);\n}\nMemorySize scaled = base.multiply(multiplier);","typeGuard":null,"tryCatchPattern":"catch (IllegalArgumentException e) { /* negative factor is a caller bug — fix the producer, do not catch-and-continue */ }","preventionTips":["Validate fractions from config/user input are >= 0 before use","Express shrinking as a positive factor < 1, not a negative factor"],"tags":["memory","validation","arithmetic","flink-core-api"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}