diff --git a/base/parse.jl b/base/parse.jl index 202390278e9f5..6853c93a0de6b 100644 --- a/base/parse.jl +++ b/base/parse.jl @@ -175,6 +175,13 @@ function tryparse_internal(::Type{Bool}, sbuff::Union{String,SubString{String}}, return nothing end + if isnumeric(sbuff[1]) + intres = tryparse_internal(UInt8, sbuff, startpos, endpos, base, false) + (intres == 1) && return true + (intres == 0) && return false + raise && throw(ArgumentError("invalid Bool representation: $(repr(sbuff))")) + end + orig_start = startpos orig_end = endpos diff --git a/test/parse.jl b/test/parse.jl index 6d79116efb7d5..00d6ced46a26c 100644 --- a/test/parse.jl +++ b/test/parse.jl @@ -321,3 +321,14 @@ end @test eltype([tryparse(Float64, s) for s in String[]]) == Union{Nothing, Float64} @test eltype([tryparse(Complex{Int}, s) for s in String[]]) == Union{Nothing, Complex{Int}} end + +@testset "isssue #29980" begin + @test parse(Bool, "1") === true + @test parse(Bool, "01") === true + @test parse(Bool, "0") === false + @test parse(Bool, "000000000000000000000000000000000000000000000000001") === true + @test parse(Bool, "000000000000000000000000000000000000000000000000000") === false + @test_throws ArgumentError parse(Bool, "1000000000000000000000000000000000000000000000000000") + @test_throws ArgumentError parse(Bool, "2") + @test_throws ArgumentError parse(Bool, "02") +end