-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix HashSet copy constructor handling of instances that have fallen b…
- Loading branch information
1 parent
03eaee3
commit 43381f9
Showing
3 changed files
with
57 additions
and
4 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/libraries/System.Collections/tests/Generic/HashSet/HashSet.NonGeneric.Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Runtime.Serialization; | ||
using Xunit; | ||
|
||
namespace System.Collections.Tests | ||
{ | ||
public static class HashSet_NonGeneric_Tests | ||
{ | ||
[Fact] | ||
public static void HashSet_CopyConstructor_ShouldWorkWithRandomizedEffectiveComparer() | ||
{ | ||
HashSet<string> set = CreateCopyWithRandomizedComparer(new HashSet<string>() { "a", "b" }); | ||
Assert.True(set.Contains("a")); | ||
|
||
HashSet<string> copiedSet = new(set); | ||
Assert.True(copiedSet.Contains("a")); | ||
|
||
static HashSet<string> CreateCopyWithRandomizedComparer(HashSet<string> set) | ||
{ | ||
// To reproduce the bug, we need a HashSet<string> instance that has fallen back to | ||
// the randomized comparer. This typically happens when there are many collisions but | ||
// it can also happen when the set is serialized and deserialized via ISerializable. | ||
// For consistent results and to avoid brute forcing collisions, use the latter approach. | ||
|
||
SerializationInfo info = new(typeof(HashSet<string>), new FormatterConverter()); | ||
StreamingContext context = new(StreamingContextStates.All); | ||
set.GetObjectData(info, context); | ||
|
||
HashSet<string> copiedSet = (HashSet<string>)Activator.CreateInstance(typeof(HashSet<string>), BindingFlags.NonPublic | BindingFlags.Instance, null, [info, context], null); | ||
copiedSet.OnDeserialization(null); | ||
return copiedSet; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters