Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

medhatiwari
Copy link
Contributor

Description

This PR fixes deserialization issues in PrimitivesArrayReader.cs which caused 51 test failures in the MongoDB.Bson.Tests suite on Big Endian systems (e.g., IBM s390x).

Problem

The deserialization logic relied on BitConverter.ToXxx(...) which assumes Little Endian memory layout. On Big Endian architectures, this results in incorrect values for arrays of primitive types such as Int64, Single, Double, and Decimal, leading to test failures.

Fix

  • Replaced unsafe BitConverter usages with BinaryPrimitives.ReadXxxLittleEndian(...) for endian-safe reads.

  • Covered conversions for:

      -- float, double (via BitConverter.Int64BitsToDouble(...))
    
      -- decimal (via Decimal128.FromIEEEBits(...))
    
      -- char (casted from ushort)
    
      -- All numeric types (e.g. int, long, ushort, etc.)
    

Results:

51 test failures resolved.

Example fixed tests:

MemorySerializerTests.ReadonlyMemory_should_roundtrip_special_values<Int64>

MemorySerializerTests.Memory_should_roundtrip_special_values<Single>

BinaryVectorSerializerTests.BinaryVectors_should_roundtrip<float>

Pre-requisites:

  • s390x system (e.g., IBM Z, RHEL on s390x)

  • .NET 8.0 SDK installed

cc: @giritrivedi

…imitives for Big Endian support

Signed-off-by: Medha Tiwari <[email protected]>
@medhatiwari medhatiwari requested a review from a team as a code owner May 6, 2025 06:14
@medhatiwari medhatiwari requested review from rstam and removed request for a team May 6, 2025 06:14
@medhatiwari
Copy link
Contributor Author

hi @rstam , Can you please review this?

@rstam
Copy link
Contributor

rstam commented May 7, 2025

We will discuss this at our next triage meeting. Not sure whether we intend to support Big Endian systems or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants