From 3eefec13bcda9e0a8eb1e06b1e4d94d3a82a5c1e Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 2 Oct 2023 14:58:56 -0700 Subject: [PATCH] Fix `Test-Connection` due to .NET 8 changes (#20369) --- .../commands/management/TestConnectionCommand.cs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 1f497e5dd2c..1370a56ea96 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -56,7 +56,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable #region Private Fields - private static byte[]? s_DefaultSendBuffer; + private static readonly byte[] s_DefaultSendBuffer = Array.Empty(); private readonly CancellationTokenSource _dnsLookupCancel = new(); @@ -484,7 +484,10 @@ private void ProcessTraceroute(string targetNameOrAddress) reply.Status == IPStatus.Success ? reply.RoundtripTime : timer.ElapsedMilliseconds, - buffer.Length, + + // If we use the empty buffer, then .NET actually uses a 32 byte buffer so we want to show + // as the result object the actual buffer size used instead of 0. + buffer.Length == 0 ? DefaultSendBufferSize : buffer.Length, pingNum: i); WriteObject(new TraceStatus( currentHop, @@ -707,7 +710,7 @@ private void ProcessPing(string targetNameOrAddress) resolvedTargetName, reply, reply.RoundtripTime, - buffer.Length, + buffer.Length == 0 ? DefaultSendBufferSize : buffer.Length, pingNum: (uint)i)); } @@ -862,7 +865,7 @@ private IPHostEntry GetCancellableHostEntry(string targetNameOrAddress) // Creates and fills a send buffer. This follows the ping.exe and CoreFX model. private static byte[] GetSendBuffer(int bufferSize) { - if (bufferSize == DefaultSendBufferSize && s_DefaultSendBuffer != null) + if (bufferSize == DefaultSendBufferSize) { return s_DefaultSendBuffer; } @@ -874,11 +877,6 @@ private static byte[] GetSendBuffer(int bufferSize) sendBuffer[i] = (byte)((int)'a' + i % 23); } - if (bufferSize == DefaultSendBufferSize && s_DefaultSendBuffer == null) - { - s_DefaultSendBuffer = sendBuffer; - } - return sendBuffer; }