Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCalvin Buckley <calvin@cmpct.info>2019-11-25 21:15:55 +0300
committermonojenkins <jo.shields+jenkins@xamarin.com>2019-11-25 21:15:55 +0300
commit836f45390628c0d05b45d7b27237634062b224a8 (patch)
tree97da5000702fb9dd30d351243d0a933439c055e0
parentbdf4790fa061704a6e40d55c07b214961e5083cc (diff)
Fix BinaryReader on BE (#17893)
Fix BinaryReader on BE The ints passed into the int array ctor needed to be endian swapped. Interestingly, the BinaryWriter implementation currently being used (not refsrc AFAIK) does this on write just fine. Fixes ReadDecimal test on AIX, and adds another test to ensure that Write(decimal) can be read back by ReadDecimal in case.
-rw-r--r--mcs/class/corlib/Test/System.IO/BinaryWriterTest.cs18
-rw-r--r--mcs/class/referencesource/mscorlib/system/io/binaryreader.cs7
2 files changed, 25 insertions, 0 deletions
diff --git a/mcs/class/corlib/Test/System.IO/BinaryWriterTest.cs b/mcs/class/corlib/Test/System.IO/BinaryWriterTest.cs
index 8fc3d11f902..6ba707a1d7f 100644
--- a/mcs/class/corlib/Test/System.IO/BinaryWriterTest.cs
+++ b/mcs/class/corlib/Test/System.IO/BinaryWriterTest.cs
@@ -419,6 +419,24 @@ public class BinaryWriterTest {
Assert.AreEqual (0, bytes [45], "test#47");
Assert.AreEqual (0, bytes [46], "test#48");
}
+
+ [Test]
+ public void WriteDecimalAndReadBack()
+ {
+ // This value is the same used in BinaryReader.ReadDecimal, which will be
+ // problematic if BinaryWriter or BinaryReader are endianness-confused.
+ Decimal writeDec = -18295873486192640;
+ using (var ms = new MemoryStream(32)) {
+ using (var bw = new BinaryWriter(ms, new UTF8Encoding(), true)) {
+ bw.Write(writeDec);
+ }
+ ms.Position = 0;
+ using (var br = new BinaryReader(ms)) {
+ Decimal readDec = br.ReadDecimal();
+ Assert.AreEqual (readDec, writeDec, "test#01");
+ }
+ }
+ }
[Test]
public void WriteFloat ()
diff --git a/mcs/class/referencesource/mscorlib/system/io/binaryreader.cs b/mcs/class/referencesource/mscorlib/system/io/binaryreader.cs
index e4e1ecfe376..9f1b89de5d5 100644
--- a/mcs/class/referencesource/mscorlib/system/io/binaryreader.cs
+++ b/mcs/class/referencesource/mscorlib/system/io/binaryreader.cs
@@ -23,6 +23,7 @@ namespace System.IO {
using System.Globalization;
using System.Diagnostics.Contracts;
using System.Security;
+ using System.Buffers.Binary;
[System.Runtime.InteropServices.ComVisible(true)]
public class BinaryReader : IDisposable
@@ -243,6 +244,12 @@ namespace System.IO {
try {
int[] ints = new int[4];
Buffer.BlockCopy(m_buffer, 0, ints, 0, 16);
+ if (!BitConverter.IsLittleEndian) {
+ // We need to reverse the ints on BE
+ for (int i = 0; i < 4; i++) {
+ ints[i] = BinaryPrimitives.ReverseEndianness(ints[i]);
+ }
+ }
return new decimal(ints);
}
catch (ArgumentException e) {