Repository navigation
Conversation
🦋 Changeset detectedLatest commit: 973cc07 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi, I'm Kyle Cox (@Kshot9000), an independent developer building on Ergo. I found this while auditing the serializer's number codecs against the reference implementation (scorex-util's
VLQWriter/VLQReader, which sigmastate uses forputUInt/getUInt).The bug
writeVLQ/readVLQinpackages/serializer/src/coders/vlq.tsdid their math with JS bitwise operators (>>,<<,|), which truncate operands to signed 32 bits. Everything below 2^31 works, which is why the existing tests (vectors up to 2^28, randoms up to 10^8) never caught it — but at and above 2^31 the codec silently corrupts values:writeVLQ(2147483648)(2^31)[0x00]— the value 0[0x80, 0x80, 0x80, 0x80, 0x08]writeVLQ(4294967295)(2^32 − 1)[0x7f]— the value 127[0xff, 0xff, 0xff, 0xff, 0x0f]readVLQ([0xff, 0xff, 0xff, 0xff, 0x0f])readVLQof the 2^32 encodinggetUIntrejects it (out of unsigned int range)writeVLQ(4294967296)(2^32)putUIntrejects itThese functions back
SigmaByteWriter.writeUInt/SigmaByteReader.readUInt, which serialize box creation heights, the ErgoTree size prefix, AVL tree key/value lengths, and every array length and count prefix — so anyUIntin the upper half of its range was corrupted on write and misread on parse, with no error either way.The fix
writeVLQ: shift with division (% 128/Math.floor(val / 128)) instead of bitwise operators, and throw aRangeErrorfor values aboveMAX_U32(new constant innumRanges.ts), matchingputUInt'srequire(x >= 0 && x <= 0xFFFFFFFF).readVLQ: accumulate with multiplication (* 2 ** shift) instead of<<, and throw aRangeErrorwhen the decoded value exceedsMAX_U32, matchinggetUInt.readBigVLQ/writeBigVLQ(the 64-bit path) already usedbigintmath and are untouched.Tests
Tests-first: the new cases fail on the old code (7 failures) and pass with the fix.
RangeError.0 … 4294967295.Changeset included (
@fleet-sdk/serializer: patch). Happy to adjust anything — thanks for maintaining Fleet.— Kyle