Sentinel values: -1 from a search used as a position, NaN from a parsed date, holes in an array, dividing by a filtered list #118

Merged
sionzee merged 35 commits from sentinel-values into main 2026-11-21 23:27:20 +01:00
Owner

Based on fail-closed-integration, which is main plus the open PRs #109 to #114 merged together. Once those are in, I'll rebase this onto main and retarget it. It is independent of #115, #116 and #117.

Some built-ins answer "there is no answer" with an ordinary value of the result's own type, and the code goes on with a wrong value instead of failing. Rust returns Option in these places; JavaScript returns -1 or NaN. These passed tsc strict, safe-ts check and oxlint:

items.splice(items.indexOf(item), 1);        // not found is -1: removes the last element
text.slice(0, text.indexOf(mark));           // -1 drops the last character
2026 - new Date(input).getFullYear()         // NaN for text that isn't a date
const slots: number[] = []; slots[n] = 1;    // leaves holes; a later slots[k]! reads undefined as a number

Four checks

Check Rule Level
A search result used as a position before it is compared with -1 new not-found-index warning
A date parsed from text, its number used before a validity check (also compared or subtracted) number-parse-nan, extended warning
A write that leaves holes in an array new sparse-array-write error
Dividing by the size of a collection the function just selected into new divide-by-length warning

The third one also repairs an existing guarantee. no-unchecked-cast accepts a[i]! where the checker proves 0 <= i < a.length, on the assumption that an index in bounds holds a value. After a write that can leave holes, the checker no longer proves a[i]! on that array.

Each rule was cut to what a real project showed

The implementer read every new finding on an adopter's project, in two rounds.

Division. A rule for any unchecked divisor gave 1,679 findings; of 24 read at random, none was real (map widths, sample rates, constants). A rule for any list's length gave 29, of which 2 were real: the rest were lists a caller guarantees non-empty. A warning that is wrong nine times in ten teaches the reflex Math.max(1, n), which turns a visible NaN into a silently wrong number. Both real findings divided by the length of a list the function had just filtered, so that is the rule now: the size of a collection made in the same function by filter, flatMap, or pushes under a condition. A parameter's length is not reported. On the adopter that leaves one finding, and it is real. Division by zero in general stays a listed class in the soundness corpus.

Search results. 41 findings, 7 real. Now recognised as found: an element pushed or includes-checked earlier in the function, one taken out of the list by find or an index read, the loop variable over the list. Test files are not checked, since a wrong index there fails the test's own assertion (22 findings, none real). That leaves 18 findings, 7 real:

  • real: markers searched in files read from disk (4), a key without its separator, a cookie without =, a missing heading;
  • not real: the element is known to be in the list from a caller or a helper (7), by its type (2), or by a fixed input format (2).

All seven real ones are searches in text. The nine array findings are all of the "known from a caller" kind; the rule still reports arrays, because splice(indexOf(x), 1) is the classic form of the bug and the fix is one comparison.

Dates. 14 findings, 8 real: dates from API and DOM text shown as NaN, and a warning silently skipped.

Holes. 1 finding: real holes, harmless today because every read uses ?? [].

The whole-program pass time on that project's largest app is unchanged (821 ms before, 815 ms after).

How it was checked

  • The Rust suite (334 tests), test:std, typecheck, the docs examples (875 compile), test:vite, the docs site build and the soundness corpus pass.
  • Lines that must be reported are, in the cases given to the implementation and in two further sets it had not seen. The same operations with the sentinel handled stay clean: a guard before the use, lastIndexOf("/") + 1, a length test before the division, a date from the clock, an array filled with push, by a counting loop or by a callback's index.
  • Ordinary array code (a grid built with push, a heap sift, a guarded swap) gets exactly the findings it had before.
  • The soundness corpus drops two classes on this branch (sentinel-splice-indexOf-neg1, bounds-sparse-hole).

Not done

  • No quick fixes for the new rules, and no std helper for removing an item or parsing a date, which would resolve most not-found-index warnings.
  • A divisor that is a parameter, a field, or any list the function did not select into.
  • Holes made in another file.

Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review and acceptance tests) in Claude Code.

