Closed
Description
Description
Convert.FromHexString
that returns an OperationStatus
will throw if
- The input hex string is short enough that it does not go down the vector 128 path.
- 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: