Skip to content

Commit

Permalink
Fixes for big-endian systems (#6204)
Browse files Browse the repository at this point in the history
* In MSBuildNameIgnoreCaseComparer::GetHashCode, when reading pairs of
  characters aliased to an int, handle the single last remaing char
  correctly on big-endian systems.

* In NodeProviderOutOfProcBase, byte-swap the packet length on
  big-endian systems.
  • Loading branch information
uweigand committed Mar 11, 2021
1 parent 38da84d commit 514ceb5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Globalization;
Expand Down Expand Up @@ -738,7 +739,7 @@ public async Task RunPacketReadLoopAsync()
}

NodePacketType packetType = (NodePacketType)_headerByte[0];
int packetLength = BitConverter.ToInt32(_headerByte, 1);
int packetLength = BinaryPrimitives.ReadInt32LittleEndian(new Span<byte>(_headerByte, 1, 4));

_readBufferMemoryStream.SetLength(packetLength);
byte[] packetData = _readBufferMemoryStream.GetBuffer();
Expand Down Expand Up @@ -1027,7 +1028,7 @@ private void HeaderReadComplete(IAsyncResult result)
return;
}

int packetLength = BitConverter.ToInt32(_headerByte, 1);
int packetLength = BinaryPrimitives.ReadInt32LittleEndian(new Span<byte>(_headerByte, 1, 4));
MSBuildEventSource.Log.PacketReadSize(packetLength);

// Ensures the buffer is at least this length.
Expand Down
18 changes: 16 additions & 2 deletions src/Shared/MSBuildNameIgnoreCaseComparer.cs
Expand Up @@ -149,7 +149,14 @@ public int GetHashCode(string obj, int start, int length)
// the string, and not the null terminator etc.
if (length == 1)
{
val &= 0xFFFF;
if (BitConverter.IsLittleEndian)
{
val &= 0xFFFF;
}
else
{
val &= unchecked((int)0xFFFF0000);
}
}

hash1 = ((hash1 << 5) + hash1 + (hash1 >> 27)) ^ val;
Expand All @@ -162,7 +169,14 @@ public int GetHashCode(string obj, int start, int length)
val = pint[1] & 0x00DF00DF;
if (length == 3)
{
val &= 0xFFFF;
if (BitConverter.IsLittleEndian)
{
val &= 0xFFFF;
}
else
{
val &= unchecked((int)0xFFFF0000);
}
}

hash2 = ((hash2 << 5) + hash2 + (hash2 >> 27)) ^ val;
Expand Down

0 comments on commit 514ceb5

Please sign in to comment.