Skip to content

Commit

Permalink
Base: fix tryparse(Bool, " ") (JuliaLang#42623)
Browse files Browse the repository at this point in the history
The `while` conditions were incorrectly ordered, resulting in
`BoundsError`.
  • Loading branch information
barucden authored and LilithHafner committed Mar 8, 2022
1 parent 2410473 commit d02a644
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions base/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ function tryparse_internal(::Type{Bool}, sbuff::Union{String,SubString{String}},
orig_end = endpos

# Ignore leading and trailing whitespace
while isspace(sbuff[startpos]) && startpos <= endpos
while startpos <= endpos && isspace(sbuff[startpos])
startpos = nextind(sbuff, startpos)
end
while isspace(sbuff[endpos]) && endpos >= startpos
while endpos >= startpos && isspace(sbuff[endpos])
endpos = prevind(sbuff, endpos)
end

Expand Down
7 changes: 7 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ end
@test_throws ArgumentError parse(Int, "2", base = 63)
end

@testset "issue #42616" begin
@test tryparse(Bool, "") === nothing
@test tryparse(Bool, " ") === nothing
@test_throws ArgumentError parse(Bool, "")
@test_throws ArgumentError parse(Bool, " ")
end

# issue #17333: tryparse should still throw on invalid base
for T in (Int32, BigInt), base in (0,1,100)
@test_throws ArgumentError tryparse(T, "0", base = base)
Expand Down

0 comments on commit d02a644

Please sign in to comment.