Skip to content

Host: fix Rectangle.GetHashCode when Left XOR Right is negative#27105

Open
cuiweixie wants to merge 1 commit intoPowerShell:masterfrom
cuiweixie:fix-Host-Rectangle-GetHashCode-lower-xor
Open

Host: fix Rectangle.GetHashCode when Left XOR Right is negative#27105
cuiweixie wants to merge 1 commit intoPowerShell:masterfrom
cuiweixie:fix-Host-Rectangle-GetHashCode-lower-xor

Conversation

@cuiweixie
Copy link
Copy Markdown

Summary

Corrects Rectangle.GetHashCode() when (Left ^ Right) is negative (and not Int32.MinValue): the lower 32-bit contribution must use -lower, not -upper, so the hash matches the intended mix of vertical and horizontal XOR halves.

Changes

  • In MshHostRawUserInterface.cs, use (UInt64)(-lower) in the non-MinValue negative branch of the lower-half fold.
  • Add a Host.Rectangle.GetHashCode Pester test (Tags: CI) for Rectangle(-1, 0, 0, 0) (expected GetHashCode() == 1 with the fix).

Testing

  • Invoke-Pester on test/powershell/Host/HostUtilities.Tests.ps1 (new Describe block).

The lower 32-bit fold for negative (Left ^ Right) incorrectly added (UInt64)(-upper) instead of (UInt64)(-lower), mixing the vertical XOR into the horizontal half.

Add a CI regression test for Rectangle(-1,0,0,0) whose hash must include the corrected lower contribution.
Copilot AI review requested due to automatic review settings March 27, 2026 18:17
@cuiweixie cuiweixie requested a review from a team as a code owner March 27, 2026 18:17
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an arithmetic bug in System.Management.Automation.Host.Rectangle.GetHashCode() so the hash folding uses the correct 32-bit half when (Left ^ Right) is negative, and adds a regression test to prevent reintroduction.

Changes:

  • Correct the lower-half negative fold to add -lower (not -upper) when lower < 0 and lower != Int32.MinValue.
  • Add a CI-tagged Pester regression test covering Rectangle(-1, 0, 0, 0).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/System.Management.Automation/engine/hostifaces/MshHostRawUserInterface.cs Fixes the incorrect operand used when folding the lower 32-bit half into the 64-bit mix for Rectangle.GetHashCode().
test/powershell/Host/HostUtilities.Tests.ps1 Adds a targeted regression test validating the corrected hash result for a negative (Left ^ Right) case.

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