Inspiration
Julia is a programming language used widely in science and data work. Issue #64143, opened by MilesCranmerBot, showed that first("é", typemax(Int)) returned an empty string, while first("abc", typemax(Int)) returned "abc". Passing the largest whole number, typemax(Int), is a common way to say "no limit", so this could quietly empty out real text for anyone whose text has accents or other non English characters.
What it does
The merged change, JuliaLang/julia#64370, makes Julia's nextind function stop at the largest Int instead of wrapping around to a negative number. As a result, first(s, typemax(Int)) now returns the whole string for any text, and Iterators.partition(s, typemax(Int)), which splits text into chunks, returns one chunk instead of throwing a BoundsError.
How I built it
I reproduced the bug on a nightly build and on release 1.13.3, then asked nextind directly. For "é" it returned -9223372036854775808, the most negative Int.
The cause: Julia counts string positions in bytes, and é takes two. When nextind reaches the end of the text with steps left, it returns the end position plus the leftover. For "é" that sum is one more than the largest Int, so it overflows and wraps. The fix is one line:
return n > typemax(Int) - i ? typemax(Int) : i + n
I added tests to the existing string tests for nextind, first, last and partition, updated the nextind docs, loaded the patched function into a nightly build and ran the string test files. The new tests failed without the fix and passed with it.
Challenges I ran into
Choosing where to fix it. Clamping the count inside first would have fixed first only, and partition shares the same pattern. The overflow lives in nextind's own arithmetic, so that's where the fix went. The check also had to avoid overflowing itself, which is why it compares n with typemax(Int) - i instead of computing i + n first.
chop was the other challenge. The issue mentions it, and the fix covers it for a plain String, but a SubString input overflows in a separate place. I documented that in the PR and left it for a follow up rather than widening the change.
Accomplishments that I'm proud of
A single line, merged into Julia's core string code, that fixed two user facing functions at once. vtjnash approved it and IanButterworth merged it on 28 October 2026.
What I learned
Plain English text hid the bug: with one byte per letter, the sum landed exactly on the largest Int and never went past it. Testing with accented text was what exposed it. I also learned from vtjnash's comment on the PR some of the history behind how nextind treats positions past the end of a string.
What's next
The natural follow up is chop on a SubString with a huge count, which overflows in a separate place and needs its own small change.
The full story, with all the code, is on my blog: https://ashokramsi.hashnode.dev/julia-nextind-saturate
Built With
- github
- julia