kwsch / SysBot.NET

sys-botbase client for remote control automation of Nintendo Switch consoles.
https://projectpokemon.org/home/files/file/4207-sysbot-automatic-build/
GNU Affero General Public License v3.0
463 stars 270 forks source link

Net 8 "Index was outside bounds of array" #190

Closed bdawg1989 closed 9 months ago

bdawg1989 commented 9 months ago

image

Latest build in debug mode shows an issue with this in particular on bot start up.

    public async Task<SAV9SV> GetFakeTrainerSAV(CancellationToken token)
    {
        var sav = new SAV9SV();
        var info = sav.MyStatus;
        var read = await SwitchConnection.PointerPeek(info.Data.Length, Offsets.MyStatusPointer, token).ConfigureAwait(false);
        read.CopyTo(info.Data, 0);
        return sav;
    }
Particularly with var read.  

It looks like the issue could be stemming from Decoder.cs line 27

I think that the issue lies in the data being passed to the LoadHexBytesTo method. This error is thrown because the length of the str parameter is not an even multiple of tupleSize, which is expected to be 2 for hexadecimal byte pairs.

    public static void LoadHexBytesTo(ReadOnlySpan<byte> str, Span<byte> dest, int tupleSize)
{
    // The input string is 2-char hex values optionally separated.
    // The destination array should always be larger or equal than the bytes written. Let the runtime bounds check us.
    // Iterate through the string without allocating.
    for (int i = 0, j = 0; i < str.Length; i += tupleSize)
        dest[j++] = DecodeTuple((char)str[i + 0], (char)str[i + 1]);
}
bdawg1989 commented 9 months ago

I added some additional logging to ReadBytesFromCmdAsync and got this:

image

Lusamine commented 9 months ago

This is fixed by https://github.com/kwsch/SysBot.NET/commit/8af30590f4928d93837df91f1cd7b7ae4039bda8.