{"record":{"id":"38f6ee2dd45d72f2","repo":"BoundaryML/baml","slug":"baml-panics-integeroverflow","errorCode":"baml.panics.IntegerOverflow","errorMessage":"integer overflow: {message}","messagePattern":"integer overflow: (.+?)","errorType":"error_code","errorClass":"VmPanic","httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_vm_types/src/errors.rs","lineNumber":31,"sourceCode":"    BinOp, CmpOp, SysOpErrorCategory, UnaryOp, Value,\n    types::{ObjectType, Type},\n};\n\n/// A catchable BAML panic — maps 1:1 to a `baml.panics.*` class.\n///\n/// These are user-visible runtime errors (division by zero, index out of\n/// bounds, etc.) that can be caught by `catch` handlers. The handler's\n/// `ThrowIfPanic` instruction filters which panics are caught vs rethrown.\n#[derive(Debug, Error, PartialEq, Clone)]\npub enum VmPanic {\n    #[error(\"division by zero: {left:?} / {right:?}\")]\n    DivisionByZero { left: Value, right: Value },\n\n    /// An `int` (i63) arithmetic operation overflowed the representable\n    /// range `[INT_MIN, INT_MAX]`. Carries a human-readable description of\n    /// the operation (e.g. `\"4611686018427387903 + 1\"`); built only on the\n    /// cold overflow path, so the `String` alloc never touches hot code.\n    #[error(\"integer overflow: {message}\")]\n    IntegerOverflow { message: String },\n\n    // Raised by array and byte-array subscripting, so the message stays generic\n    // (\"index\", not \"array index\").\n    #[error(\"index out of bounds: {index} of {length}\")]\n    IndexOutOfBounds { index: i64, length: usize },\n\n    #[error(\"invalid field access: field {field_index} of {field_count}\")]\n    InvalidFieldAccess {\n        field_index: usize,\n        field_count: usize,\n    },\n\n    #[error(\"key not found in map\")]\n    MapKeyNotFound,\n\n    #[error(\"stack overflow\")]\n    StackOverflow,","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_vm_types/src/errors.rs#L13-L49","documentation":"This panic is raised when an arithmetic operation on a BAML `int` (i63) produces a result outside the representable range [INT_MIN, INT_MAX]. It carries a human-readable description of the operation (e.g. \"4611686018427387903 + 1\"). It is built only on the cold overflow path, so the String allocation never touches the hot arithmetic code path.","triggerScenarios":"Any `int` arithmetic that exceeds the i63 range: adding, subtracting, or multiplying large values, negating INT_MIN, or a sequence of increments like INT_MAX + 1. The message names the exact operation that overflowed.","commonSituations":"Loop accumulators that keep growing (e.g. factorial or power computations), converting huge counters or bit math without bounds checks, users assuming 64-bit int semantics when BAML ints are i63.","solutions":["Clamp or bounds-check operands before arithmetic to keep results within [INT_MIN, INT_MAX].","Catch the panic in BAML via `baml.panics.IntegerOverflow` if overflow is an expected case in your algorithm.","If wider range is needed, restructure the computation (e.g. reduce early, use logarithms) or move big-integer math to the host language."],"exampleFix":"// before\nlet total = 0;\nfor (i in huge_list) { total = total + i * i; }\n// after\nlet total = 0;\nfor (i in huge_list) {\n  if (total > INT_MAX - i * i) { return INT_MAX; }\n  total = total + i * i;\n}","handlingStrategy":"try-catch","validationCode":"// BAML: bounds-check before arithmetic\nif (a > 0 && b > INT_MAX - a) { return err(\"addition would overflow\"); }","typeGuard":"// BAML helper\nfn fits_i63(v: int) -> bool { return v >= INT_MIN && v <= INT_MAX; }","tryCatchPattern":"// BAML\ntry {\n  let r = a + b;\n} catch (e: baml.panics.IntegerOverflow) {\n  return err(\"overflow: \" + e.message);\n}","preventionTips":["Bound loop accumulators and check growth per iteration","Remember BAML ints are i63, not full i64","Clamp user-supplied numbers before heavy arithmetic"],"tags":["vm","arithmetic","overflow","panic","baml"],"backgroundTag":"value-out-of-range","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}