Skip to content

Commit

Permalink
Restrict tryparse_internal on Bool to Union{String,SubString}
Browse files Browse the repository at this point in the history
Add a test of the error throwing branch of tryparse_internal
which has had a typo in it since a68f950
  • Loading branch information
tkelman committed Jul 2, 2016
1 parent 373db0f commit c7026a1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 8 additions & 4 deletions base/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,16 @@ function tryparse_internal{T<:Integer}(::Type{T}, s::AbstractString, startpos::I
return Nullable{T}(n)
end

function tryparse_internal(::Type{Bool}, sbuff::AbstractString, startpos::Int, endpos::Int, base::Integer, raise::Bool)
function tryparse_internal(::Type{Bool}, sbuff::Union{String,SubString},
startpos::Int, endpos::Int, base::Integer, raise::Bool)
len = endpos-startpos+1
p = pointer(sbuff)+startpos-1
(len == 4) && (0 == ccall(:memcmp, Int32, (Ptr{UInt8}, Ptr{UInt8}, UInt), p, "true", 4)) && (return Nullable(true))
(len == 5) && (0 == ccall(:memcmp, Int32, (Ptr{UInt8}, Ptr{UInt8}, UInt), p, "false", 5)) && (return Nullable(false))
raise && throw(ArgumentError("invalid Bool representation: $(repr(SubString(s,startpos,endpos)))"))
(len == 4) && (0 == ccall(:memcmp, Int32, (Ptr{UInt8}, Ptr{UInt8}, UInt),
p, "true", 4)) && (return Nullable(true))
(len == 5) && (0 == ccall(:memcmp, Int32, (Ptr{UInt8}, Ptr{UInt8}, UInt),
p, "false", 5)) && (return Nullable(false))
raise && throw(ArgumentError("invalid Bool representation: " *
repr(SubString(sbuff, startpos, endpos))))
Nullable{Bool}()
end

Expand Down
3 changes: 3 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,6 @@ end
@test get(tryparse(Bool, "false")) === get(Nullable{Bool}(false))
@test_throws ArgumentError parse(Int, "2", 1)
@test_throws ArgumentError parse(Int, "2", 63)

# error throwing branch from #10560
@test_throws ArgumentError Base.tryparse_internal(Bool, "foo", 1, 2, 10, true)

0 comments on commit c7026a1

Please sign in to comment.