Based on `fail-closed-integration`, which is `main` plus the open PRs #109 to #114 merged together. Once those are in, I'll rebase this onto `main` and retarget it. It is independent of #115, #116 and #117. Some built-ins answer "there is no answer" with an ordinary value of the result's own type, and the code goes on with a wrong value instead of failing. Rust returns `Option` in these places; JavaScript returns `-1` or `NaN`. These passed `tsc` strict, `safe-ts check` and oxlint: ```ts items.splice(items.indexOf(item), 1); // not found is -1: removes the last element text.slice(0, text.indexOf(mark)); // -1 drops the last character 2026 - new Date(input).getFullYear() // NaN for text that isn't a date const slots: number[] = []; slots[n] = 1; // leaves holes; a later slots[k]! reads undefined as a number ``` ## Four checks | Check | Rule | Level | |---|---|---| | A search result used as a position before it is compared with `-1` | new `not-found-index` | warning | | A date parsed from text, its number used before a validity check (also compared or subtracted) | `number-parse-nan`, extended | warning | | A write that leaves holes in an array | new `sparse-array-write` | error | | Dividing by the size of a collection the function just selected into | new `divide-by-length` | warning | The third one also repairs an existing guarantee. `no-unchecked-cast` accepts `a[i]!` where the checker proves `0 <= i < a.length`, on the assumption that an index in bounds holds a value. After a write that can leave holes, the checker no longer proves `a[i]!` on that array. ## Each rule was cut to what a real project showed The implementer read every new finding on an adopter's project, in two rounds. **Division.** A rule for any unchecked divisor gave 1,679 findings; of 24 read at random, none was real (map widths, sample rates, constants). A rule for any list's length gave 29, of which 2 were real: the rest were lists a caller guarantees non-empty. A warning that is wrong nine times in ten teaches the reflex `Math.max(1, n)`, which turns a visible `NaN` into a silently wrong number. Both real findings divided by the length of a list the function had just filtered, so that is the rule now: the size of a collection made in the same function by `filter`, `flatMap`, or pushes under a condition. A parameter's length is not reported. On the adopter that leaves one finding, and it is real. Division by zero in general stays a listed class in the soundness corpus. **Search results.** 41 findings, 7 real. Now recognised as found: an element pushed or `includes`-checked earlier in the function, one taken out of the list by `find` or an index read, the loop variable over the list. Test files are not checked, since a wrong index there fails the test's own assertion (22 findings, none real). That leaves 18 findings, 7 real: - real: markers searched in files read from disk (4), a key without its separator, a cookie without `=`, a missing heading; - not real: the element is known to be in the list from a caller or a helper (7), by its type (2), or by a fixed input format (2). All seven real ones are searches in text. The nine array findings are all of the "known from a caller" kind; the rule still reports arrays, because `splice(indexOf(x), 1)` is the classic form of the bug and the fix is one comparison. **Dates.** 14 findings, 8 real: dates from API and DOM text shown as `NaN`, and a warning silently skipped. **Holes.** 1 finding: real holes, harmless today because every read uses `?? []`. The whole-program pass time on that project's largest app is unchanged (821 ms before, 815 ms after). ## How it was checked - The Rust suite (334 tests), `test:std`, `typecheck`, the docs examples (875 compile), `test:vite`, the docs site build and the soundness corpus pass. - Lines that must be reported are, in the cases given to the implementation and in two further sets it had not seen. The same operations with the sentinel handled stay clean: a guard before the use, `lastIndexOf("/") + 1`, a length test before the division, a date from the clock, an array filled with `push`, by a counting loop or by a callback's index. - Ordinary array code (a grid built with `push`, a heap sift, a guarded swap) gets exactly the findings it had before. - The soundness corpus drops two classes on this branch (`sentinel-splice-indexOf-neg1`, `bounds-sparse-hole`). ## Not done - No quick fixes for the new rules, and no std helper for removing an item or parsing a date, which would resolve most `not-found-index` warnings. - A divisor that is a parameter, a field, or any list the function did not select into. - Holes made in another file. Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review and acceptance tests) in Claude Code.
Open: built-ins that answer "there is none" with a value of their own type passed tsc strict,
safe-ts check --idiomatic and oxlint, and the code went on with a wrong value:
`items.splice(items.indexOf(item), 1)` removing the last element, `text.slice(0,
text.indexOf(mark))`, `items[items.indexOf(from)] = to`, `sum / values.length` of an empty
list, `2026 - new Date(input).getFullYear()` for text that isn't a date. And
`const slots: number[] = []; slots[n] = 1` left holes that `slots[k]!` under
`k < slots.length` (accepted by no-unchecked-cast) read as a number.

Closed:
- not-found-index (new, warning): a search result (indexOf, lastIndexOf, findIndex,
  findLastIndex, search) used as a position (splice, slice, substring, at, with, ..., an index
  written to, an index read past it) before a comparison with -1 or `< 0`. The `+ 1` idioms of
  slice/substring/splice, answers known to be found (literal of a const list, a value typed
  `(typeof LIST)[number]`, after includes, the element of a for...of over the list or a copy)
  and project types with their own method are left alone; the message points to std's indexIn.
- number-parse-nan: Date.parse(text), and numbers read from new Date(text), before a
  Number.isNaN(date.getTime()) check. Dates from the clock, milliseconds or parts are valid.
- divide-by-length (new, warning): a division or remainder by a list's length where nothing
  shows the list isn't empty. Only lengths: any non-literal divisor was ~1,700 findings on an
  adopter with none real in a sample.
- sparse-array-write (new, error): a write past the end of an array filled by position, at
  `a.length + k`, or `delete a[i]`. Any write the bounds proof can't show inside an array (not
  a typed array) now also stops `a[i]!` from being proved on it in the file.
- The bounds proof also learns non-empty arrays from for...of bodies, per-element callbacks,
  assert.ok, copies after a check, tuples with a rest and literals; a search result's range;
  fill loops and compaction.

Soundness corpus: sentinel-splice-indexOf-neg1 and bounds-sparse-hole no longer escape.
division-by-zero-nan still does (its second program divides by a parameter).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sentinel values, review round: narrow divide-by-length and not-found-index to what the code shows
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m18s
Tests / test (pull_request) Successful in 6m39s
e9bcf8868a
Open (review of 2f411346): divide-by-length was right 2 times in 29 on an adopter, the others
lists a caller always fills; not-found-index was right 7 times in 41, 28 of the others an
element known to be in the list; `new Date(a) > new Date(b)` wasn't reported; `[1, 2, 3].length`
as a divisor and a fill by a forEach callback's index were.

Closed:
- divide-by-length looks only at the length or size of a collection the function itself made
  by selecting (`filter`, `flatMap`, `[]` / `new Set()` / `new Map()` grown only under a
  condition), not a parameter's, a field's or a module-level list's. On the adopter: 1
  finding, real. The acceptance lines that divide by a parameter's length are no longer
  reported, nor is the testing guide's `average(values)` (restored as accepted).
- not-found-index knows the element is there when the function pushed it, took it out of the
  list with find / at / an index, loops over the list or a filter or copy of it, or built the
  list literal from it; and doesn't check test files (1 of 23 findings there was real). On the
  adopter: 18 findings, 7 real (was 41, 7 real).
- number-parse-nan reads a date made inline in a comparison or subtraction.
- The bounds proof knows a literal's length, and that a forEach / map callback's index and
  `entries()`'s fill an empty array in order; a fill no longer fails on an alias made after it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee changed target branch from fail-closed-integration to ecmascript-builtins 2026-11-21 12:44:24 +01:00
Merge origin/ecmascript-builtins: the sentinel checks on top of the built-ins map and failure contracts
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m31s
Tests / test (pull_request) Successful in 7m9s
c451e43a9e
Both branches rebuilt the bounds proof. `Bounds` keeps both sets of answers (verdicts and the
divide-by-length / sparse-array-write diagnostics; numbers, non_empty, in_bounds for
builtins.rs). Each visitor both sides had is one method: the division and the hole checks look
at the facts before the operands are walked, the built-ins' questions are asked after
(visit_call_expression, visit_binary_expression, visit_assignment_expression; their
visit_new_expression as it was). shape.rs keeps both `new` cases; escaping.txt drops both
sides' closed classes; CHANGELOG keeps both sides' entries.

In meaning: the non-empty facts the sentinel work added (a for...of over the list, assert.ok,
a copy after a check) now also let `reduce` without an initial value go unreported there
(fixture cases added). A date check accepted by number-parse-nan (`String(d) === "Invalid
Date"`, `Number.isInteger(d.getTime())`) now also clears unhandled-failure's `toISOString`.
The not-found-index fixture's `with(-1, v)` example was rightly reported by unhandled-failure
(it throws for an empty list): the example uses `toSpliced` now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merge ecmascript-builtins: one finding for a declared handler slot
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m43s
Tests / test (pull_request) Successful in 7m33s
e802978283
sionzee changed target branch from ecmascript-builtins to main 2026-11-21 23:25:56 +01:00
Merge main
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m53s
Tests / test (pull_request) Successful in 8m51s
d9143d2116
Co-Authored-By: Claude Fable 5.2 <[email protected]>
sionzee deleted branch sentinel-values 2026-11-21 23:27:20 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!118
No description provided.