Fix `Char::Reader.new` to raise on an invalid position #17416
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
youdie006
Follow-up to #17382. That made
#pos=reject a position outside0..bytesize, but the constructor writes@posunchecked, so the same field has two public entry points that answer differently.byte_atis@string.to_unsafe[i].to_u32, so an out-of-rangeposreads through a raw pointer. Measured on master:| |
Char::Reader.new("abc", p)|reader.pos = p||---|---|---|
| -1000 | accepted,
current_charis the NUL char |IndexError|| -1 | accepted,
current_charis the NUL char |IndexError|| 5 | accepted,
current_charis the NUL char |IndexError|| 1000 | accepted,
current_charis U+0001 |IndexError|| 3 (== bytesize) | accepted | accepted |
The guard is deliberately written the same way as
#pos=sopos == bytesizestays legal;reader_spec.cr:30relies on that.<details>
<summary>Verification</summary>
Crystal 1.21.0, stdlib source from this tree,
crystal specin the official image.| row |
src/char/reader.crmd5 | result ||---|---|---|
| pristine + new spec |
0da9bda4a98ab66c570bf719ec3b9bf5| 17 examples, 1 failure, only the new.newone; #17382's#posexample 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.newexample 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,regexgave 2912 examples, 0 failures, 5 pending.Both in-tree callers that pass an explicit
posalready validate it, so nothing shipping changes behaviour:src/string.cr:997guards withbyte_index && byte_index < @bytesize, andsrc/string.cr:3506raises unlesschar_index_to_byte_indexreturned 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.