Skip to content

Commit

Permalink
Base: fix tryparse(Bool, " ") (#42623)
Browse files Browse the repository at this point in the history
The `while` conditions were incorrectly ordered, resulting in
`BoundsError`.

(cherry picked from commit 9739f50)
  • Loading branch information
barucden authored and KristofferC committed Nov 11, 2021
1 parent de8c915 commit 1d03f3e
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 1d03f3e

Please sign in to comment.