Skip to content

Commit

Permalink
Fix #29451: parse(Int, s::AbstractString) when ncodeunits(s) > lastin…
Browse files Browse the repository at this point in the history
…dex(s) (#29465)

(cherry picked from commit a984ad0)
  • Loading branch information
samoconnor authored and KristofferC committed Feb 20, 2020
1 parent 3a89bd2 commit a9dcb6a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function parseint_preamble(signed::Bool, base::Int, s::AbstractString, startpos:
(j == 0) && (return 0, 0, 0)

if base == 0
if c == '0' && i <= ncodeunits(s)
if c == '0' && i <= endpos
c, i = iterate(s,i)::Tuple{Char, Int}
base = c=='b' ? 2 : c=='o' ? 8 : c=='x' ? 16 : 10
if base != 10
Expand Down
10 changes: 10 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
@test_throws ArgumentError parse(Int, 'a')
@test_throws ArgumentError parse(Int,typemax(Char))

# Issue 29451
struct Issue29451String <: AbstractString end
Base.ncodeunits(::Issue29451String) = 12345
Base.lastindex(::Issue29451String) = 1
Base.isvalid(::Issue29451String, i::Integer) = i == 1
Base.iterate(::Issue29451String, i::Integer=1) = i == 1 ? ('0', 2) : nothing

@test Issue29451String() == "0"
@test parse(Int, Issue29451String()) == 0

# Issue 20587
for T in Any[BigInt, Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8]
T === BigInt && continue # TODO: make BigInt pass this test
Expand Down

0 comments on commit a9dcb6a

Please sign in to comment.