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

Exception on empty input #24

Open
Alezy80 opened this issue Oct 24, 2022 · 1 comment
Open

Exception on empty input #24

Alezy80 opened this issue Oct 24, 2022 · 1 comment

Comments

@Alezy80
Copy link

Alezy80 commented Oct 24, 2022

When input is empty array/span all hashes throws an exception.

var bytes = new byte[0];
var hash = xxHash3.ComputeHash(bytes, bytes.Length); //exception

var span = bytes.AsSpan();
var hash2 = xxHash3.ComputeHash(span, span.Length); //exception

In all hash functions must be guard clause like this

if (data.Length == 0)
    return ComputeEmptyHash(seed);
...
private static unsafe uint ComputeEmptyHash(uint seed)
{
    var pData = stackalloc byte[1]; //doesn't work with zero
    return UnsafeComputeHash(pData, 0, seed);
}
@Alezy80
Copy link
Author

Alezy80 commented Oct 24, 2022

Another variant is to delegate all work to ReadOnlySpan version (Span overload is excessive) and get reference as

fixed (byte* pData = &MemoryMarshal.GetReference(data))
{
    return UnsafeComputeHash(pData, data.Length, seed);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant