Skip to content

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

@universorum

Description

@universorum

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Runtimein-prThere is an active PR which will close this issue when it is merged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions