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
Loading…
Reference in a new issue
No description provided.
Delete branch "sentinel-values"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Based on
fail-closed-integration, which ismainplus the open PRs #109 to #114 merged together. Once those are in, I'll rebase this ontomainand 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
Optionin these places; JavaScript returns-1orNaN. These passedtscstrict,safe-ts checkand oxlint:Four checks
-1not-found-indexnumber-parse-nan, extendedsparse-array-writedivide-by-lengthThe third one also repairs an existing guarantee.
no-unchecked-castacceptsa[i]!where the checker proves0 <= 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 provesa[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 visibleNaNinto 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 byfilter,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 byfindor 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:=, a missing heading;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
test:std,typecheck, the docs examples (875 compile),test:vite, the docs site build and the soundness corpus pass.lastIndexOf("/") + 1, a length test before the division, a date from the clock, an array filled withpush, by a counting loop or by a callback's index.push, a heap sift, a guarded swap) gets exactly the findings it had before.sentinel-splice-indexOf-neg1,bounds-sparse-hole).Not done
not-found-indexwarnings.Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review and acceptance tests) in Claude Code.
declareis a cast #119Resultkept in a local #120promise-as-value,floating-promise#121this, parts handed out, kept copies, wider aliases, loans alive at once #122