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 exception with too large buffer #105426

Merged
merged 8 commits into from
Aug 12, 2024

Conversation

hrrrrustic
Copy link
Contributor

@hrrrrustic hrrrrustic commented Jul 24, 2024

Should close #105405

Draft for now to see CI results

@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 Jul 24, 2024
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jul 24, 2024
@hrrrrustic hrrrrustic marked this pull request as ready for review July 25, 2024 19:34
@vcsjones vcsjones 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 Jul 25, 2024
@@ -3007,11 +3007,15 @@ public static OperationStatus FromHexString(ReadOnlySpan<char> source, Span<byte
quotient = destination.Length;
result = OperationStatus.DestinationTooSmall;
}
else if (remainder == 1)
else if (destination.Length > quotient)
Copy link
Member

@eiriktsarpalis eiriktsarpalis Aug 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we also be handling remainders in cases where the destination buffer precisely matches the quotient?

Suggested change
else if (destination.Length > quotient)
else

Are we missing a unit test for that case?

{
source = source.Slice(0, source.Length - 1);
}

result = OperationStatus.NeedMoreData;
Copy link
Member

@eiriktsarpalis eiriktsarpalis Aug 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the destination buffer can fit the result and the remainder is 0, why should the status be NeedMoreData? Do we have unit test for that case?

byte[] buffer = new byte[100];
var status = Convert.FromHexString(hex, buffer, out int charsConsumed, out int bytesWritten);

Assert.Equal(OperationStatus.NeedMoreData, status);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that this assertion is incorrect. NeedMoreData should only be returned if the parser handled partial data (i.e. there's an odd number of hex chars). If the buffer fits the result it should always return Done.

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed changes that address an additional corner case and update the behaviour such that OperationStatus.Done is being returned in cases where the destination buffer is longer than the output.

cc @adamsitnik

@eiriktsarpalis eiriktsarpalis merged commit 69bb1a4 into dotnet:main Aug 12, 2024
147 checks passed
@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 community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Convert.FromHexString returning an OperationStatus throws unless destination buffer is precisely sized
3 participants