Skip to content

Nat.read compares its bound as fifteen digits, so a checked read of 7 finishes - #1009

Merged
nicolas-abril merged 3 commits into
bendlang:mainfrom
AppSprout-dev:nat-read-bound
Sep 25, 2026
Merged

nicolas-abril merged 3 commits into
bendlang:mainfrom
AppSprout-dev:nat-read-bound

Conversation

@jkbennitt

@jkbennitt jkbennitt commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Nat.read divided the unary nat 2^48 - 1 on every digit, so {Nat.read("7") == Some{7n}} never finished (#1008). The bound is the fifteen-digit text 281474976710655. Leading zeros are stripped first. A shorter all-digit string is in range, a longer one is None, and fifteen digits compare as text. Nat.read.max is gone; nothing in the repo called it.

The length check is exact, so a sixteen-digit string is None even when its text sorts before the bound, and leading zeros still read as the number they spell (0000000000000007 is 7).

The values pinned by tests/base/read_bounds.bend are unchanged, including 10^14 through 2^48 - 1 (a plain digit cap once rejected that range).

Test plan

  • tests/base/read_bounds.bend --check-only closes, including equalities for "7", "007", "000", "", "12x", "1000000000000000", "281474976710656", and Nat.show(7n)
  • The same file prints its #| lines from the interpreter, from emitted JS, and from the C binary, including the sixteen-digit and leading-zero cases
  • tests/base/text_ops.bend still prints 14
  • Cluster gate (not run here)

… finishes

2^48-1 was built as a unary nat and divided on every digit, so a law
that reads "7" never closed. The bound is the text 281474976710655.
Shorter digit strings are in range, longer ones are not, and fifteen
digits compare as text. Closes bendlang#1008.
@nicolas-abril nicolas-abril self-assigned this Sep 24, 2026

@nicolas-abril nicolas-abril left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. It fixes #1008: the laws for "7", "" and "12x" close at once. I found one regression, though, in how the bound handles strings longer than fifteen digits.

The problem. Nat.read.under is

Bool.or(Nat.is_lt(n, 15n), String.is_le(s, "281474976710655"))

String.is_le compares text alphabetically, so when n > 15 a string still passes whenever its first characters sort at or before the bound's. Nat.read.go then builds a value past 2^48 − 1, and the runtime fail-stops. Results on current main and on this PR merged onto it, the same on the interpreter, JS and C:

Input main this PR
"1000000000000000" (10^15) None bend: a Nat past the largest immediate 2^48-1
"2000000000000000" None same fail-stop
"0281474976710656" (2^48, one leading zero) None same fail-stop
"0999999999999999" None same fail-stop
"18446744073709551616" None same fail-stop
"2814749767106550" None None

So any program that reads untrusted input with Nat.read can now be killed by a sixteen-digit string where it used to get None. read_bounds.bend doesn't catch this because its longest input has fifteen digits.

Leading zeros matter for the fix: main and this PR both read "0000000000000007" as 7 and "00000000000000000000281474976710655" as 281474976710655. So simply rejecting every string of more than fifteen digits would fix the fail-stop but reject those.

A possible fix. This is one way to do it; you may see a better one. Strip leading zeros, then allow fewer than fifteen digits, or exactly fifteen that compare <= to the bound as text:

def Nat.read.under(+n: Nat, s: String) -> Bool:
  Bool.or(Nat.is_lt(n, 15n),
    Bool.and(Nat.is_eq(n, 15n), String.is_le(s, "281474976710655")))

def Nat.read.trim(s: String) -> String:
  match s:
    case SCon{'0', t}:
      Nat.read.trim(t)
    case t:
      t

def Nat.read.pick(+s: String, got: Maybe<&2, Nat>) -> Maybe<&2, Nat>:
  match got:
    case None{}:
      None{}
    case Some{n}:
      Nat.read.gate(s, Nat.read.under(n, s))

def Nat.read(s: String) -> Maybe<&2, Nat>:
  match s:
    case SNil{}:
      None{}
    case SCon{h, t}:
      +u = Nat.read.trim(SCon{h, t})
      Nat.read.pick(u, Nat.read.len(u))

n becomes +n because it is now used twice. "" is handled by the match in Nat.read, so the Some{0n} case in pick goes away, and an all-zero string trims to "" and reads as Some{0n}.

With this change, Nat.read gives the same result as main on 25 edge strings on all three lanes. They cover empty, "0", "000", leading zeros, non-digits such as "/" and ":", 14 to 35 digits, and both sides of the bound. These laws all close in the checker:

  • Nat.read("7") == Some{7n}, Nat.read("007") == Some{7n} and Nat.read("000") == Some{0n};
  • Nat.read("12x") == None{} and Nat.read("281474976710656") == None{};
  • Nat.read("1000000000000000") == None{} and Nat.read(Nat.show(7n)) == Some{7n}.

read_bounds (with your new laws) and text_ops still pass on every lane.

Whichever fix you choose, please add a sixteen-digit case like "1000000000000000" and a leading-zero case to read_bounds.bend, so this stays covered.

Otherwise the PR merges cleanly onto current main, and with it merged the local test suite passes except the two gfx tests that need a machine without a display.

Note: this review was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

A longer string can still sort before 281474976710655, so the old
check built a Nat past 2^48-1 and the runtime fail-stopped. Zeros
come off first; fewer than fifteen digits are in range, more are
not, and fifteen compare as text.
@jkbennitt

Copy link
Copy Markdown
Contributor Author

Leading zeros are stripped, then fewer than fifteen digits are in range and exactly fifteen compare as text with 281474976710655.

1000000000000000 and 0281474976710656 are None again. 0000000000000007 is 7, and the bound with leading zeros is still 281474976710655. read_bounds prints those, and the checker laws for 007, 000, the sixteen-digit string, one past the bound, and Nat.show(7n) close. The interpreter, JS, and C agree, and text_ops still prints 14.

@nicolas-abril
nicolas-abril merged commit 7819652 into bendlang:main Sep 25, 2026
@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thank you @jkbennitt! This fixes #1008, and the leading-zero handling and the sixteen-digit cases in read_bounds.bend are exactly what was needed. Merged.

Right after the merge we simplified it a little in ff4bb00:

  • Nat.read.len and its three helpers (len.if, len.suc, len.no) counted digits while checking each one. Nat.read.go already returns None on a non-digit, so the bound only needs the string's length. String.length is already in Base, so a single Nat.read.fits now replaces len, under and pick. That leaves three helpers (trim, fits, gate) instead of eight.
  • Nat.read.gate stays a def. We tried Bool.pick instead, but it evaluates both branches first, so a string past the bound would build its Nat and fail-stop.

To be sure the change is safe, we wrote a Bend proof, checked against your version of base.bend, that the two Nat.read give the same result on every string. The key lemma is that when len returns None, go does too. We also compared 27 edge strings (empty, zeros, non-digits, 14 to 35 digits, both sides of the bound) on the interpreter, JS and C, and read_bounds and text_ops pass on every lane.

Thanks again for sticking with this through the review 🙏

Note: this comment was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

@Lorenzobattistela Lorenzobattistela mentioned this pull request Sep 25, 2026
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.

2 participants