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.FromHexString returning an OperationStatus throws unless destination buffer is precisely sized #105405

Closed
vcsjones opened this issue Jul 24, 2024 · 2 comments · Fixed by #105426
Assignees
Labels
area-System.Runtime blocking-release bug in-pr There is an active PR which will close this issue when it is merged
Milestone

Comments

@vcsjones
Copy link
Member

vcsjones commented Jul 24, 2024

Description

Convert.FromHexString that returns an OperationStatus will throw if

  1. The input hex string is short enough that it does not go down the vector 128 path.
  2. The destination is larger than needed.

Reproduction Steps

using System.Buffers;

string hex = Convert.ToHexString([255, 255, 255]);
byte[] buffer = new byte[100];
OperationStatus status = Convert.FromHexString(hex, buffer, out int charsConsumed, out int bytesWritten);
Console.WriteLine(status);
Console.WriteLine(bytesWritten);
Console.WriteLine(charsConsumed);

Expected behavior

The hex string decode successfully. The bytesWritten output value should be 3.

Actual behavior

Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.HexConverter.TryDecodeFromUtf16_Scalar(ReadOnlySpan`1 chars, Span`1 bytes, Int32& charsProcessed)
   at System.HexConverter.TryDecodeFromUtf16(ReadOnlySpan`1 chars, Span`1 bytes, Int32& charsProcessed)
   at System.Convert.FromHexString(ReadOnlySpan`1 source, Span`1 destination, Int32& charsConsumed, Int32& bytesWritten)
   at System.Convert.FromHexString(String source, Span`1 destination, Int32& charsConsumed, Int32& bytesWritten)
   at Program.<Main>$(String[] args) in /Users/vcsjones/Projects/scratch/Program.cs:line 5

Regression?

No, this is a new API in .NET 9.

Known Workarounds

Slice the destination buffer to the required size.

Configuration

  • .NET 9.0.0-preview.6.24327.7
  • macOS
  • Apple Silicon M1 / aarch64

Other information

A debug build of the runtime fails for me hitting this assert:

Debug.Assert(chars.Length / 2 == bytes.Length, "Target buffer not right-sized for provided characters");

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jul 24, 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.

@vcsjones vcsjones changed the title Convert.FromHexString returning an OperationStatus fails unless destination buffer is precisely sized Convert.FromHexString returning an OperationStatus throws unless destination buffer is precisely sized Jul 24, 2024
@stephentoub stephentoub added this to the 9.0.0 milestone Jul 24, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Jul 24, 2024
@hrrrrustic
Copy link
Contributor

hrrrrustic commented Jul 24, 2024

Introduced in my PR with initial implementation: #86556

@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Aug 8, 2024
@eiriktsarpalis eiriktsarpalis added in-pr There is an active PR which will close this issue when it is merged and removed in-pr There is an active PR which will close this issue when it is merged labels Aug 9, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Sep 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime blocking-release bug in-pr There is an active PR which will close this issue when it is merged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants