Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NRE in EncodingProvider #55238

Merged
merged 2 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal static void AddProvider(EncodingProvider provider)

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

if (Interlocked.CompareExchange(ref s_providers, newProviders, providers) == providers)
{
Expand Down
20 changes: 20 additions & 0 deletions src/libraries/System.Runtime/tests/System/Text/EncodingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Linq;
using Microsoft.DotNet.RemoteExecutor;
using Moq;
using Xunit;

Expand Down Expand Up @@ -106,6 +107,25 @@ public void GetEncodings_FromProvider_DoesNotContainDisallowedEncodings(string e
});
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void RegisterProvider_EncodingsAreUsable()
{
RemoteExecutor.Invoke(() =>
{
for (int i = 0; i < 10; i++)
{
Encoding.RegisterProvider(new NullEncodingProvider());
Assert.Same(Encoding.UTF8, Encoding.GetEncoding(65001));
}
}).Dispose();
}

private sealed class NullEncodingProvider : EncodingProvider
{
public override Encoding GetEncoding(int codepage) => null;
public override Encoding GetEncoding(string name) => null;
}

private sealed class ThreadStaticEncodingProvider : EncodingProvider
{
private static readonly object _globalRegistrationLockObj = new object();
Expand Down