Skip to content

Commit 3cfe9ce

Browse files
samoconnorKristofferC
authored andcommitted
Fix #29451: parse(Int, s::AbstractString) when ncodeunits(s) > lastindex(s) (#29465)
(cherry picked from commit a984ad0)
1 parent b226b2e commit 3cfe9ce

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

base/parse.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function parseint_preamble(signed::Bool, base::Int, s::AbstractString, startpos:
7373
(j == 0) && (return 0, 0, 0)
7474

7575
if base == 0
76-
if c == '0' && i <= ncodeunits(s)
76+
if c == '0' && i <= endpos
7777
c, i = iterate(s,i)::Tuple{Char, Int}
7878
base = c=='b' ? 2 : c=='o' ? 8 : c=='x' ? 16 : 10
7979
if base != 10

test/parse.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@
3030
@test_throws ArgumentError parse(Int, 'a')
3131
@test_throws ArgumentError parse(Int,typemax(Char))
3232

33+
# Issue 29451
34+
struct Issue29451String <: AbstractString end
35+
Base.ncodeunits(::Issue29451String) = 12345
36+
Base.lastindex(::Issue29451String) = 1
37+
Base.isvalid(::Issue29451String, i::Integer) = i == 1
38+
Base.iterate(::Issue29451String, i::Integer=1) = i == 1 ? ('0', 2) : nothing
39+
40+
@test Issue29451String() == "0"
41+
@test parse(Int, Issue29451String()) == 0
42+
3343
# Issue 20587
3444
for T in Any[BigInt, Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8]
3545
T === BigInt && continue # TODO: make BigInt pass this test

0 commit comments

Comments
 (0)