From d11c8b67625efc81e1d13ba1c72995489f51bfb1 Mon Sep 17 00:00:00 2001 From: nick evans Date: Fri, 17 Jan 2025 08:55:15 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20`SequenceSet#merge`=20with?= =?UTF-8?q?=20another=20SequenceSet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Most methods convert their inputs into an array of range tuples. For efficiency, SequenceSet inputs just use the internal `@tuples` array directly. Unfortunately, the internal tuples arrays were also reused, which could cause a variety of bugs. Fortunately, the only bug I experienced was that adding a frozen SequenceSet would result in frozen tuples being added to a mutable set. But this could also result in modifications to one SequenceSet affecting another SequenceSet! --- lib/net/imap/sequence_set.rb | 4 ++-- test/net/imap/test_sequence_set.rb | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/net/imap/sequence_set.rb b/lib/net/imap/sequence_set.rb index cb3e41a3..5f10a187 100644 --- a/lib/net/imap/sequence_set.rb +++ b/lib/net/imap/sequence_set.rb @@ -1342,8 +1342,8 @@ def tuple_add(tuple) modifying! min, max = tuple lower, lower_idx = tuple_gte_with_index(min - 1) - if lower.nil? then tuples << tuple - elsif (max + 1) < lower.first then tuples.insert(lower_idx, tuple) + if lower.nil? then tuples << [min, max] + elsif (max + 1) < lower.first then tuples.insert(lower_idx, [min, max]) else tuple_coalesce(lower, lower_idx, min, max) end end diff --git a/test/net/imap/test_sequence_set.rb b/test/net/imap/test_sequence_set.rb index e2f1ce7e..fa91288e 100644 --- a/test/net/imap/test_sequence_set.rb +++ b/test/net/imap/test_sequence_set.rb @@ -359,6 +359,11 @@ def obj.to_sequence_set; 192_168.001_255 end assert_equal seqset["1:3,5,7:9"], seqset["1,3,5,7:8"].merge(seqset["2,8:9"]) assert_equal seqset["1:*"], seqset["5:*"].merge(1..4) assert_equal seqset["1:5"], seqset["1,3,5"].merge(seqset["2,4"]) + # when merging frozen SequenceSet + set = SequenceSet.new + set.merge SequenceSet[1, 3, 5] + set.merge SequenceSet[2..33] + assert_equal seqset[1..33], set end test "set - other" do