Skip to content

Commit

Permalink
Throw PNSE on browser
Browse files Browse the repository at this point in the history
  • Loading branch information
GrabYourPitchforks committed Jul 15, 2020
1 parent d4f16b9 commit 731b699
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<!-- On certain runtimes BinaryFormatter isn't supported at all. -->
<DefineConstants Condition="'$(TargetsBrowser)' == 'true'">$(DefineConstants);BINARYFORMATTER_PNSE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Include="System.Runtime.Serialization.Formatters.TypeForwards.cs" />
<Compile Include="System\Runtime\Serialization\DeserializationEventHandler.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public BinaryFormatter(ISurrogateSelector? selector, StreamingContext context)

public object Deserialize(Stream serializationStream)
{
// don't refactor this check out into a helper method; linker will have difficulty pruning
// don't refactor the 'throw' into a helper method; linker will have difficulty trimming
if (!SerializationInfo.BinaryFormatterEnabled)
{
throw new NotSupportedException(SR.BinaryFormatter_SerializationDisallowed);
throw CreateBinaryFormatterDisallowedException();
}

if (serializationStream == null)
Expand Down Expand Up @@ -82,10 +82,10 @@ public object Deserialize(Stream serializationStream)

public void Serialize(Stream serializationStream, object graph)
{
// don't refactor this check out into a helper method; linker will have difficulty pruning
// don't refactor the 'throw' into a helper method; linker will have difficulty trimming
if (!SerializationInfo.BinaryFormatterEnabled)
{
throw new NotSupportedException(SR.BinaryFormatter_SerializationDisallowed);
throw CreateBinaryFormatterDisallowedException();
}

if (serializationStream == null)
Expand All @@ -112,5 +112,19 @@ internal static TypeInformation GetTypeInformation(Type type) =>
string assemblyName = FormatterServices.GetClrAssemblyName(t, out bool hasTypeForwardedFrom);
return new TypeInformation(FormatterServices.GetClrTypeFullName(t), assemblyName, hasTypeForwardedFrom);
});

private static Exception CreateBinaryFormatterDisallowedException()
{
// If we're in an environment where BinaryFormatter never has a
// chance of succeeding, use PNSE. Otherwise use regular NSE.

string message = SR.BinaryFormatter_SerializationDisallowed;

#if BINARYFORMATTER_PNSE
throw new PlatformNotSupportedException(message);
#else
throw new NotSupportedException(message);
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public static void DisabledAlwaysInBrowser()

MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
var ex = Assert.Throws<NotSupportedException>(() => bf.Serialize(ms, "A string to serialize."));
var ex = Assert.Throws<PlatformNotSupportedException>(() => bf.Serialize(ms, "A string to serialize."));
Assert.Contains(MoreInfoUrl, ex.Message, StringComparison.Ordinal); // error message should link to the more info URL

// Then test deserialization

ex = Assert.Throws<NotSupportedException>(() => bf.Deserialize(ms));
ex = Assert.Throws<PlatformNotSupportedException>(() => bf.Deserialize(ms));
Assert.Contains(MoreInfoUrl, ex.Message, StringComparison.Ordinal); // error message should link to the more info URL
}

Expand Down

0 comments on commit 731b699

Please sign in to comment.