From 8820a14aefed308d6c0bb5a3abc435b2bbf17cc1 Mon Sep 17 00:00:00 2001 From: kmz <6367575+kmz@users.noreply.github.com> Date: Tue, 24 Nov 2026 21:37:08 +0900 Subject: [PATCH 1/2] Fix SortedSet set-operation checks throwing for views IsSupersetOf/IsProperSupersetOf prune this set with GetViewBetween(other.Min, other.Max), and IsSubsetOf/IsProperSubsetOf prune the argument the same way. When the set being pruned is itself a TreeSubSet view and the bounds fall outside the view, GetViewBetween throws ArgumentOutOfRangeException instead of the checks returning false. Skip the pruning when the set is a view; the containment loops are correct without it. --- .../System/Collections/Generic/SortedSet.cs | 11 ++++++--- .../SortedSet/SortedSet.TreeSubSet.Tests.cs | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.cs b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.cs index 60b7c226d6ad04..f613ecbc1b4f02 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.cs @@ -1193,7 +1193,9 @@ public bool IsSubsetOf(IEnumerable other) private bool IsSubsetOfSortedSetWithSameComparer(SortedSet asSorted) { - SortedSet prunedOther = asSorted.GetViewBetween(Min, Max); + // The other set may itself be a view, in which case GetViewBetween would throw when this set's + // bounds fall outside it. The plain containment checks below handle that correctly. + SortedSet prunedOther = asSorted.treeSubset == null ? asSorted.GetViewBetween(Min, Max) : asSorted; foreach (T item in this) { if (!prunedOther.Contains(item)) @@ -1240,7 +1242,9 @@ public bool IsSupersetOf(IEnumerable other) { if (Count < asSorted.Count) return false; - SortedSet pruned = GetViewBetween(asSorted.Min, asSorted.Max); + // This may itself be a view, in which case GetViewBetween would throw when the other set's + // bounds fall outside this view. The plain containment checks below handle that correctly. + SortedSet pruned = treeSubset == null ? GetViewBetween(asSorted.Min, asSorted.Max) : this; foreach (T item in asSorted) { if (!pruned.Contains(item)) @@ -1269,7 +1273,8 @@ public bool IsProperSupersetOf(IEnumerable other) { if (asSorted.Count >= Count) return false; - SortedSet pruned = GetViewBetween(asSorted.Min, asSorted.Max); + // See the note in IsSupersetOf: pruning is only safe when this is not a view. + SortedSet pruned = treeSubset == null ? GetViewBetween(asSorted.Min, asSorted.Max) : this; foreach (T item in asSorted) { if (!pruned.Contains(item)) diff --git a/src/libraries/System.Collections/tests/Generic/SortedSet/SortedSet.TreeSubSet.Tests.cs b/src/libraries/System.Collections/tests/Generic/SortedSet/SortedSet.TreeSubSet.Tests.cs index 7a0150fa6fdda7..0a3c54f279bc2c 100644 --- a/src/libraries/System.Collections/tests/Generic/SortedSet/SortedSet.TreeSubSet.Tests.cs +++ b/src/libraries/System.Collections/tests/Generic/SortedSet/SortedSet.TreeSubSet.Tests.cs @@ -36,6 +36,29 @@ public void ExceptWith(bool useSelf) Assert.Equal(0, view.Count); Assert.Equal(new[] { 1, 4 }, set); } + + [Fact] + public static void SetOperationChecksDoNotThrowWhenViewBoundsExcluded() + { + SortedSet set = new SortedSet { 1, 2, 3, 4 }; + SortedSet view = set.GetViewBetween(2, 3); + + // The other set's bounds fall outside the view. These must report false, not throw. + Assert.False(view.IsSupersetOf(new SortedSet { 1 })); + Assert.False(view.IsSupersetOf(new SortedSet { 2, 9 })); + Assert.False(view.IsProperSupersetOf(new SortedSet { 1 })); + + // The same when the view is the argument rather than the instance. + Assert.False(new SortedSet { 5 }.IsSubsetOf(view)); + Assert.False(new SortedSet { 5 }.IsProperSubsetOf(view)); + + // Results that are unaffected by the pruning fast path must not change. + Assert.True(view.IsSupersetOf(new SortedSet { 2, 3 })); + Assert.False(view.IsSupersetOf(new SortedSet { 4 })); + Assert.True(view.IsProperSupersetOf(new SortedSet { 2 })); + Assert.True(view.IsSubsetOf(set)); + Assert.True(view.IsProperSubsetOf(set)); + } } public class SortedSet_TreeSubset_String_Tests : SortedSet_TreeSubset_Tests From 364b2569a5cd7e050ca690fa02e6b76879166d90 Mon Sep 17 00:00:00 2001 From: kmz <6367575+kmz@users.noreply.github.com> Date: Tue, 24 Nov 2026 22:03:52 +0900 Subject: [PATCH 2/2] Use pattern matching for the view check and tighten the regression test --- .../src/System/Collections/Generic/SortedSet.cs | 14 ++++++-------- .../SortedSet/SortedSet.TreeSubSet.Tests.cs | 2 +- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.cs b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.cs index f613ecbc1b4f02..9fafad2564438e 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.cs @@ -1193,9 +1193,8 @@ public bool IsSubsetOf(IEnumerable other) private bool IsSubsetOfSortedSetWithSameComparer(SortedSet asSorted) { - // The other set may itself be a view, in which case GetViewBetween would throw when this set's - // bounds fall outside it. The plain containment checks below handle that correctly. - SortedSet prunedOther = asSorted.treeSubset == null ? asSorted.GetViewBetween(Min, Max) : asSorted; + // GetViewBetween on a view throws if the bounds fall outside the view, so skip the pruning for views. + SortedSet prunedOther = asSorted is TreeSubSet ? asSorted : asSorted.GetViewBetween(Min, Max); foreach (T item in this) { if (!prunedOther.Contains(item)) @@ -1242,9 +1241,8 @@ public bool IsSupersetOf(IEnumerable other) { if (Count < asSorted.Count) return false; - // This may itself be a view, in which case GetViewBetween would throw when the other set's - // bounds fall outside this view. The plain containment checks below handle that correctly. - SortedSet pruned = treeSubset == null ? GetViewBetween(asSorted.Min, asSorted.Max) : this; + // GetViewBetween on a view throws if the bounds fall outside the view, so skip the pruning for views. + SortedSet pruned = this is TreeSubSet ? this : GetViewBetween(asSorted.Min, asSorted.Max); foreach (T item in asSorted) { if (!pruned.Contains(item)) @@ -1273,8 +1271,8 @@ public bool IsProperSupersetOf(IEnumerable other) { if (asSorted.Count >= Count) return false; - // See the note in IsSupersetOf: pruning is only safe when this is not a view. - SortedSet pruned = treeSubset == null ? GetViewBetween(asSorted.Min, asSorted.Max) : this; + // GetViewBetween on a view throws if the bounds fall outside the view, so skip the pruning for views. + SortedSet pruned = this is TreeSubSet ? this : GetViewBetween(asSorted.Min, asSorted.Max); foreach (T item in asSorted) { if (!pruned.Contains(item)) diff --git a/src/libraries/System.Collections/tests/Generic/SortedSet/SortedSet.TreeSubSet.Tests.cs b/src/libraries/System.Collections/tests/Generic/SortedSet/SortedSet.TreeSubSet.Tests.cs index 0a3c54f279bc2c..ea5b3343446e4f 100644 --- a/src/libraries/System.Collections/tests/Generic/SortedSet/SortedSet.TreeSubSet.Tests.cs +++ b/src/libraries/System.Collections/tests/Generic/SortedSet/SortedSet.TreeSubSet.Tests.cs @@ -46,6 +46,7 @@ public static void SetOperationChecksDoNotThrowWhenViewBoundsExcluded() // The other set's bounds fall outside the view. These must report false, not throw. Assert.False(view.IsSupersetOf(new SortedSet { 1 })); Assert.False(view.IsSupersetOf(new SortedSet { 2, 9 })); + Assert.False(view.IsSupersetOf(new SortedSet { 4 })); Assert.False(view.IsProperSupersetOf(new SortedSet { 1 })); // The same when the view is the argument rather than the instance. @@ -54,7 +55,6 @@ public static void SetOperationChecksDoNotThrowWhenViewBoundsExcluded() // Results that are unaffected by the pruning fast path must not change. Assert.True(view.IsSupersetOf(new SortedSet { 2, 3 })); - Assert.False(view.IsSupersetOf(new SortedSet { 4 })); Assert.True(view.IsProperSupersetOf(new SortedSet { 2 })); Assert.True(view.IsSubsetOf(set)); Assert.True(view.IsProperSubsetOf(set));