Skip to content

Commit 604ea07

Browse files
Big-endian fix for JsonClassInfo.GetKey (dotnet#49708)
* Big-endian fix for JsonClassInfo.GetKey * Skip assertion in JsonClassInfo.GetKey on big-endian (code will not ensure the properties tested here) * Update src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.Cache.cs Co-authored-by: Stephen Toub <[email protected]>
1 parent 4591b07 commit 604ea07

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.Cache.cs

+17-13
Original file line numberDiff line numberDiff line change
@@ -474,19 +474,23 @@ public static ulong GetKey(ReadOnlySpan<byte> name)
474474
}
475475

476476
// Verify key contains the embedded bytes as expected.
477-
const int BitsInByte = 8;
478-
Debug.Assert(
479-
// Verify embedded property name.
480-
(name.Length < 1 || name[0] == ((key & ((ulong)0xFF << BitsInByte * 0)) >> BitsInByte * 0)) &&
481-
(name.Length < 2 || name[1] == ((key & ((ulong)0xFF << BitsInByte * 1)) >> BitsInByte * 1)) &&
482-
(name.Length < 3 || name[2] == ((key & ((ulong)0xFF << BitsInByte * 2)) >> BitsInByte * 2)) &&
483-
(name.Length < 4 || name[3] == ((key & ((ulong)0xFF << BitsInByte * 3)) >> BitsInByte * 3)) &&
484-
(name.Length < 5 || name[4] == ((key & ((ulong)0xFF << BitsInByte * 4)) >> BitsInByte * 4)) &&
485-
(name.Length < 6 || name[5] == ((key & ((ulong)0xFF << BitsInByte * 5)) >> BitsInByte * 5)) &&
486-
(name.Length < 7 || name[6] == ((key & ((ulong)0xFF << BitsInByte * 6)) >> BitsInByte * 6)) &&
487-
// Verify embedded length.
488-
(name.Length >= 0xFF || (key & ((ulong)0xFF << BitsInByte * 7)) >> BitsInByte * 7 == (ulong)name.Length) &&
489-
(name.Length < 0xFF || (key & ((ulong)0xFF << BitsInByte * 7)) >> BitsInByte * 7 == 0xFF));
477+
// Note: the expected properties do not hold true on big-endian platforms
478+
if (BitConverter.IsLittleEndian)
479+
{
480+
const int BitsInByte = 8;
481+
Debug.Assert(
482+
// Verify embedded property name.
483+
(name.Length < 1 || name[0] == ((key & ((ulong)0xFF << BitsInByte * 0)) >> BitsInByte * 0)) &&
484+
(name.Length < 2 || name[1] == ((key & ((ulong)0xFF << BitsInByte * 1)) >> BitsInByte * 1)) &&
485+
(name.Length < 3 || name[2] == ((key & ((ulong)0xFF << BitsInByte * 2)) >> BitsInByte * 2)) &&
486+
(name.Length < 4 || name[3] == ((key & ((ulong)0xFF << BitsInByte * 3)) >> BitsInByte * 3)) &&
487+
(name.Length < 5 || name[4] == ((key & ((ulong)0xFF << BitsInByte * 4)) >> BitsInByte * 4)) &&
488+
(name.Length < 6 || name[5] == ((key & ((ulong)0xFF << BitsInByte * 5)) >> BitsInByte * 5)) &&
489+
(name.Length < 7 || name[6] == ((key & ((ulong)0xFF << BitsInByte * 6)) >> BitsInByte * 6)) &&
490+
// Verify embedded length.
491+
(name.Length >= 0xFF || (key & ((ulong)0xFF << BitsInByte * 7)) >> BitsInByte * 7 == (ulong)name.Length) &&
492+
(name.Length < 0xFF || (key & ((ulong)0xFF << BitsInByte * 7)) >> BitsInByte * 7 == 0xFF));
493+
}
490494

491495
return key;
492496
}

0 commit comments

Comments
 (0)