-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5614: Fix deserialization of primitive arrays on Big Endian systems #1683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
89a8e71
9180338
31d88d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System; | ||
using System.Buffers.Binary; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
using MongoDB.Bson.IO; | ||
|
@@ -77,6 +78,7 @@ private static T[] ReadBsonArray<T>( | |
|
||
var result = new List<T>(); | ||
|
||
|
||
var index = 4; // 4 first bytes are array object size in bytes | ||
var maxIndex = array.Length - 1; | ||
|
||
|
@@ -85,7 +87,7 @@ private static T[] ReadBsonArray<T>( | |
ValidateBsonType(bsonDataType); | ||
|
||
// Skip name | ||
while (bytes[index] != 0) { index++; }; | ||
while (bytes[index] != 0) { index++; } | ||
index++; // Skip string terminating 0 | ||
|
||
T value = default; | ||
|
@@ -95,85 +97,81 @@ private static T[] ReadBsonArray<T>( | |
{ | ||
case ConversionType.DoubleToSingle: | ||
{ | ||
var v = (float)BitConverter.ToDouble(bytes, index); | ||
|
||
var v = (float)BitConverter.Int64BitsToDouble(BinaryPrimitives.ReadInt64LittleEndian(bytes.AsSpan(index))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably could just use |
||
value = Unsafe.As<float, T>(ref v); | ||
break; | ||
} | ||
case ConversionType.DoubleToDouble: | ||
{ | ||
var v = BitConverter.ToDouble(bytes, index); | ||
var v = BitConverter.Int64BitsToDouble(BinaryPrimitives.ReadInt64LittleEndian(bytes.AsSpan(index))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like we can just work with span instead of bytes in all cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’ve updated the code to work directly with |
||
value = Unsafe.As<double, T>(ref v); | ||
break; | ||
} | ||
case ConversionType.Decimal128ToDecimal128: | ||
{ | ||
var lowBits = (ulong)BitConverter.ToInt64(bytes, index); | ||
var highBits = (ulong)BitConverter.ToInt64(bytes, index + 8); | ||
var lowBits = BinaryPrimitives.ReadUInt64LittleEndian(bytes.AsSpan(index)); | ||
var highBits = BinaryPrimitives.ReadUInt64LittleEndian(bytes.AsSpan(index + 8)); | ||
var v = Decimal128.ToDecimal(Decimal128.FromIEEEBits(highBits, lowBits)); | ||
|
||
value = Unsafe.As<decimal, T>(ref v); | ||
break; | ||
} | ||
case ConversionType.BoolToBool: | ||
{ | ||
var v = bytes[index] != 0; | ||
|
||
value = Unsafe.As<bool, T>(ref v); | ||
break; | ||
} | ||
case ConversionType.Int32ToInt8: | ||
{ | ||
var v = (sbyte)BitConverter.ToInt32(bytes, index); | ||
var v = (sbyte)BinaryPrimitives.ReadInt32LittleEndian(bytes.AsSpan(index)); | ||
value = Unsafe.As<sbyte, T>(ref v); | ||
|
||
break; | ||
} | ||
case ConversionType.Int32ToUInt8: | ||
{ | ||
var v = (byte)BitConverter.ToInt32(bytes, index); | ||
var v = (byte)BinaryPrimitives.ReadInt32LittleEndian(bytes.AsSpan(index)); | ||
value = Unsafe.As<byte, T>(ref v); | ||
break; | ||
} | ||
case ConversionType.Int32ToInt16: | ||
{ | ||
var v = (short)BitConverter.ToInt32(bytes, index); | ||
var v = (short)BinaryPrimitives.ReadInt32LittleEndian(bytes.AsSpan(index)); | ||
value = Unsafe.As<short, T>(ref v); | ||
break; | ||
} | ||
case ConversionType.Int32ToUInt16: | ||
{ | ||
var v = (ushort)BitConverter.ToInt32(bytes, index); | ||
var v = (ushort)BinaryPrimitives.ReadInt32LittleEndian(bytes.AsSpan(index)); | ||
value = Unsafe.As<ushort, T>(ref v); | ||
break; | ||
} | ||
case ConversionType.Int32ToChar: | ||
{ | ||
var v = BitConverter.ToChar(bytes, index); | ||
var v = (char)(ushort)BinaryPrimitives.ReadInt32LittleEndian(bytes.AsSpan(index)); | ||
value = Unsafe.As<char, T>(ref v); | ||
break; | ||
} | ||
case ConversionType.Int32ToInt32: | ||
{ | ||
var v = BitConverter.ToInt32(bytes, index); | ||
var v = BinaryPrimitives.ReadInt32LittleEndian(bytes.AsSpan(index)); | ||
value = Unsafe.As<int, T>(ref v); | ||
break; | ||
} | ||
case ConversionType.Int32ToUInt32: | ||
{ | ||
var v = BitConverter.ToUInt32(bytes, index); | ||
var v = BinaryPrimitives.ReadUInt32LittleEndian(bytes.AsSpan(index)); | ||
value = Unsafe.As<uint, T>(ref v); | ||
break; | ||
} | ||
case ConversionType.Int64ToInt64: | ||
{ | ||
var v = BitConverter.ToInt64(bytes, index); | ||
var v = BinaryPrimitives.ReadInt64LittleEndian(bytes.AsSpan(index)); | ||
value = Unsafe.As<long, T>(ref v); | ||
break; | ||
} | ||
case ConversionType.Int64ToUInt64: | ||
{ | ||
var v = BitConverter.ToUInt64(bytes, index); | ||
var v = BinaryPrimitives.ReadUInt64LittleEndian(bytes.AsSpan(index)); | ||
value = Unsafe.As<ulong, T>(ref v); | ||
break; | ||
} | ||
|
@@ -182,12 +180,10 @@ private static T[] ReadBsonArray<T>( | |
} | ||
|
||
result.Add(value); | ||
|
||
index += bsonDataSize; | ||
} | ||
|
||
ValidateBsonType(BsonType.EndOfDocument); | ||
|
||
BorisDog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return result.ToArray(); | ||
|
||
void ValidateBsonType(BsonType bsonType) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for empty lines changes (extra line here, and removed lines below)