Skip to content

Commit

Permalink
Remove unreachable error branch in memset calls (and in repeat) (#55985)
Browse files Browse the repository at this point in the history
Some places use the pattern memset(A, v, length(A)), which requires a
conversion UInt(length(A)). This is technically fallible, but can't
actually fail when A is a Memory or Array.
Remove the dead error branch by casting to UInt instead.

Similarly, in repeat(x, r), r is first checked to be nonnegative, then
converted to UInt, then used in multiple calls where it is converted to
UInt each time. Here, only do it once.
  • Loading branch information
jakobnissen authored Oct 6, 2024
1 parent 03c0b89 commit ab6df86
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ function fill!(a::Union{Array{UInt8}, Array{Int8}}, x::Integer)
ref = a.ref
t = @_gc_preserve_begin ref
p = unsafe_convert(Ptr{Cvoid}, ref)
memset(p, x isa eltype(a) ? x : convert(eltype(a), x), length(a))
memset(p, x isa eltype(a) ? x : convert(eltype(a), x), length(a) % UInt)
@_gc_preserve_end t
return a
end
Expand Down
2 changes: 1 addition & 1 deletion base/genericmemory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function fill!(a::Union{Memory{UInt8}, Memory{Int8}}, x::Integer)
t = @_gc_preserve_begin a
p = unsafe_convert(Ptr{Cvoid}, a)
T = eltype(a)
memset(p, x isa T ? x : convert(T, x), length(a))
memset(p, x isa T ? x : convert(T, x), length(a) % UInt)
@_gc_preserve_end t
return a
end
Expand Down
2 changes: 1 addition & 1 deletion base/iddict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function empty!(d::IdDict)
d.ht = Memory{Any}(undef, 32)
ht = d.ht
t = @_gc_preserve_begin ht
memset(unsafe_convert(Ptr{Cvoid}, ht), 0, sizeof(ht))
memset(unsafe_convert(Ptr{Cvoid}, ht), 0, sizeof(ht) % UInt)
@_gc_preserve_end t
d.ndel = 0
d.count = 0
Expand Down
3 changes: 2 additions & 1 deletion base/strings/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,10 @@ julia> repeat('A', 3)
```
"""
function repeat(c::AbstractChar, r::Integer)
r < 0 && throw(ArgumentError("can't repeat a character $r times"))
r = UInt(r)::UInt
c = Char(c)::Char
r == 0 && return ""
r < 0 && throw(ArgumentError("can't repeat a character $r times"))
u = bswap(reinterpret(UInt32, c))
n = 4 - (leading_zeros(u | 0xff) >> 3)
s = _string_n(n*r)
Expand Down
1 change: 1 addition & 0 deletions base/strings/substring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ end

function repeat(s::Union{String, SubString{String}}, r::Integer)
r < 0 && throw(ArgumentError("can't repeat a string $r times"))
r = UInt(r)::UInt
r == 0 && return ""
r == 1 && return String(s)
n = sizeof(s)
Expand Down

0 comments on commit ab6df86

Please sign in to comment.