Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tryparse for invalid Chars #32351

Merged
merged 1 commit into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions base/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ function tryparse_internal(::Type{T}, s::AbstractString, startpos::Int, endpos::
_Z = UInt32('Z')
_z = UInt32('z')
while n <= m
_c = UInt32(c)
# Fast path from `UInt32(::Char)`; non-ascii will be >= 0x80
_c = reinterpret(UInt32, c) >> 24
d::T = _0 <= _c <= _9 ? _c-_0 :
_A <= _c <= _Z ? _c-_A+ UInt32(10) :
_a <= _c <= _z ? _c-_a+a : base
Expand All @@ -142,7 +143,8 @@ function tryparse_internal(::Type{T}, s::AbstractString, startpos::Int, endpos::
end
(T <: Signed) && (n *= sgn)
while !isspace(c)
_c = UInt32(c)
# Fast path from `UInt32(::Char)`; non-ascii will be >= 0x80
_c = reinterpret(UInt32, c) >> 24
d::T = _0 <= _c <= _9 ? _c-_0 :
_A <= _c <= _Z ? _c-_A+ UInt32(10) :
_a <= _c <= _z ? _c-_a+a : base
Expand Down
6 changes: 6 additions & 0 deletions test/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ end
@test tryparse(Float32, "32o") === nothing
end

@testset "tryparse invalid chars" begin
# #32314: tryparse shouldn't throw, even given strings with invalid Chars
@test tryparse(UInt8, "\xb5") === nothing
@test tryparse(UInt8, "100\xb5") === nothing # Code path for numeric overflow
end

import Unicode

@testset "issue #10994: handle embedded NUL chars for string parsing" begin
Expand Down