Skip to content

Test-Connection -BufferSize 0 reports buffer size of 32 #27656

Description

@raman118

Steps to reproduce

Run the Test-Connection cmdlet specifying a buffer size of 0, then inspect the BufferSize property of the output PingStatus object, or check the returned table:

$result = Test-Connection -TargetName localhost -Count 1 -BufferSize 0
Write-Host "Result BufferSize property: $($result.BufferSize)"
Write-Host "Actual Reply.Buffer.Length: $($result.Reply.Buffer.Length)"

Expected behavior

The cmdlet output and the BufferSize property should correctly reflect the requested buffer size of 0:

Result BufferSize property: 0
Actual Reply.Buffer.Length: 0

Actual behavior

The cmdlet output and the BufferSize property incorrectly report 32:

Result BufferSize property: 32
Actual Reply.Buffer.Length: 0

The console output table also reports the BufferSize (B) as 32:

Ping Source           Address                   Latency BufferSize Status
                                                   (ms)        (B)
---- ------           -------                   ------- ---------- ------
   1 localhost        ::1                             0         32 Success

Error details

No exception is thrown. The ping completes successfully but outputs incorrect metadata.

Environment data

Name                           Value
----                           -----
PSVersion                      7.6.3
PSEdition                      Core
GitCommitId                    7.6.3
OS                             Microsoft Windows 10.0.26200
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.4
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Additional Context

This is caused by the following logic in TestConnectionCommand.cs:

buffer.Length == 0 ? DefaultSendBufferSize : buffer.Length

When -BufferSize 0 is passed, GetSendBuffer(0) allocates a 0-length array (new byte[0]). Since buffer.Length is 0, the expression evaluates to DefaultSendBufferSize (32), misreporting the buffer size as 32 bytes even though a 0-byte payload was actually sent.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions