Skip to content

Commit 89d23da

Browse files
Fix NRE in EncodingProvider (#55238)
* Issue 55233: fix NRE in EncodingProvider * Add test Co-authored-by: Stephen Toub <[email protected]>
1 parent 8bab2ef commit 89d23da

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/libraries/System.Private.CoreLib/src/System/Text/EncodingProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ internal static void AddProvider(EncodingProvider provider)
7171

7272
var newProviders = new EncodingProvider[providers.Length + 1];
7373
Array.Copy(providers, newProviders, providers.Length);
74-
providers[^1] = provider;
74+
newProviders[^1] = provider;
7575

7676
if (Interlocked.CompareExchange(ref s_providers, newProviders, providers) == providers)
7777
{

src/libraries/System.Runtime/tests/System/Text/EncodingTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.Generic;
55
using System.Linq;
6+
using Microsoft.DotNet.RemoteExecutor;
67
using Moq;
78
using Xunit;
89

@@ -106,6 +107,25 @@ public void GetEncodings_FromProvider_DoesNotContainDisallowedEncodings(string e
106107
});
107108
}
108109

110+
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
111+
public void RegisterProvider_EncodingsAreUsable()
112+
{
113+
RemoteExecutor.Invoke(() =>
114+
{
115+
for (int i = 0; i < 10; i++)
116+
{
117+
Encoding.RegisterProvider(new NullEncodingProvider());
118+
Assert.Same(Encoding.UTF8, Encoding.GetEncoding(65001));
119+
}
120+
}).Dispose();
121+
}
122+
123+
private sealed class NullEncodingProvider : EncodingProvider
124+
{
125+
public override Encoding GetEncoding(int codepage) => null;
126+
public override Encoding GetEncoding(string name) => null;
127+
}
128+
109129
private sealed class ThreadStaticEncodingProvider : EncodingProvider
110130
{
111131
private static readonly object _globalRegistrationLockObj = new object();

0 commit comments

Comments
 (0)