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

Convert.TryToHexString{Lower} does not check the length of destination correct #109807

Open
universorum opened this issue Nov 14, 2024 · 2 comments
Labels
area-System.Runtime untriaged New issue has not been triaged by the area owner

Comments

@universorum
Copy link

Description

Convert.TryToHexString{Lower} will allow the destination that smaller than actual length and will NOT allow what is bigger than 2x actual length.

Reproduction Steps

Span<byte> hashBuffer  = stackalloc byte[16];

Span<char> biggerThanDoubleLength = stackalloc char[33];
Debug.Assert(Convert.TryToHexString(hashBuffer, biggerThanDoubleLength, out _) == false);

Span<char> lessThanLength = stackalloc char[31]; 
Debug.Assert(Convert.TryToHexStringLower(hashBuffer, lessThanLength, out var length));
Debug.Assert(length == 32);

Console.WriteLine("done");

Expected behavior

The function may return false when the size of destination is smaller than 2x source and working when destination is bigger than 2x source.

Actual behavior

The function will return true when the size of destination is smaller than 2x source with proper out variable charsWritten and will NOT working when destination is bigger than 2x source.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

It look like 2 variable is reversed.

else if (source.Length > int.MaxValue / 2 || destination.Length > source.Length * 2)

else if (source.Length > int.MaxValue / 2 || destination.Length > source.Length * 2)

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Nov 14, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 14, 2024
@huoyaoyuan huoyaoyuan added area-System.Runtime and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Nov 14, 2024
Copy link
Contributor

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

@danmoseley
Copy link
Member

danmoseley commented Nov 14, 2024

Would you like to investigate a bit? Edit: you already did. Would you like to offer a PR with a test? Thanks for the report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Runtime untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

3 participants