← Back to Pull requests

Fix `Char::Reader.new` to raise on an invalid position #17416

Closed
unknown closed this pull request without merging on Sep 19, 2026 16:10 UTC
View on GitHub
unknown unknown commented on Sep 15, 2026 09:41 UTC
Author

Follow-up to #17382. That made #pos= reject a position outside 0..bytesize, but the constructor writes @pos unchecked, so the same field has two public entry points that answer differently.

byte_at is @string.to_unsafe[i].to_u32, so an out-of-range pos reads through a raw pointer. Measured on master:

| | Char::Reader.new("abc", p) | reader.pos = p |
|---|---|---|
| -1000 | accepted, current_char is the NUL char | IndexError |
| -1 | accepted, current_char is the NUL char | IndexError |
| 5 | accepted, current_char is the NUL char | IndexError |
| 1000 | accepted, current_char is U+0001 | IndexError |
| 3 (== bytesize) | accepted | accepted |

The guard is deliberately written the same way as #pos= so pos == bytesize stays legal; reader_spec.cr:30 relies on that.

<details>
<summary>Verification</summary>

Crystal 1.21.0, stdlib source from this tree, crystal spec in the official image.

| row | src/char/reader.cr md5 | result |
|---|---|---|
| pristine + new spec | 0da9bda4a98ab66c570bf719ec3b9bf5 | 17 examples, 1 failure, only the new .new one; #17382's #pos example passes |
| this change | 97e93e63c64ffbe6be4fb06896451622 | 17 examples, 0 failures |
| guard only pos < 0 | 1349cd2594f0258517a6244834cdb430 | 17 examples, 1 failure still, because 5 and 1000 are accepted |
| this change, with #17382 reverted | c8d3f2ad20aa2182e573027d5ad3efc0 | 3 failures: #17382's own example plus both empty-string scenarios; the new .new example passes |

The last row is the check that this does not stand in for #17382: the two guards are independent, and removing the setter's guard still breaks the setter.

The third row is why the fix is not the one-liner the title of #17382 suggests. Guarding only the negative side leaves Char::Reader.new("abc", 1000) reading out of bounds.

Adjacent suites, all green with the change: char/reader, string, string_scanner, path, file/match, mime/media_type, time/format, regex gave 2912 examples, 0 failures, 5 pending.

Both in-tree callers that pass an explicit pos already validate it, so nothing shipping changes behaviour: src/string.cr:997 guards with byte_index && byte_index < @bytesize, and src/string.cr:3506 raises unless char_index_to_byte_index returned an index.

Not run: the full compiler spec suite.

</details>

Note on #17405: it edits the next line of this same constructor (decode_current_char unless @string.empty?) and adds no bounds validation, so the two are orthogonal. Whichever lands second needs a one-line context rebase. Happy to rebase onto it, or to have this folded in there instead.

Disclosure: written with AI assistance (Claude). Every number above is measured.

Diff: 344 lines (+258 / -86) across 4 files Reviewer: youdie006 Review duration: 6148m
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.42) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.42 · 344 lines diff across 4 files
0.42
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.350
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #17416"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 42.4% (MEDIUM) · Higher = larger blast radius
42.4%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 16.7% · Safe to merge
16.7%