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

ArraySerializeUnsafe.Read returns wrong value on small reads #1

Open
cooldie opened this issue Aug 18, 2021 · 2 comments
Open

ArraySerializeUnsafe.Read returns wrong value on small reads #1

cooldie opened this issue Aug 18, 2021 · 2 comments

Comments

@cooldie
Copy link

cooldie commented Aug 18, 2021

An unexpected value is returned by public unsafe static ulong Read(ulong* uPtr, int bitposition, int bits) on small reads.

This seems to be caused by the fact that the following array element is always taken into account:
value |= uPtr[index + 1] << (MAXBITS - offset);

The following code reproduces the issue. It prints 31 but it should print 30. An additional bit is enabled due to the OR mentioned above.

const ulong WRITE_VALUE = 30;
            
byte[] buffer = new byte[1024];
            
for (int i = 0; i < buffer.Length; ++i)
{
    buffer[i] = byte.MaxValue;
}

fixed (byte* ptrBuf = buffer)
{
    ulong* ptrUlBuf = (ulong*) ptrBuf;

    int writeBitPos = 0;
    ArraySerializeUnsafe.Write(ptrUlBuf, WRITE_VALUE, ref writeBitPos, 5);

    int readBitPos = 0;
    ulong readValue = ArraySerializeUnsafe.Read(ptrUlBuf, readBitPos, 5);

    // Prints 31 but should print 30
    Debug.Log(readValue);
}

I haven't tested other unsafe functions but if they do something similiar or the same they might also return wrong values on small reads. The safe functions seem to return the correct values though.

@emotitron
Copy link
Owner

Btw, running that code above for me does print 30. Though it is possible I fixed this quite some time ago.

@emotitron
Copy link
Owner

yeah, I see I fixed this in 2022, so closing it. Sorry for the delay.

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

2 participants