Skip to content

Commit 1d03f3e

Browse files
barucdenKristofferC
authored andcommitted
Base: fix tryparse(Bool, " ") (#42623)
The `while` conditions were incorrectly ordered, resulting in `BoundsError`. (cherry picked from commit 9739f50)
1 parent de8c915 commit 1d03f3e

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

base/parse.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ function tryparse_internal(::Type{Bool}, sbuff::Union{String,SubString{String}},
194194
orig_end = endpos
195195

196196
# Ignore leading and trailing whitespace
197-
while isspace(sbuff[startpos]) && startpos <= endpos
197+
while startpos <= endpos && isspace(sbuff[startpos])
198198
startpos = nextind(sbuff, startpos)
199199
end
200-
while isspace(sbuff[endpos]) && endpos >= startpos
200+
while endpos >= startpos && isspace(sbuff[endpos])
201201
endpos = prevind(sbuff, endpos)
202202
end
203203

test/parse.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@ end
236236
@test_throws ArgumentError parse(Int, "2", base = 63)
237237
end
238238

239+
@testset "issue #42616" begin
240+
@test tryparse(Bool, "") === nothing
241+
@test tryparse(Bool, " ") === nothing
242+
@test_throws ArgumentError parse(Bool, "")
243+
@test_throws ArgumentError parse(Bool, " ")
244+
end
245+
239246
# issue #17333: tryparse should still throw on invalid base
240247
for T in (Int32, BigInt), base in (0,1,100)
241248
@test_throws ArgumentError tryparse(T, "0", base = base)

0 commit comments

Comments
 (0)