Avoid non documented exceptions in BinaryFormatter.Deserialize (dotnet/corefx#40215)
authorbirkmose <michael.birkmose@gmail.com>
Tue, 8 Oct 2019 16:07:03 +0000 (18:07 +0200)
committerViktor Hofer <viktor.hofer@microsoft.com>
Tue, 8 Oct 2019 16:07:03 +0000 (18:07 +0200)
* dotnet/corefx#35491 Fixes issue with BinaryFormatter.Deserialize throwing other exceptions than those specified in the documentation (only supposed to throw SerializationException or SecurityException).

* Update UnitySerializationHolderTests to test the expected behaviour of BinaryFormatter.Deserialize. Fix naming error in SerializationGuardTests.

Commit migrated from https://github.com/dotnet/corefx/commit/1736bfa5f06d4d3a60742142f0aaf601bd4db788

src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/BinaryFormatter.cs
src/libraries/System.Runtime.Serialization.Formatters/tests/SerializationGuardTests.cs
src/libraries/System.Runtime/tests/System/UnitySerializationHolderTests.cs

index 2ed345c..a1a6454 100644 (file)
@@ -63,9 +63,21 @@ namespace System.Runtime.Serialization.Formatters.Binary
             {
                 _crossAppDomainArray = _crossAppDomainArray
             };
-            var parser = new BinaryParser(serializationStream, reader);
-            return reader.Deserialize(parser, check);
+            try
+            {
+                var parser = new BinaryParser(serializationStream, reader);
+                return reader.Deserialize(parser, check);
+            }
+            catch (SerializationException)
+            {
+                throw;
+            }
+            catch (Exception e)
+            {
+                throw new SerializationException(SR.Serialization_CorruptedStream, e);
+            }
         }
+
         public void Serialize(Stream serializationStream, object graph) =>
             Serialize(serializationStream, graph, true);
 
index 5c83477..66acfc3 100644 (file)
@@ -73,8 +73,8 @@ namespace System.Runtime.Serialization.Formatters.Tests
             ms.Position = 0;
 
             BinaryFormatter reader = new BinaryFormatter();
-            TargetInvocationException tie = Assert.Throws<TargetInvocationException>(() => reader.Deserialize(ms));
-            Assert.IsAssignableFrom<SerializationException>(tie.InnerException);
+            SerializationException se = Assert.Throws<SerializationException>(() => reader.Deserialize(ms));
+            Assert.IsAssignableFrom<TargetInvocationException>(se.InnerException);
         }
     }
 
index f36235d..253484c 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+using System.Runtime.Serialization;
 using System.Runtime.Serialization.Formatters.Tests;
 using Xunit;
 
@@ -13,8 +14,9 @@ namespace System.Tests
         public void UnitySerializationHolderWithAssemblySingleton()
         {
             const string UnitySerializationHolderAssemblyBase64String = "AAEAAAD/////AQAAAAAAAAAEAQAAAB9TeXN0ZW0uVW5pdHlTZXJpYWxpemF0aW9uSG9sZGVyAwAAAAREYXRhCVVuaXR5VHlwZQxBc3NlbWJseU5hbWUBAAEIBgIAAABLbXNjb3JsaWIsIFZlcnNpb249NC4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5BgAAAAkCAAAACw==";
-            AssertExtensions.Throws<ArgumentException>(() =>
+            SerializationException se = AssertExtensions.Throws<SerializationException>(() =>
               BinaryFormatterHelpers.FromBase64String(UnitySerializationHolderAssemblyBase64String));
+            Assert.IsAssignableFrom<ArgumentException>(se.InnerException);
         }
     }
 }