Project

General

Profile

Actions

Bug #15834

closed

Mutating the result of SortedSet.to_a mutates the original set, potentially violating the set's guarantees

Added by viko (Viko Viko) almost 5 years ago. Updated almost 4 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]
[ruby-core:92578]
Tags:

Description

require 'set'

set = SortedSet[1, 2, 3]
set.to_a << -1
set.to_a << 3
set.each do |x|
  puts x
end

puts
set.add -2
set.each do |x|
  puts x
end
1
2
3
-1
3

-2
1
2
3

As can be seen, this breaks SortedSet's guarantee of ordering and Set's guarantee of uniqueness, besides being strange behaviour. Mutating the set normally undoes the changes. This bug only seems to exist for SortedSet, and not for Set itself. Workaround: dup.


Files

sorted-set-to_a-mutate-15834 (1.02 KB) sorted-set-to_a-mutate-15834 jeremyevans0 (Jeremy Evans), 10/27/2019 11:08 PM

Updated by marcandre (Marc-Andre Lafortune) almost 5 years ago

  • Assignee set to knu (Akinori MUSHA)

Updated by jeremyevans0 (Jeremy Evans) over 4 years ago

Attached is a patch with a test for fixing this.

Actions #3

Updated by jeremyevans (Jeremy Evans) almost 4 years ago

  • Status changed from Open to Closed

Applied in changeset git|a6cf2f3d22a78aeae9d7f36cc78a195deb686705.


Make mutating the result of SortedSet#to_a not affect the set

Fixes [Bug #15834]

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0