From 731b6993f4b549dcb32ea9d491c54d4f052b1920 Mon Sep 17 00:00:00 2001 From: Levi Broderick Date: Tue, 14 Jul 2020 16:53:18 -0700 Subject: [PATCH] Throw PNSE on browser --- ...em.Runtime.Serialization.Formatters.csproj | 4 ++++ .../Formatters/Binary/BinaryFormatter.cs | 22 +++++++++++++++---- .../tests/DisableBitTests.cs | 4 ++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.Runtime.Serialization.Formatters/src/System.Runtime.Serialization.Formatters.csproj b/src/libraries/System.Runtime.Serialization.Formatters/src/System.Runtime.Serialization.Formatters.csproj index e39003d146ac3..f55ef0b5f2b4d 100644 --- a/src/libraries/System.Runtime.Serialization.Formatters/src/System.Runtime.Serialization.Formatters.csproj +++ b/src/libraries/System.Runtime.Serialization.Formatters/src/System.Runtime.Serialization.Formatters.csproj @@ -5,6 +5,10 @@ $(NetCoreAppCurrent) enable + + + $(DefineConstants);BINARYFORMATTER_PNSE + diff --git a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryFormatter.cs b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryFormatter.cs index 8e9ba9e04c478..f8b5c097054d8 100644 --- a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryFormatter.cs +++ b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryFormatter.cs @@ -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) @@ -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) @@ -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 + } } } diff --git a/src/libraries/System.Runtime.Serialization.Formatters/tests/DisableBitTests.cs b/src/libraries/System.Runtime.Serialization.Formatters/tests/DisableBitTests.cs index c5bb8658252e8..0a1874dab688a 100644 --- a/src/libraries/System.Runtime.Serialization.Formatters/tests/DisableBitTests.cs +++ b/src/libraries/System.Runtime.Serialization.Formatters/tests/DisableBitTests.cs @@ -24,12 +24,12 @@ public static void DisabledAlwaysInBrowser() MemoryStream ms = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); - var ex = Assert.Throws(() => bf.Serialize(ms, "A string to serialize.")); + var ex = Assert.Throws(() => 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(() => bf.Deserialize(ms)); + ex = Assert.Throws(() => bf.Deserialize(ms)); Assert.Contains(MoreInfoUrl, ex.Message, StringComparison.Ordinal); // error message should link to the more info URL }