{"record":{"id":"253b2736d9f1afbd","repo":"apache/flink","slug":"long-overflow","errorCode":null,"errorMessage":"long overflow","messagePattern":"long overflow","errorType":"exception","errorClass":"ArithmeticException","httpStatus":null,"severity":"error","filePath":"flink-core-api/src/main/java/org/apache/flink/configuration/MemorySize.java","lineNumber":215,"sourceCode":"    // ------------------------------------------------------------------------\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    // ------------------------------------------------------------------------\n    //  Parsing\n    // ------------------------------------------------------------------------\n\n    /**\n     * Parses the given string as as MemorySize.\n     *","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core-api/src/main/java/org/apache/flink/configuration/MemorySize.java#L197-L233","documentation":"MemorySize.multiply throws ArithmeticException(\"long overflow\") when the BigDecimal product of bytes and multiplier exceeds Long.MAX_VALUE. Flink caps memory sizes at what a signed 64-bit long can represent, so multiplying a large size by a large factor overflows the representable range.","triggerScenarios":"size.multiply(Double.MAX_VALUE); multiplying a multi-GB MemorySize by a factor in the billions; multiplier values that came from an inverted or mis-scaled ratio.","commonSituations":"Unit confusion (passing 1e9 instead of a fraction); ratios computed as total/used with arguments swapped, producing huge multipliers instead of values near 1.0.","solutions":["Check the multiplier's scale — multipliers are fractions/ratios (e.g. 0.65), not absolute byte counts","Pre-check: `if (BigDecimal.valueOf(multiplier).compareTo(BigDecimal.valueOf((double) Long.MAX_VALUE / Math.max(size.getBytes(), 1))) > 0) ...` before calling","Cap the multiplier or catch ArithmeticException and clamp to Long.MAX_VALUE bytes if that is acceptable"],"exampleFix":"// before\nMemorySize total = perTask.multiply(numByteUnitsAsMultiplier); // huge factor\n\n// after\nMemorySize total = MemorySize.ofMebiBytes(perTask.getMebiBytes() * numUnits);","handlingStrategy":"validation","validationCode":"double maxFactor = (double) Long.MAX_VALUE / Math.max(base.getBytes(), 1L);\nif (multiplier > maxFactor) {\n    throw new IllegalArgumentException(\"multiplier \" + multiplier + \" overflows max MemorySize\");\n}\nMemorySize product = base.multiply(multiplier);","typeGuard":null,"tryCatchPattern":"catch (ArithmeticException e) { /* overflow means wrong scale — treat as caller bug, fix the factor */ }","preventionTips":["Multipliers are fractions near 1.0, never absolute counts","For integer scaling use multiply-by-long arithmetic in mebi-bytes instead"],"tags":["memory","overflow","arithmetic","flink-core-api"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}