ssg / SimpleBase

.NET library for encoding/decoding Base16, Base32, Base58 and Base85.
Apache License 2.0
147 stars 21 forks source link

Base32.Rfc4648.Encode(long) fails for multiples of 256 #58

Closed pschwarzpp closed 1 month ago

pschwarzpp commented 1 month ago

Throws IndexOutOfRangeException.

using SimpleBase;

namespace SimpleBaseTests;

public class UnitTest1
{
    [Fact]
    public void Encode_Base32_Long()
    {
        var someFailures = FailingBase32Rfc4648Encodes().Take(10).ToArray();
        Assert.Empty(someFailures);
    }

    private IEnumerable<(long, Exception)> FailingBase32Rfc4648Encodes()
    {
        for (var val = 0; val >= 0; ++val)
        {
            (long, Exception)? problem = null;

            try
            {
                Base32.Rfc4648.Encode(val);
            }
            catch (Exception ex)
            {
                problem = (val, ex);
            }

            if (problem is not null)
            {
                yield return problem.Value;
                problem = null;
            }
        }
    }
}

will produce output like

Xunit.Sdk.EmptyException
Assert.Empty() Failure: Collection was not empty
Collection: [Tuple (0, System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at SimpleBase.Base32.Encode(UInt64 number)
   at SimpleBase.Base32.Encode(Int64 number)
   at SimpleBaseTests.UnitTest1.FailingBase32Rfc4648Encodes()+MoveNext() in C:\Projects\PS.Playgrounds\SimpleBaseTests\UnitTest1.cs:line 22), Tuple (256, System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at SimpleBase.Base32.Encode(UInt64 number)
   at SimpleBase.Base32.Encode(Int64 number)
   at SimpleBaseTests.UnitTest1.FailingBase32Rfc4648Encodes()+MoveNext() in C:\Projects\PS.Playgrounds\SimpleBaseTests\UnitTest1.cs:line 22), Tuple (512, System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at SimpleBase.Base32.Encode(UInt64 number)
   at SimpleBase.Base32.Encode(Int64 number)
   at SimpleBaseTests.UnitTest1.FailingBase32Rfc4648Encodes()+MoveNext() in C:\Projects\PS.Playgrounds\SimpleBaseTests\UnitTest1.cs:line 22), Tuple (768, System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at SimpleBase.Base32.Encode(UInt64 number)
   at SimpleBase.Base32.Encode(Int64 number)
   at SimpleBaseTests.UnitTest1.FailingBase32Rfc4648Encodes()+MoveNext() in C:\Projects\PS.Playgrounds\SimpleBaseTests\UnitTest1.cs:line 22), Tuple (1024, System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at SimpleBase.Base32.Encode(UInt64 number)
   at SimpleBase.Base32.Encode(Int64 number)
   at SimpleBaseTests.UnitTest1.FailingBase32Rfc4648Encodes()+MoveNext() in C:\Projects\PS.Playgrounds\SimpleBaseTests\UnitTest1.cs:line 22), ···]
   at SimpleBaseTests.UnitTest1.Encode_Base32_Long() in C:\Projects\PS.Playgrounds\SimpleBaseTests\UnitTest1.cs:line 11
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
ssg commented 1 month ago

Thanks @pschwarzpp! Will release 4.0.1 shortly

pschwarzpp commented 1 month ago

Thanks a lot for fixing this so quickly!