Skip to content

fix(translate): diagnose out-of-range numeric literals - #127

Merged
tetsuo-cpp merged 1 commit into
canonfrom
fix/literal-overflow
Oct 24, 2026
Merged

tetsuo-cpp merged 1 commit into
canonfrom
fix/literal-overflow

Conversation

@tetsuo-cpp

Copy link
Copy Markdown
Owner

The parser converted number tokens with std::stoll and std::stod. Both throw std::out_of_range when the value does not fit, and nothing caught it, so the compiler aborted.

Change

  • Integer literals are parsed with StringRef::getAsInteger (a leading + is stripped first, as isNumber allows it). Out-of-range values report integer literal out of range for i64 at the token.
  • Float literals are parsed with llvm::to_float, the same routine the lexer uses to classify them. A value that overflows to infinity reports float literal out of range for f64. Underflow is accepted and rounds to zero or a subnormal, as in C. Before this change underflow also crashed, because std::stod throws on any ERANGE.
  • The lexer's classification logic is unchanged (Hex literals, nan and inf are silently lexed as float literals #112 touches isFloat separately).

Before

$ warpforthc int.forth   # 99999999999999999999 a !
libc++abi: terminating due to uncaught exception of type std::out_of_range: stoll: out of range
exit=134
$ warpforthc flt.forth   # 1e999 a !
libc++abi: terminating due to uncaught exception of type std::out_of_range: stod: out of range
exit=134

After

$ warpforthc int.forth
int.forth:3:1: error: integer literal out of range for i64
99999999999999999999 a !
^
error: failed to parse Forth source
exit=1
$ warpforthc flt.forth
flt.forth:3:1: error: float literal out of range for f64
1e999 a !
^
error: failed to parse Forth source
exit=1

Tests

  • New: integer-literal-overflow-error.forth, integer-literal-underflow-error.forth, float-literal-overflow-error.forth. Each one crashed before this change.
  • basic-literals.forth now covers +5, INT64_MAX and INT64_MIN.
  • float-literals.forth now covers DBL_MAX and 1e-400, which underflows to zero.

Fixes #111

Integer and float literals outside the i64/f64 range threw an uncaught std::out_of_range from std::stoll/std::stod and aborted the compiler. Parse integers with StringRef::getAsInteger and floats with llvm::to_float, and emit a located parser error when the value does not fit. Float literals that overflow to infinity are rejected; literals that underflow round to zero or a subnormal, as in C.
@tetsuo-cpp
tetsuo-cpp force-pushed the fix/literal-overflow branch from 052cfe3 to 1c82d40 Compare October 24, 2026 10:03
@tetsuo-cpp
tetsuo-cpp merged commit 71887d3 into canon Oct 24, 2026
1 check passed
@tetsuo-cpp
tetsuo-cpp deleted the fix/literal-overflow branch October 24, 2026 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Integer literal overflow crashes the compiler (uncaught std::out_of_range)

1 participant