{"record":{"id":"b689a183aa32cce0","repo":"apple/pkl","slug":"integeroverflow-b689a1","errorCode":"integerOverflow","errorMessage":"integerOverflow","messagePattern":"integerOverflow","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/runtime/VmSafeMath.java","lineNumber":38,"sourceCode":"import com.oracle.truffle.api.nodes.Node;\nimport java.math.RoundingMode;\nimport org.pkl.core.runtime.VmException.ProgramValue;\nimport org.pkl.core.util.MathUtils;\n\n/**\n * Uses methods from [java.lang.(Strict)Math] where appropriate, which may benefit from special\n * optimization by Graal. To control error messages in a single place (namely here),\n * [ArithmeticException]s thrown by [java.lang.StrictMath] are caught and rethrown.\n */\npublic final class VmSafeMath {\n  private VmSafeMath() {}\n\n  public static long negate(long x) {\n    try {\n      return Math.negateExact(x);\n    } catch (ArithmeticException e) {\n      CompilerDirectives.transferToInterpreter();\n      throw intOverflow();\n    }\n  }\n\n  public static double negate(double x) {\n    return -x;\n  }\n\n  public static long add(long x, long y) {\n    try {\n      return StrictMath.addExact(x, y);\n    } catch (ArithmeticException e) {\n      CompilerDirectives.transferToInterpreter();\n      throw intOverflow();\n    }\n  }\n\n  public static double add(double x, double y) {\n    return x + y;","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/runtime/VmSafeMath.java#L20-L56","documentation":"Pkl's Int is a 64-bit signed integer. Unary negation of an Int is performed with Math.negateExact; negating Long.MIN_VALUE (-9223372036854775808) has no positive counterpart, so VmSafeMath.negate throws the `integerOverflow` eval error instead of silently wrapping.","triggerScenarios":"Evaluating the unary minus operator on Int value -9223372036854775808 (the only value whose negation overflows), e.g. `-x` where x == Long.MIN_VALUE, or `-(-9223372036854775808)`.","commonSituations":"Config arithmetic on extreme sentinel values (using Long.MIN_VALUE as 'unset'); large absolute values produced by earlier multiplication before negation.","solutions":["Change the value to something within [-9223372036854775807, 9223372036854775807].","If you need the magnitude of Long.MIN_VALUE, use a Float: `-(9223372036854775808.0.toFloat())`.","Guard the negation: `if (x == Int.MIN_VALUE) ... else -x` and handle the extreme case explicitly."],"exampleFix":"// before\nabs = -x  // x == -9223372036854775808\n\n// after\nabs = if (x == Int.MIN_VALUE) 9223372036854775808.0.toFloat() else -x","handlingStrategy":"validation","validationCode":"// Pkl\nisSafeToNegate = (x: Int) -> x != Int.MIN_VALUE","typeGuard":"function isNegatable(x: Int): Boolean = x != Int.MIN_VALUE","tryCatchPattern":null,"preventionTips":["Avoid using Int.MIN_VALUE as a sentinel value","Guard unary negation of user-supplied integers","Use Float when magnitudes near 2^63 are possible"],"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"}