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

Hasher reports that it is not initalized or destroyed, or segfaults #24

Closed
FrancisRussell opened this issue Jan 11, 2024 · 2 comments
Closed
Labels
enhancement New feature or request

Comments

@FrancisRussell
Copy link

FrancisRussell commented Jan 11, 2024

I am using the 1.0.0 package from Nuget and compiling on a Debian bookworm x86_64 system. If I run the following code:

class Program
{
    static void Main()
    {
        var data = new byte[] { 1, 2, 3};
        Hash1(data);
        Hash2(data);
    }

    static void Hash1(byte[] data)
    {
        var hash = Blake3.Hasher.Hash(data);
        System.Console.WriteLine($"Hash 1 completed: {hash}");
    }

    static void Hash2(byte[] data)
    {
        var hasher = new Blake3.Hasher();
        hasher.Update(data);
        var hash = hasher.Finalize();
        System.Console.WriteLine($"Hash 2 completed: {hash}");
    }
}

I get this error:

Unhandled exception. System.NullReferenceException: The Hasher is not initialized or already destroyed.
   at Blake3.Hasher.ThrowNullReferenceException()
   at Blake3.Hasher.Update(ReadOnlySpan`1 data)
   at Program.Hash2(Byte[] data) in /home/fpr/fast_block_upload/c_sharp/main.cs:line 19
   at Program.Main() in /home/fpr/fast_block_upload/c_sharp/main.cs:line 7

Prior to attempting to simplify this example, I was encountering segmentation faults which in gdb pointed to blake3_finalize. Invoking Blake3.Hasher.Hash seems to work fine though.

@FrancisRussell
Copy link
Author

FrancisRussell commented Jan 11, 2024

Ah, I see the problem. You need to construct a Hasher with the static function Hasher.New. If you call new Hasher() you will get back a hasher which hasn't had its internal hasher initalized.

I'm going to leave this open, as it seems peculiar to me to not have the default constructor provide a functional hasher or for there to not be a stronger defence against this.

@xoofx
Copy link
Owner

xoofx commented Jan 11, 2024

Afair, when this library was created, C# was not supporting default constructors for structs.

@xoofx xoofx added the enhancement New feature or request label Jan 11, 2024
@xoofx xoofx closed this as completed in 2213abe Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants