Skip to content

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

Merged
merged 3 commits into from
Jun 12, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Expand Down Expand Up @@ -77,6 +78,7 @@ private static T[] ReadBsonArray<T>(

var result = new List<T>();


Copy link
Contributor

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)

var index = 4; // 4 first bytes are array object size in bytes
var maxIndex = array.Length - 1;

Expand All @@ -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;
Expand All @@ -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)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably could just use BinaryPrimitivesCompat.ReadDoubleLittleEndian ?

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)));
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve updated the code to work directly with Span<byte> instead of using byte[] where applicable, as you suggested. Please verify and let me know if any further changes are needed.

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;
}
Expand All @@ -182,12 +180,10 @@ private static T[] ReadBsonArray<T>(
}

result.Add(value);

index += bsonDataSize;
}

ValidateBsonType(BsonType.EndOfDocument);

return result.ToArray();

void ValidateBsonType(BsonType bsonType)
Expand Down
Loading