Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TryParse of a CIDR string with a trailing space and subsequent characters and sanitanize: false returns true #256

Open
enclave-alistair opened this issue Feb 20, 2023 · 2 comments

Comments

@enclave-alistair
Copy link

If I invoke IPNetwork.TryParse with a CIDR string that contains additional trailing characters after a space character, the parse succeeds:

[Test]
public void Should_return_false_when_parsing_a_cidr_with_extra_characters()
{
    var result = IPNetwork.TryParse("20.42.65.64/29 !%!!!!!", sanitanize: false, out _);

    // This assert fails.
    Assert.That(result, Is.False);
}

This is unexpected, since sanitanize: false leaves the trailing characters where they are.

The problem is caused by the space character; in InternalParse a space character is included in the split character array, but there is no check for there not being more than 2 components to the resulting split.

string[] args = network.Split(new char[] { ' ', '/' }, splitOptions);

@lduchosal
Copy link
Owner

Thanks for the report, PR welcome!

@alexangas
Copy link

I guess the issue here is that the use of sanitanize: false implies that the method should fail, but instead due to the bug it is inadvertently removing (sanitizing) the garbage instead of returning null (TryParse) or throwing an exception (Parse).

In addition, it would be good to have a clearer definition of the intention of the sanitanize parameter. Also to introduce a new function that deprecates the typo in the parameter name 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants