From 9739f504e584eefe3cdd0e1f1255893dfe3e39d9 Mon Sep 17 00:00:00 2001 From: Denis Barucic Date: Wed, 13 Oct 2021 16:04:01 +0200 Subject: [PATCH] Base: fix `tryparse(Bool, " ")` (#42623) The `while` conditions were incorrectly ordered, resulting in `BoundsError`. --- base/parse.jl | 4 ++-- test/parse.jl | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/base/parse.jl b/base/parse.jl index 1097e8a19b804..1c911c96e1479 100644 --- a/base/parse.jl +++ b/base/parse.jl @@ -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 diff --git a/test/parse.jl b/test/parse.jl index 2deeecd516f2a..ae07936b3a18e 100644 --- a/test/parse.jl +++ b/test/parse.jl @@ -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)