Skip to content

Commit d19bc3f

Browse files
Ellipse0934KristofferC
authored andcommittedFeb 23, 2022
Fix aliasing bug in copy!(x, x) for x::AbstractSet and x::AbstractDict, fixes #41268 (#44265)
(cherry picked from commit 0b48b91)
1 parent d0f0726 commit d19bc3f

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed
 

‎base/abstractdict.jl‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ empty(a::AbstractDict) = empty(a, keytype(a), valtype(a))
155155
empty(a::AbstractDict, ::Type{V}) where {V} = empty(a, keytype(a), V) # Note: this is the form which makes sense for `Vector`.
156156

157157
copy(a::AbstractDict) = merge!(empty(a), a)
158-
copy!(dst::AbstractDict, src::AbstractDict) = merge!(empty!(dst), src)
158+
function copy!(dst::AbstractDict, src::AbstractDict)
159+
dst === src && return dst
160+
merge!(empty!(dst), src)
161+
end
159162

160163
"""
161164
merge!(d::AbstractDict, others::AbstractDict...)

‎base/abstractset.jl‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
eltype(::Type{<:AbstractSet{T}}) where {T} = @isdefined(T) ? T : Any
44
sizehint!(s::AbstractSet, n) = nothing
55

6-
copy!(dst::AbstractSet, src::AbstractSet) = union!(empty!(dst), src)
6+
function copy!(dst::AbstractSet, src::AbstractSet)
7+
dst === src && return dst
8+
union!(empty!(dst), src)
9+
end
710

811
## set operations (union, intersection, symmetric difference)
912

‎test/dict.jl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,8 @@ end
11791179
@test s === copy!(s, Base.ImmutableDict(a[])) == Dict(a[])
11801180
end
11811181
end
1182+
s2 = copy(s)
1183+
@test copy!(s, s) == s2
11821184
end
11831185

11841186
@testset "map!(f, values(dict))" begin

‎test/sets.jl‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ end
147147
@test s === copy!(s, BitSet(a)) == S(a)
148148
end
149149
end
150+
s = Set([1, 2])
151+
s2 = copy(s)
152+
@test copy!(s, s) == s2
150153
end
151154

152155
@testset "sizehint, empty" begin

0 commit comments

Comments
 (0)
Please sign in to comment.