Repository navigation
Conversation
The shortU16 decoder loop had no upper bound, so a continuation chain longer than the documented three-byte encoding was accepted, producing values far outside the u16 domain the encoder enforces. Worse, once the chain ran past the end of the buffer, the out-of-bounds byte read as undefined terminated the loop silently: truncated input decoded to garbage and returned an offset beyond bytes.length instead of failing. Bound the loop at three bytes and assert per-byte availability through assertByteArrayHasEnoughBytesForCodec, matching every other number decoder. A chain that still continues after three bytes now throws NUMBER_OUT_OF_RANGE, mirroring the encoder's own range assertion.
🦋 Changeset detectedLatest commit: a3e06ae The changes in this PR will be included in the next version bump. This PR includes changesets to release 48 packages
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 |
Author
|
Reopening — this was closed by the stale bot, not reviewed. I re-verified every claim in the PR body by executing the actual pre/post code with the real Old code reproduces exactly as described:
Fixed code:
Happy to address any review feedback. 🙏 |
Contributor
|
Because there has been no activity on this PR for 14 days since it was merged, it has been automatically locked. Please open a new issue if it requires a follow up. |
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Problem
The
shortU16decoder loop had no upper bound, violating the codec's own documented contract of a 1 to 3 byte encoding (maxSize: 3). Two concrete failure modes:80 80 80 80 ...with continuation bits keeps decoding, producing values far outside the0..65535domain the encoder asserts viaassertNumberIsBetweenForCodec.bytes[offset + byteIndex]returnedundefined;undefined & 0b10000000 === 0terminated the loop, so truncated input decoded to garbage and returned an offset beyondbytes.lengthinstead of failing. For example,decode(new Uint8Array([0x80]))currently returns[0, 2].Every other number decoder guards its reads through
assertByteArrayHasEnoughBytesForCodec;shortU16was the odd one out.Change
assertByteArrayHasEnoughBytesForCodec('shortU16', ...), so truncation throwsINVALID_BYTE_LENGTHexactly like the fixed-size number decoders.NUMBER_OUT_OF_RANGE, mirroring the encoder's own range assertion for the same domain violation.Test plan
Two regression tests fail on current main and pass with this change:
decode([0x80])anddecode([0x80, 0x80])(truncated chains) now throwSOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH.decode([0xff, 0xff, 0xff, 0x00])(four-byte chain) now throwsSOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE.Full
codecs-numberssuite passes (28/28), including the existing round-trip sweep over all\[0, 0xffff\]values; package typecheck is clean.