Skip to content

Commit c940a31

Browse files
authored
Remove unreachable error branch by casting to UInt in parsing (#58480)
The `ncodeunits` field of substrings - and therefore also their size, cannot be negative. Ideally we would also remove the unnecessary checks in the other `tryparse` methods, that take start and end indices, but that should probably be rewritten to take a view, but that's a PR for another day.
1 parent 3466a16 commit c940a31

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

base/parse.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,12 @@ tryparse(::Type{Union{}}, slurp...; kwargs...) = error("cannot parse a value as
261261

262262
function tryparse(::Type{Float64}, s::String)
263263
hasvalue, val = ccall(:jl_try_substrtod, Tuple{Bool, Float64},
264-
(Ptr{UInt8},Csize_t,Csize_t), s, 0, sizeof(s))
264+
(Ptr{UInt8},Csize_t,Csize_t), s, 0, sizeof(s) % UInt)
265265
hasvalue ? val : nothing
266266
end
267267
function tryparse(::Type{Float64}, s::SubString{String})
268268
hasvalue, val = ccall(:jl_try_substrtod, Tuple{Bool, Float64},
269-
(Ptr{UInt8},Csize_t,Csize_t), s.string, s.offset, s.ncodeunits)
269+
(Ptr{UInt8},Csize_t,Csize_t), s.string, s.offset, s.ncodeunits % UInt)
270270
hasvalue ? val : nothing
271271
end
272272
function tryparse_internal(::Type{Float64}, s::String, startpos::Int, endpos::Int)
@@ -281,12 +281,12 @@ function tryparse_internal(::Type{Float64}, s::SubString{String}, startpos::Int,
281281
end
282282
function tryparse(::Type{Float32}, s::String)
283283
hasvalue, val = ccall(:jl_try_substrtof, Tuple{Bool, Float32},
284-
(Ptr{UInt8},Csize_t,Csize_t), s, 0, sizeof(s))
284+
(Ptr{UInt8},Csize_t,Csize_t), s, 0, sizeof(s) % UInt)
285285
hasvalue ? val : nothing
286286
end
287287
function tryparse(::Type{Float32}, s::SubString{String})
288288
hasvalue, val = ccall(:jl_try_substrtof, Tuple{Bool, Float32},
289-
(Ptr{UInt8},Csize_t,Csize_t), s.string, s.offset, s.ncodeunits)
289+
(Ptr{UInt8},Csize_t,Csize_t), s.string, s.offset, s.ncodeunits % UInt)
290290
hasvalue ? val : nothing
291291
end
292292
function tryparse_internal(::Type{Float32}, s::String, startpos::Int, endpos::Int)

0 commit comments

Comments
 (0)