{"record":{"id":"05c7939361d486d7","repo":"apple/pkl","slug":"intvaluetoolarge","errorCode":"intValueTooLarge","errorMessage":"intValueTooLarge","messagePattern":"intValueTooLarge","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/runtime/VmSafeMath.java","lineNumber":105,"sourceCode":"    }\n\n    return result;\n  }\n\n  public static long remainder(long x, long y) {\n    return x % y;\n  }\n\n  public static double remainder(double x, double y) {\n    return x % y;\n  }\n\n  public static int toInt32(long x) {\n    try {\n      return StrictMath.toIntExact(x);\n    } catch (ArithmeticException e) {\n      CompilerDirectives.transferToInterpreter();\n      throw new VmExceptionBuilder().evalError(\"intValueTooLarge\", x).build();\n    }\n  }\n\n  public static double truncate(double x) {\n    if (x < 0) {\n      return StrictMath.ceil(x);\n    }\n    return StrictMath.floor(x);\n  }\n\n  @TruffleBoundary\n  public static long toInt(double x, Node sourceNode) {\n    try {\n      return MathUtils.roundToLong(x, RoundingMode.DOWN);\n    } catch (ArithmeticException e) {\n      CompilerDirectives.transferToInterpreter();\n      throw new VmExceptionBuilder()\n          .withLocation(sourceNode)","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/runtime/VmSafeMath.java#L87-L123","documentation":"`VmSafeMath.toInt32` converts a 64-bit long to a 32-bit int via `StrictMath.toIntExact`, which throws ArithmeticException when the value does not fit in Int32 range (-2147483648..2147483647). The runtime re-raises it as an evalError with the offending value.","triggerScenarios":"Any Pkl Int-to-Int32 conversion where the long operand is outside the Int32 range, e.g. `x.toInt32()` on a large Int, or Int32-returning stdlib functions given out-of-range arguments.","commonSituations":"Bit-manipulation code (` shl `, `and`, masks) on big integers; passing computed values like timestamps in milliseconds beyond Int32 bounds to Int32 APIs.","solutions":["Check the value is within -2147483648..2147483647 before converting.","Keep the value as a Pkl Int (64-bit) if full range is needed.","Pre-clip with explicit bounds: `if (x > 2147483647) ... else x.toInt32()`.","Recompute the expression in a way that cannot overflow (e.g. divide or mask before converting)."],"exampleFix":"// before\nval flags = hugeCounter.toInt32()\n// after\nval flags = (hugeCounter % 2147483648).toInt32()","handlingStrategy":"validation","validationCode":"function fitsInt32(x) { return x >= -2147483648 && x <= 2147483647; }","typeGuard":"function isInt32Safe(x) { return typeof x === 'number' && Number.isInteger(x) && x >= -2147483648 && x <= 2147483647; }","tryCatchPattern":"try { x.toInt32() } catch (e) { /* value out of Int32 range; fall back to 64-bit Int */ }","preventionTips":["Keep values as 64-bit Int unless Int32 is required","Check bounds before bit-operations/conversions","Watch for timestamp/value computations that exceed 2^31"],"tags":["pkl","integer-overflow","math","conversion"],"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"}