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

Shortvec bitwise operation issue for decoding length #2

Open
maxim-petlyuk opened this issue Apr 27, 2023 · 1 comment · May be fixed by #3
Open

Shortvec bitwise operation issue for decoding length #2

maxim-petlyuk opened this issue Apr 27, 2023 · 1 comment · May be fixed by #3

Comments

@maxim-petlyuk
Copy link

maxim-petlyuk commented Apr 27, 2023

@JvmStatic
    fun decodeLength(bytes: ByteArray): Pair<Int, ByteArray> {
        var newBytes = bytes
        var len = 0
        var size = 0
        while (true) {
            val elem = newBytes.first().toInt().also { newBytes = newBytes.drop(1).toByteArray() }
            len = len or (elem and 0x7f) shl (size * 7)
            size += 1
            if ((elem and 0x80) == 0) {
                break
            }
        }
        return len to newBytes
    }

There is wrong order of bitwise operations for line:
len = len or (elem and 0x7f) shl (size * 7)
which is finally impact on wrong length calculation.

It should be fixed with:
en = len or ((elem and 0x7f) shl (size * 7))

@maxim-petlyuk maxim-petlyuk linked a pull request Apr 27, 2023 that will close this issue
@maxim-petlyuk
Copy link
Author

@kihonyoo I have compared code with official library Web3 Solana JS. So there is also proof inside JS code. What else I can do to merge this PR into library?

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

Successfully merging a pull request may close this issue.

1 participant