{"record":{"id":"ead92175fb322c2c","repo":"apple/pkl","slug":"integeroverflow-ead921","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/TruncatingDivisionNode.java","lineNumber":48,"sourceCode":"public abstract class TruncatingDivisionNode extends BinaryExpressionNode {\n  protected TruncatingDivisionNode(SourceSection sourceSection) {\n    super(sourceSection);\n  }\n\n  @Specialization\n  protected long eval(long left, long right) {\n    if (right == 0) {\n      CompilerDirectives.transferToInterpreter();\n      throw exceptionBuilder().evalError(\"divisionByZero\").build();\n    }\n\n    var result = left / right;\n\n    // use same check as com.oracle.truffle.sl.nodes.expression.SLDivNode\n    if ((left & right & result) < 0) {\n      CompilerDirectives.transferToInterpreter();\n      assert left == Long.MIN_VALUE && right == -1;\n      throw exceptionBuilder().evalError(\"integerOverflow\").build();\n    }\n    return result;\n  }\n\n  @Specialization\n  protected long eval(long left, double right) {\n    return doTruncatingDivide(left, right);\n  }\n\n  @Specialization\n  protected long eval(double left, long right) {\n    return doTruncatingDivide(left, right);\n  }\n\n  @Specialization\n  protected long eval(double left, double right) {\n    return doTruncatingDivide(left, right);\n  }","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/binary/TruncatingDivisionNode.java#L30-L66","documentation":"Thrown when truncating integer division produces the one result that overflows 64-bit range: `Long.MIN_VALUE ~/ -1` (mathematically +2^63, which exceeds Long.MAX_VALUE). The `(left & right & result) < 0` bit trick detects exactly this case.","triggerScenarios":"Evaluating `left ~/ right` where `left == Long.MIN_VALUE (-9223372036854775808)` and `right == -1`.","commonSituations":"Dividing extreme sentinel values, negating/normalizing Long.MIN_VALUE in scaling or percentage computations.","solutions":["Convert to `Double`/`Float` before dividing when operands may be extreme","Check for `left == Int.MIN_VALUE && right == -1` beforehand and handle specially","Clamp or validate operand ranges so Long.MIN_VALUE paired with -1 never reaches the operator"],"exampleFix":"// before\nresult = minInt ~/ -1\n// after\nresult = (minInt.toDouble() / -1).truncate()","handlingStrategy":"validation","validationCode":"function canTruncDiv(a, b) { return !(a === -9223372036854775808n && b === -1n) }","typeGuard":"function isSafeDividend(a) { return a > -9223372036854775808n }","tryCatchPattern":null,"preventionTips":["Special-case Long.MIN_VALUE divided by -1","Use Float division for extreme values","Clamp operand ranges"],"tags":["pkl","arithmetic","integer-overflow"],"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"}