Repository navigation
Nat.read compares its bound as fifteen digits, so a checked read of 7 finishes - #1009
Conversation
… 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
left a comment
There was a problem hiding this comment.
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}andNat.read("000") == Some{0n};Nat.read("12x") == None{}andNat.read("281474976710656") == None{};Nat.read("1000000000000000") == None{}andNat.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.
|
Leading zeros are stripped, then fewer than fifteen digits are in range and exactly fifteen compare as text with
|
|
Thank you @jkbennitt! This fixes #1008, and the leading-zero handling and the sixteen-digit cases in Right after the merge we simplified it a little in ff4bb00:
To be sure the change is safe, we wrote a Bend proof, checked against your version of 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. |
Summary
Nat.readdivided the unary nat2^48 - 1on every digit, so{Nat.read("7") == Some{7n}}never finished (#1008). The bound is the fifteen-digit text281474976710655. Leading zeros are stripped first. A shorter all-digit string is in range, a longer one isNone, and fifteen digits compare as text.Nat.read.maxis gone; nothing in the repo called it.The length check is exact, so a sixteen-digit string is
Noneeven when its text sorts before the bound, and leading zeros still read as the number they spell (0000000000000007is7).The values pinned by
tests/base/read_bounds.bendare unchanged, including10^14through2^48 - 1(a plain digit cap once rejected that range).Test plan
tests/base/read_bounds.bend --check-onlycloses, including equalities for"7","007","000","","12x","1000000000000000","281474976710656", andNat.show(7n)#|lines from the interpreter, from emitted JS, and from the C binary, including the sixteen-digit and leading-zero casestests/base/text_ops.bendstill prints14