From 195b23eb61ced1cfc63e75aa15ad19592fd9be5e Mon Sep 17 00:00:00 2001 From: Chris Foster Date: Thu, 20 Jun 2019 15:01:45 +1000 Subject: [PATCH] Fix tryparse for invalid Chars (#32351) This fix adapts the fast path from UInt32(::Char) to the case of digit parsing. (cherry picked from commit 510db132fecd6a1ec742c0034cdf0618e863e128) --- base/parse.jl | 6 ++++-- test/strings/basic.jl | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/base/parse.jl b/base/parse.jl index 9cde8054435bd5..c9b252100d9dea 100644 --- a/base/parse.jl +++ b/base/parse.jl @@ -120,7 +120,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 @@ -139,7 +140,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 diff --git a/test/strings/basic.jl b/test/strings/basic.jl index b2e14ac2f5451c..851c258c2f6e34 100644 --- a/test/strings/basic.jl +++ b/test/strings/basic.jl @@ -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