Skip to content

Commit 0b48b91

Browse files
authored
Fix aliasing bug in copy!(x, x) for x::AbstractSet and x::AbstractDict, fixes #41268 (#44265)
1 parent 0a294f9 commit 0b48b91

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
@@ -189,7 +189,10 @@ empty(a::AbstractDict) = empty(a, keytype(a), valtype(a))
189189
empty(a::AbstractDict, ::Type{V}) where {V} = empty(a, keytype(a), V) # Note: this is the form which makes sense for `Vector`.
190190

191191
copy(a::AbstractDict) = merge!(empty(a), a)
192-
copy!(dst::AbstractDict, src::AbstractDict) = merge!(empty!(dst), src)
192+
function copy!(dst::AbstractDict, src::AbstractDict)
193+
dst === src && return dst
194+
merge!(empty!(dst), src)
195+
end
193196

194197
"""
195198
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
@@ -151,6 +151,9 @@ end
151151
@test s === copy!(s, BitSet(a)) == S(a)
152152
end
153153
end
154+
s = Set([1, 2])
155+
s2 = copy(s)
156+
@test copy!(s, s) == s2
154157
end
155158

156159
@testset "sizehint, empty" begin

0 commit comments

Comments
 (0)