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

IPAddress.TryFormat returns false erroneously for shorter IP addresses #84482

Closed
stephentoub opened this issue Apr 7, 2023 · 3 comments · Fixed by #84487
Closed

IPAddress.TryFormat returns false erroneously for shorter IP addresses #84482

stephentoub opened this issue Apr 7, 2023 · 3 comments · Fixed by #84487
Assignees
Milestone

Comments

@stephentoub
Copy link
Member

This:

char[] chars = new char[7];
Console.WriteLine(IPAddress.Parse("1.1.1.1").TryFormat(chars, out int charsWritten));
Console.WriteLine(charsWritten);

should print:

True
7

but instead it prints:

False
0

If the size of the char[] is increased to be at least 15, then it prints the desired output. The issue is the implementation is comparing the target destination buffer against the maximum size of a rendered IPv4 address, and failing if the destination is smaller than that.

@stephentoub stephentoub added this to the 8.0.0 milestone Apr 7, 2023
@stephentoub stephentoub self-assigned this Apr 7, 2023
@ghost
Copy link

ghost commented Apr 7, 2023

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

Issue Details

This:

char[] chars = new char[7];
Console.WriteLine(IPAddress.Parse("1.1.1.1").TryFormat(chars, out int charsWritten));
Console.WriteLine(charsWritten);

should print:

True
7

but instead it prints:

False
0

If the size of the char[] is increased to be at least 15, then it prints the desired output. The issue is the implementation is comparing the target destination buffer against the maximum size of a rendered IPv4 address, and failing if the destination is smaller than that.

Author: stephentoub
Assignees: stephentoub
Labels:

area-System.Net

Milestone: 8.0.0

@EgorBo
Copy link
Member

EgorBo commented Apr 7, 2023

Ouch, so e.g. google dns (8.8.8.8, 8.8.4.4) can't be parsed currently?

@stephentoub
Copy link
Member Author

Ouch, so e.g. google dns (8.8.8.8, 8.8.4.4) can't be parsed currently?

It'll be parsed fine. It's just if you give a buffer smaller than 15 to TryFormat, TryFormat will return false. When whatever is formatting then grows the destination buffer to >= 15, it'll render fine.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Apr 7, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Apr 10, 2023
@ghost ghost locked as resolved and limited conversation to collaborators May 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants