Restore Advance when reading float/double with XmlBufferReader. (#80274)
authorSteve Molloy <smolloy@microsoft.com>
Sun, 8 Jan 2023 03:38:04 +0000 (19:38 -0800)
committerGitHub <noreply@github.com>
Sun, 8 Jan 2023 03:38:04 +0000 (22:38 -0500)
* Restore Advance when reading float/double with XmlBufferReader.

* Removing extra unneeded usings that got magically added and are failing on not-my-machine.

src/libraries/Common/tests/System/Runtime/Serialization/DataContractSerializerHelper.cs
src/libraries/System.Private.DataContractSerialization/src/System/Xml/XmlBufferReader.cs

index 164015006109b4f0df4a06cfd3450189d211ae7c..eeebcc9dcbb28da4d1b599493ce845d8141c00ba 100644 (file)
@@ -4,14 +4,14 @@
 using System.IO;
 using System.Reflection;
 using System.Runtime.Serialization.Formatters.Binary;
-
+using System.Xml;
 using Xunit;
 
 namespace System.Runtime.Serialization.Tests
 {
     public static class DataContractSerializerHelper
     {
-        internal static T SerializeAndDeserialize<T>(T value, string baseline, DataContractSerializerSettings settings = null, Func<DataContractSerializer> serializerFactory = null, bool skipStringCompare = false)
+        internal static T SerializeAndDeserialize<T>(T value, string baseline, DataContractSerializerSettings settings = null, Func<DataContractSerializer> serializerFactory = null, bool skipStringCompare = false, bool verifyBinaryRoundTrip = true)
         {
             DataContractSerializer dcs;
             if (serializerFactory != null)
@@ -23,6 +23,7 @@ namespace System.Runtime.Serialization.Tests
                 dcs = (settings != null) ? new DataContractSerializer(typeof(T), settings) : new DataContractSerializer(typeof(T));
             }
 
+            T deserialized;
             using (MemoryStream ms = new MemoryStream())
             {
                 dcs.WriteObject(ms, value);
@@ -38,9 +39,37 @@ namespace System.Runtime.Serialization.Tests
                 }
 
                 ms.Position = 0;
-                T deserialized = (T)dcs.ReadObject(ms);
+                deserialized = (T)dcs.ReadObject(ms);
+            }
+
+            if (verifyBinaryRoundTrip)
+            {
+                RoundTripBinarySerialization<T>(value, settings, serializerFactory);
+            }
 
-                return deserialized;
+            return deserialized;
+        }
+
+        internal static T RoundTripBinarySerialization<T>(T value, DataContractSerializerSettings settings = null, Func<DataContractSerializer> serializerFactory = null)
+        {
+            DataContractSerializer dcs;
+            if (serializerFactory != null)
+            {
+                dcs = serializerFactory();
+            }
+            else
+            {
+                dcs = (settings != null) ? new DataContractSerializer(typeof(T), settings) : new DataContractSerializer(typeof(T));
+            }
+
+            using (MemoryStream ms = new MemoryStream())
+            using (XmlDictionaryWriter binWriter = XmlDictionaryWriter.CreateBinaryWriter(ms))
+            {
+                dcs.WriteObject(binWriter, value);
+                binWriter.Flush();
+                ms.Position = 0;
+                XmlDictionaryReader binReader = XmlDictionaryReader.CreateBinaryReader(ms, XmlDictionaryReaderQuotas.Max);
+                return (T)dcs.ReadObject(binReader);
             }
         }
     }
index a4ea049ecdb60741cc10a19db4e7509a2bbf6257..34bf2427ee3915621ffd59397cb85025d6cfa9cf 100644 (file)
@@ -373,10 +373,18 @@ namespace System.Xml
             => BitConverter.IsLittleEndian ? ReadRawBytes<long>() : BinaryPrimitives.ReverseEndianness(ReadRawBytes<long>());
 
         public float ReadSingle()
-            => BinaryPrimitives.ReadSingleLittleEndian(GetBuffer(sizeof(float), out int offset).AsSpan(offset, sizeof(float)));
+        {
+            float f = BinaryPrimitives.ReadSingleLittleEndian(GetBuffer(sizeof(float), out int offset).AsSpan(offset, sizeof(float)));
+            Advance(sizeof(float));
+            return f;
+        }
 
         public double ReadDouble()
-            => BinaryPrimitives.ReadDoubleLittleEndian(GetBuffer(sizeof(double), out int offset).AsSpan(offset, sizeof(double)));
+        {
+            double d = BinaryPrimitives.ReadDoubleLittleEndian(GetBuffer(sizeof(double), out int offset).AsSpan(offset, sizeof(double)));
+            Advance(sizeof(double));
+            return d;
+        }
 
         public decimal ReadDecimal()
         {