{"record":{"id":"1c0d498c9f6ad4ea","repo":"apple/pkl","slug":"integeroverflow","errorCode":"integerOverflow","errorMessage":"integerOverflow","messagePattern":"integerOverflow","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/expression/binary/AdditionNode.java","lineNumber":43,"sourceCode":"@NodeInfo(shortName = \"+\")\npublic abstract class AdditionNode extends BinaryExpressionNode {\n  protected AdditionNode(SourceSection sourceSection) {\n    super(sourceSection);\n  }\n\n  @Specialization\n  @TruffleBoundary\n  protected String eval(String left, String right) {\n    return left + right;\n  }\n\n  @Specialization\n  protected long eval(long left, long right) {\n    try {\n      return StrictMath.addExact(left, right);\n    } catch (ArithmeticException e) {\n      CompilerDirectives.transferToInterpreter();\n      throw exceptionBuilder().evalError(\"integerOverflow\").build();\n    }\n  }\n\n  @Specialization\n  protected double eval(long left, double right) {\n    return left + right;\n  }\n\n  @Specialization\n  protected double eval(double left, long right) {\n    return left + right;\n  }\n\n  @Specialization\n  protected double eval(double left, double right) {\n    return left + right;\n  }\n","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/binary/AdditionNode.java#L25-L61","documentation":"Pkl's `+` operator on two Ints uses StrictMath.addExact and throws `integerOverflow` when the mathematical sum exceeds the 64-bit signed Long range. Pkl integers are fixed-width longs, so silent wraparound is never allowed. The error surfaces at the site of the addition expression in Pkl source.","triggerScenarios":"Evaluating `left + right` where both operands are Int and the result is > Long.MAX_VALUE (9223372036854775807) or < Long.MIN_VALUE, e.g. `9223372036854775807 + 1`.","commonSituations":"Accumulating large counters, byte/duration arithmetic in raw integer form, or summing user-supplied config numbers that individually fit but together overflow.","solutions":["Reduce operand magnitudes or restructure the computation so intermediate sums stay within Long range","Convert one operand to Float (`left.toDouble() + right`) if approximate precision is acceptable","Use pkl-math or break the sum into checked stages if exact big values are required"],"exampleFix":"// before (Pkl)\nval total = 9223372036854775807 + 1\n// after\nval total = 9223372036854775807.0 + 1.0 // Float arithmetic, no overflow","handlingStrategy":"validation","validationCode":"function canAddSafely(a, b) {\n  return a <= Number.MAX_SAFE_INTEGER && b <= Number.MAX_SAFE_INTEGER &&\n         (b > 0 ? a <= Number.MAX_SAFE_INTEGER - b : true);\n}","typeGuard":"const isPklInt = (v) => typeof v === 'number' && Number.isInteger(v) && v >= -(2**63) && v <= 2**63 - 1;","tryCatchPattern":null,"preventionTips":["Bounds-check operands before large sums","Switch to Float when values approach 2^53 / 2^63","Never assume integer arithmetic wraps"],"tags":["arithmetic","integer-overflow","pkl"],"backgroundTag":"value-out-of-range","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}