Skip to content

Commit 195b23e

Browse files
c42fKristofferC
authored andcommitted
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 510db13)
1 parent 90caa80 commit 195b23e

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

base/parse.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ function tryparse_internal(::Type{T}, s::AbstractString, startpos::Int, endpos::
120120
_Z = UInt32('Z')
121121
_z = UInt32('z')
122122
while n <= m
123-
_c = UInt32(c)
123+
# Fast path from `UInt32(::Char)`; non-ascii will be >= 0x80
124+
_c = reinterpret(UInt32, c) >> 24
124125
d::T = _0 <= _c <= _9 ? _c-_0 :
125126
_A <= _c <= _Z ? _c-_A+ UInt32(10) :
126127
_a <= _c <= _z ? _c-_a+a : base
@@ -139,7 +140,8 @@ function tryparse_internal(::Type{T}, s::AbstractString, startpos::Int, endpos::
139140
end
140141
(T <: Signed) && (n *= sgn)
141142
while !isspace(c)
142-
_c = UInt32(c)
143+
# Fast path from `UInt32(::Char)`; non-ascii will be >= 0x80
144+
_c = reinterpret(UInt32, c) >> 24
143145
d::T = _0 <= _c <= _9 ? _c-_0 :
144146
_A <= _c <= _Z ? _c-_A+ UInt32(10) :
145147
_a <= _c <= _z ? _c-_a+a : base

test/strings/basic.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,12 @@ end
377377
@test tryparse(Float32, "32o") === nothing
378378
end
379379

380+
@testset "tryparse invalid chars" begin
381+
# #32314: tryparse shouldn't throw, even given strings with invalid Chars
382+
@test tryparse(UInt8, "\xb5") === nothing
383+
@test tryparse(UInt8, "100\xb5") === nothing # Code path for numeric overflow
384+
end
385+
380386
import Unicode
381387

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

0 commit comments

Comments
 (0)