Skip to content

Conversation

@ManickaP
Copy link
Member

@ManickaP ManickaP commented Jul 7, 2025

Fixes #117114

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Copy link
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

This PR changes the IPNetwork implementation to automatically clear bits outside the network prefix instead of throwing an exception, and updates tests to verify the new behavior.

  • IPNetwork constructor and TryParse now use ClearNonZeroBitsAfterNetworkPrefix to trim bits rather than rejecting the input.
  • The obsolete HasNonZeroBitsAfterNetworkPrefix logic and related error resource were removed.
  • Tests were updated to compute expected base addresses, moved previously invalid cases into valid data, and corrected a typo in prefixLength.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/libraries/System.Net.Primitives/tests/FunctionalTests/IPNetworkTest.cs Updated valid/invalid data, added helper methods to calculate expected base addresses
src/libraries/System.Net.Primitives/src/System/Net/IPNetwork.cs Replaced non-zero-bit check with clearing logic, simplified TryParse
src/libraries/System.Net.Primitives/src/Resources/Strings.resx Removed obsolete error message for invalid base addresses
Comments suppressed due to low confidence (3)

src/libraries/System.Net.Primitives/tests/FunctionalTests/IPNetworkTest.cs:39

  • Add a test case for the IPv6 /0 network (e.g. { "::/0" }) to ensure the new zero-prefix branch for IPv6 is covered.
            { "192.168.0.10/0" },

src/libraries/System.Net.Primitives/src/System/Net/IPNetwork.cs:240

  • [nitpick] Consider renaming ClearNonZeroBitsAfterNetworkPrefix to something like ComputeNetworkAddress or TrimToNetworkPrefix to better reflect that this method returns the trimmed base address.
        private static IPAddress ClearNonZeroBitsAfterNetworkPrefix(IPAddress baseAddress, int prefixLength)

src/libraries/System.Net.Primitives/src/System/Net/IPNetwork.cs:261

  • [nitpick] This comment is a bit unclear—consider clarifying or removing it, since the reasoning about shift width may not be obvious to future readers.
                // Bitwise shift works only for lower 7-bits count operands.

prefixLength <= GetMaxPrefixLength(address))
{
Debug.Assert(prefixLength >= 0); // Parsing with NumberStyles.None should ensure that prefixLength is always non-negative.
result = new IPNetwork(address, prefixLength, false);
Copy link
Contributor

Choose a reason for hiding this comment

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

The baseAddress is the original non-mask address. Is it intended?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fudge, good catch. It also means we don't have a good test coverage for this. I'll put up a PR asap.

@github-actions github-actions bot locked and limited conversation to collaborators Aug 8, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System.Net.IPNetwork not behave as Microsoft.AspNetCore.HttpOverrides.IPNetwork

4 participants