From bc820fdb008af569bff70e8ea59fa4db6c95827e Mon Sep 17 00:00:00 2001 From: latent-9 <296084221+latent-9@users.noreply.github.com> Date: Wed, 26 Aug 2026 23:53:44 +1200 Subject: [PATCH 1/7] fix(codecs-numbers): reject truncated and overlong shortU16 encodings 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. --- .../src/__tests__/short-u16-test.ts | 37 +++++++++++++++++++ packages/codecs-numbers/src/short-u16.ts | 17 +++++++-- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/packages/codecs-numbers/src/__tests__/short-u16-test.ts b/packages/codecs-numbers/src/__tests__/short-u16-test.ts index 91c218766..70e53fe70 100644 --- a/packages/codecs-numbers/src/__tests__/short-u16-test.ts +++ b/packages/codecs-numbers/src/__tests__/short-u16-test.ts @@ -1,3 +1,9 @@ +import { + SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH, + SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE, + SolanaError, +} from '@solana/errors'; + import { getShortU16Codec } from '../short-u16'; import { assertRangeError, assertValid, assertValidEncode } from './__setup__'; @@ -54,6 +60,37 @@ describe('getShortU16Codec', () => { } }); + it('throws when the buffer ends before the continuation chain terminates', () => { + expect.hasAssertions(); + // Continuation bit set, but no further bytes. + expect(() => shortU16().decode(new Uint8Array([0x80]))).toThrow( + new SolanaError(SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH, { + bytesLength: 1, + codecDescription: 'shortU16', + expected: 2, + }), + ); + expect(() => shortU16().decode(new Uint8Array([0x80, 0x80]))).toThrow( + new SolanaError(SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH, { + bytesLength: 2, + codecDescription: 'shortU16', + expected: 3, + }), + ); + }); + + it('rejects continuation chains that exceed the three-byte encoding', () => { + expect.hasAssertions(); + expect(() => shortU16().decode(new Uint8Array([0xff, 0xff, 0xff, 0x00]))).toThrow( + new SolanaError(SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE, { + codecDescription: 'shortU16', + max: MAX, + min: MIN, + value: 0x7f | (0x7f << 7) | (0x7f << 14), + }), + ); + }); + it('has the right sizes', () => { expect(shortU16().maxSize).toBe(3); expect(shortU16().getSizeFromValue(1)).toBe(1); diff --git a/packages/codecs-numbers/src/short-u16.ts b/packages/codecs-numbers/src/short-u16.ts index 89ebb70f4..2aaee45a0 100644 --- a/packages/codecs-numbers/src/short-u16.ts +++ b/packages/codecs-numbers/src/short-u16.ts @@ -1,4 +1,5 @@ import { + assertByteArrayHasEnoughBytesForCodec, combineCodec, createDecoder, createEncoder, @@ -9,6 +10,8 @@ import { VariableSizeEncoder, } from '@solana/codecs-core'; +import { SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE, SolanaError } from '@solana/errors'; + import { assertNumberIsBetweenForCodec } from './assertions'; /** @@ -90,7 +93,8 @@ export const getShortU16Decoder = (): VariableSizeDecoder => read: (bytes: ReadonlyUint8Array | Uint8Array, offset): [number, Offset] => { let value = 0; let byteCount = 0; - while (++byteCount) { + while (++byteCount <= 3) { + assertByteArrayHasEnoughBytesForCodec('shortU16', byteCount, bytes, offset); const byteIndex = byteCount - 1; const currentByte = bytes[offset + byteIndex]; const nextSevenBits = 0b1111111 & currentByte; @@ -98,10 +102,17 @@ export const getShortU16Decoder = (): VariableSizeDecoder => value |= nextSevenBits << (byteIndex * 7); if ((currentByte & 0b10000000) === 0) { // This byte does not have its continuation bit set. We're done. - break; + return [value, offset + byteCount]; } } - return [value, offset + byteCount]; + // Every encoded shortU16 fits in at most three bytes. A continuation bit on + // what would be the fourth byte implies a value outside the u16 domain. + throw new SolanaError(SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE, { + codecDescription: 'shortU16', + max: 65535, + min: 0, + value, + }); }, }); From a3e06aee7a18e6ac4544857d5d781d69a3ca877e Mon Sep 17 00:00:00 2001 From: latent-9 <296084221+latent-9@users.noreply.github.com> Date: Thu, 27 Aug 2026 00:05:18 +1200 Subject: [PATCH 2/7] chore: add changeset for shortU16 decoder fix --- .changeset/quiet-shorts-refuse.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/quiet-shorts-refuse.md diff --git a/.changeset/quiet-shorts-refuse.md b/.changeset/quiet-shorts-refuse.md new file mode 100644 index 000000000..820503740 --- /dev/null +++ b/.changeset/quiet-shorts-refuse.md @@ -0,0 +1,5 @@ +--- +'@solana/codecs-numbers': patch +--- + +Fixed `getShortU16Decoder()` silently accepting malformed input: a truncated buffer decoded to garbage with an offset past the end of the byte array, and continuation chains longer than the documented three-byte encoding produced values outside the u16 domain. Truncated input now throws `SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH` and overlong chains throw `SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE`, matching the guards every other number decoder already uses. From 74c71f1d9c609beb7ba713777e29db3c4e56b05a Mon Sep 17 00:00:00 2001 From: latent-9 <296084221+latent-9@users.noreply.github.com> Date: Thu, 17 Sep 2026 03:58:51 -0700 Subject: [PATCH 3/7] fix(codecs-numbers): reject terminated 3-byte shortU16 over u16 + lint imports Address review: a terminated 3-byte encoding can still exceed the u16 domain ([0x80,0x80,0x04] is 65536), so assert the decoded value with assertNumberIsBetweenForCodec before returning, mirroring the encoder. Also fix simple-import-sort grouping and correct the docblock's third byte description (low 7 bits, max 65535). --- .../codecs-numbers/src/__tests__/short-u16-test.ts | 13 +++++++++++++ packages/codecs-numbers/src/short-u16.ts | 11 +++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/codecs-numbers/src/__tests__/short-u16-test.ts b/packages/codecs-numbers/src/__tests__/short-u16-test.ts index 70e53fe70..c0da094da 100644 --- a/packages/codecs-numbers/src/__tests__/short-u16-test.ts +++ b/packages/codecs-numbers/src/__tests__/short-u16-test.ts @@ -91,6 +91,19 @@ describe('getShortU16Codec', () => { ); }); + it('rejects terminated three-byte encodings above the u16 domain', () => { + expect.hasAssertions(); + // No continuation bit on the third byte, but 0x04 << 14 is 65536. + expect(() => shortU16().decode(new Uint8Array([0x80, 0x80, 0x04]))).toThrow( + new SolanaError(SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE, { + codecDescription: 'shortU16', + max: MAX, + min: MIN, + value: 65536, + }), + ); + }); + it('has the right sizes', () => { expect(shortU16().maxSize).toBe(3); expect(shortU16().getSizeFromValue(1)).toBe(1); diff --git a/packages/codecs-numbers/src/short-u16.ts b/packages/codecs-numbers/src/short-u16.ts index 2aaee45a0..b6f5f5394 100644 --- a/packages/codecs-numbers/src/short-u16.ts +++ b/packages/codecs-numbers/src/short-u16.ts @@ -9,9 +9,7 @@ import { VariableSizeDecoder, VariableSizeEncoder, } from '@solana/codecs-core'; - import { SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE, SolanaError } from '@solana/errors'; - import { assertNumberIsBetweenForCodec } from './assertions'; /** @@ -102,6 +100,11 @@ export const getShortU16Decoder = (): VariableSizeDecoder => value |= nextSevenBits << (byteIndex * 7); if ((currentByte & 0b10000000) === 0) { // This byte does not have its continuation bit set. We're done. + // Every byte only contributes 7 bits, so a terminated + // 3-byte encoding can still exceed the u16 domain + // (e.g. [0x80, 0x80, 0x04] is 65536). Reject it here so + // the decoder never returns a value the encoder refuses. + assertNumberIsBetweenForCodec('shortU16', 0, 65535, value); return [value, offset + byteCount]; } } @@ -125,14 +128,14 @@ export const getShortU16Decoder = (): VariableSizeDecoder => * - If the value is `<= 0x7f` (127), it is stored in a **single byte** * and the first bit is set to `0` to indicate the end of the value. * - Otherwise, the first bit is set to `1` to indicate that the value continues in the next byte, which follows the same pattern. - * - This process repeats until the value is fully encoded in up to 3 bytes. The third and last byte, if needed, uses all 8 bits to store the remaining value. + * - This process repeats until the value is fully encoded in up to 3 bytes. The third and last byte, if needed, stores the remaining value in its low 7 bits. * * In other words, the encoding scheme follows this structure: * * ```txt * 0XXXXXXX <- Values 0 to 127 (1 byte) * 1XXXXXXX 0XXXXXXX <- Values 128 to 16,383 (2 bytes) - * 1XXXXXXX 1XXXXXXX XXXXXXXX <- Values 16,384 to 4,194,303 (3 bytes) + * 1XXXXXXX 1XXXXXXX 0XXXXXXX <- Values 16,384 to 65,535 (3 bytes) * ``` * * @returns A `VariableSizeCodec` for encoding and decoding `shortU16` values. From bf1e1309c57268868e5ceb77a2dd6079b2c1ac64 Mon Sep 17 00:00:00 2001 From: latent-9 <296084221+latent-9@users.noreply.github.com> Date: Thu, 17 Sep 2026 06:42:46 -0700 Subject: [PATCH 4/7] test(codecs-numbers): exhaustive shortU16 decode contract coverage Fully enumerate the terminated 1/2/3-byte spaces (accept iff value in 0..65535), the complete first-overflow row, and 5000 seeded arbitrary inputs asserting in-range values, in-bounds offsets, and re-encode round-trips. --- .../src/__tests__/short-u16-test.ts | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/packages/codecs-numbers/src/__tests__/short-u16-test.ts b/packages/codecs-numbers/src/__tests__/short-u16-test.ts index c0da094da..584c12ab5 100644 --- a/packages/codecs-numbers/src/__tests__/short-u16-test.ts +++ b/packages/codecs-numbers/src/__tests__/short-u16-test.ts @@ -112,4 +112,99 @@ describe('getShortU16Codec', () => { expect(shortU16().getSizeFromValue(16383)).toBe(2); expect(shortU16().getSizeFromValue(16384)).toBe(3); }); + + it('accepts exactly the terminated encodings of values 0..65535', () => { + expect.hasAssertions(); + const codec = shortU16(); + const buf = new Uint8Array(3); + // 1-byte space: 0x00..0x7f decode as-is, 0x80..0xff are truncated. + for (let b = 0; b < 0x100; b += 1) { + buf[0] = b; + if (b < 0x80) { + expect(codec.read(buf.subarray(0, 1), 0)).toEqual([b, 1]); + } else { + expect(() => codec.read(buf.subarray(0, 1), 0)).toThrow(SolanaError); + } + } + // 2-byte terminated space, fully enumerated. + for (let a = 0x80; a < 0x100; a += 1) { + for (let b = 0; b < 0x80; b += 1) { + buf[0] = a; + buf[1] = b; + const want = (a & 0x7f) | (b << 7); + expect(codec.read(buf.subarray(0, 2), 0)).toEqual([want, 2]); + } + } + // 3-byte terminated space with third byte < 0x04: every value 0..65535. + for (let a = 0x80; a < 0x100; a += 1) { + for (let b = 0x80; b < 0x100; b += 1) { + for (let c = 0; c < 0x04; c += 1) { + buf[0] = a; + buf[1] = b; + buf[2] = c; + const want = (a & 0x7f) | ((b & 0x7f) << 7) | (c << 14); + expect(codec.read(buf, 0)).toEqual([want, 3]); + } + } + } + }); + + it('rejects every terminated encoding above the u16 domain', () => { + expect.hasAssertions(); + const codec = shortU16(); + const buf = new Uint8Array(3); + // Full first-overflow row: third byte 0x04, values 65536..131071. + for (let a = 0x80; a < 0x100; a += 1) { + for (let b = 0x80; b < 0x100; b += 1) { + buf[0] = a; + buf[1] = b; + buf[2] = 0x04; + const want = (a & 0x7f) | ((b & 0x7f) << 7) | (0x04 << 14); + expect(() => codec.read(buf, 0)).toThrow( + new SolanaError(SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE, { + codecDescription: 'shortU16', + max: MAX, + min: MIN, + value: want, + }), + ); + } + } + }); + + it('never returns out-of-range values or past-end offsets for arbitrary bytes', () => { + expect.hasAssertions(); + const codec = shortU16(); + // Deterministic PRNG so failures reproduce exactly. + let state = 0x9e3779b9; + const nextByte = () => { + state = (state + 0x6d2b79f5) | 0; + let z = state; + z = Math.imul(z ^ (z >>> 15), z | 1); + z ^= z + Math.imul(z ^ (z >>> 7), z | 61); + return (z ^ (z >>> 14)) & 0xff; + }; + for (let i = 0; i < 5000; i += 1) { + const len = nextByte() % 7; + const bytes = new Uint8Array(len); + for (let j = 0; j < len; j += 1) { + bytes[j] = nextByte(); + } + let value: number; + let nextOffset: number; + try { + [value, nextOffset] = codec.read(bytes, 0); + } catch (e) { + expect(e).toBeInstanceOf(SolanaError); + continue; + } + expect(value).toBeGreaterThanOrEqual(MIN); + expect(value).toBeLessThanOrEqual(MAX); + expect(nextOffset).toBeGreaterThanOrEqual(1); + expect(nextOffset).toBeLessThanOrEqual(3); + expect(nextOffset).toBeLessThanOrEqual(bytes.length); + // Successful decodes round-trip through the encoder. + expect(codec.decode(codec.encode(value))).toBe(value); + } + }); }); From 4f789742651985b5fa9fc98b78b4770d0b3fd3cd Mon Sep 17 00:00:00 2001 From: latent-9 <296084221+latent-9@users.noreply.github.com> Date: Fri, 18 Sep 2026 20:29:18 +1200 Subject: [PATCH 5/7] fix(codecs-numbers): satisfy lint for shortU16 decoder-bounds tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes the two test:lint failures blocking CI on #2067: adds the missing blank line between the external and relative import groups in short-u16.ts per simple-import-sort, and rewrites the two new short-u16 tests to avoid jest/no-conditional-in-test and jest/no-conditional-expect by splitting the 1-byte enumeration loop and collecting fuzz outcomes before asserting. Test coverage is unchanged — the same seeded inputs run through the same assertions. Verified with compile, lint, typecheck, unit tests on Node and browser, and prettier, all green. --- .../src/__tests__/short-u16-test.ts | 34 ++++++++++++------- packages/codecs-numbers/src/short-u16.ts | 1 + 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/packages/codecs-numbers/src/__tests__/short-u16-test.ts b/packages/codecs-numbers/src/__tests__/short-u16-test.ts index 584c12ab5..de11b8dbe 100644 --- a/packages/codecs-numbers/src/__tests__/short-u16-test.ts +++ b/packages/codecs-numbers/src/__tests__/short-u16-test.ts @@ -117,14 +117,15 @@ describe('getShortU16Codec', () => { expect.hasAssertions(); const codec = shortU16(); const buf = new Uint8Array(3); - // 1-byte space: 0x00..0x7f decode as-is, 0x80..0xff are truncated. - for (let b = 0; b < 0x100; b += 1) { + // 1-byte space: 0x00..0x7f decode as-is. + for (let b = 0; b < 0x80; b += 1) { buf[0] = b; - if (b < 0x80) { - expect(codec.read(buf.subarray(0, 1), 0)).toEqual([b, 1]); - } else { - expect(() => codec.read(buf.subarray(0, 1), 0)).toThrow(SolanaError); - } + expect(codec.read(buf.subarray(0, 1), 0)).toEqual([b, 1]); + } + // 1-byte space: 0x80..0xff carry a continuation bit, so they are truncated. + for (let b = 0x80; b < 0x100; b += 1) { + buf[0] = b; + expect(() => codec.read(buf.subarray(0, 1), 0)).toThrow(SolanaError); } // 2-byte terminated space, fully enumerated. for (let a = 0x80; a < 0x100; a += 1) { @@ -184,20 +185,27 @@ describe('getShortU16Codec', () => { z ^= z + Math.imul(z ^ (z >>> 7), z | 61); return (z ^ (z >>> 14)) & 0xff; }; + const successes: Array<{ bytes: Uint8Array; nextOffset: number; value: number }> = []; + const failures: unknown[] = []; for (let i = 0; i < 5000; i += 1) { const len = nextByte() % 7; const bytes = new Uint8Array(len); for (let j = 0; j < len; j += 1) { bytes[j] = nextByte(); } - let value: number; - let nextOffset: number; try { - [value, nextOffset] = codec.read(bytes, 0); + const [value, nextOffset] = codec.read(bytes, 0); + successes.push({ bytes, nextOffset, value }); } catch (e) { - expect(e).toBeInstanceOf(SolanaError); - continue; + failures.push(e); } + } + // Every failed read must fail with a SolanaError. + failures.forEach(failure => { + expect(failure).toBeInstanceOf(SolanaError); + }); + // Every successful read must be in-range, in-bounds, and round-trip. + successes.forEach(({ bytes, nextOffset, value }) => { expect(value).toBeGreaterThanOrEqual(MIN); expect(value).toBeLessThanOrEqual(MAX); expect(nextOffset).toBeGreaterThanOrEqual(1); @@ -205,6 +213,6 @@ describe('getShortU16Codec', () => { expect(nextOffset).toBeLessThanOrEqual(bytes.length); // Successful decodes round-trip through the encoder. expect(codec.decode(codec.encode(value))).toBe(value); - } + }); }); }); diff --git a/packages/codecs-numbers/src/short-u16.ts b/packages/codecs-numbers/src/short-u16.ts index b6f5f5394..1cfb5787a 100644 --- a/packages/codecs-numbers/src/short-u16.ts +++ b/packages/codecs-numbers/src/short-u16.ts @@ -10,6 +10,7 @@ import { VariableSizeEncoder, } from '@solana/codecs-core'; import { SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE, SolanaError } from '@solana/errors'; + import { assertNumberIsBetweenForCodec } from './assertions'; /** From bad233a5b91e0357594281535bbb32777c3a5719 Mon Sep 17 00:00:00 2001 From: latent-9 <296084221+latent-9@users.noreply.github.com> Date: Fri, 18 Sep 2026 20:49:29 +1200 Subject: [PATCH 6/7] test(codecs-numbers): drop redundant exhaustive shortU16 decode tests Restores the shortU16 test file to 74c71f1, removing the exhaustive 1/2/3-byte enumeration and fuzz tests that duplicated the full-domain round-trip sweep, per review feedback. The targeted truncation, overlong-chain, and u16-overflow boundary tests stay, as does the import-sort fix in short-u16.ts. --- .../src/__tests__/short-u16-test.ts | 103 ------------------ 1 file changed, 103 deletions(-) diff --git a/packages/codecs-numbers/src/__tests__/short-u16-test.ts b/packages/codecs-numbers/src/__tests__/short-u16-test.ts index de11b8dbe..c0da094da 100644 --- a/packages/codecs-numbers/src/__tests__/short-u16-test.ts +++ b/packages/codecs-numbers/src/__tests__/short-u16-test.ts @@ -112,107 +112,4 @@ describe('getShortU16Codec', () => { expect(shortU16().getSizeFromValue(16383)).toBe(2); expect(shortU16().getSizeFromValue(16384)).toBe(3); }); - - it('accepts exactly the terminated encodings of values 0..65535', () => { - expect.hasAssertions(); - const codec = shortU16(); - const buf = new Uint8Array(3); - // 1-byte space: 0x00..0x7f decode as-is. - for (let b = 0; b < 0x80; b += 1) { - buf[0] = b; - expect(codec.read(buf.subarray(0, 1), 0)).toEqual([b, 1]); - } - // 1-byte space: 0x80..0xff carry a continuation bit, so they are truncated. - for (let b = 0x80; b < 0x100; b += 1) { - buf[0] = b; - expect(() => codec.read(buf.subarray(0, 1), 0)).toThrow(SolanaError); - } - // 2-byte terminated space, fully enumerated. - for (let a = 0x80; a < 0x100; a += 1) { - for (let b = 0; b < 0x80; b += 1) { - buf[0] = a; - buf[1] = b; - const want = (a & 0x7f) | (b << 7); - expect(codec.read(buf.subarray(0, 2), 0)).toEqual([want, 2]); - } - } - // 3-byte terminated space with third byte < 0x04: every value 0..65535. - for (let a = 0x80; a < 0x100; a += 1) { - for (let b = 0x80; b < 0x100; b += 1) { - for (let c = 0; c < 0x04; c += 1) { - buf[0] = a; - buf[1] = b; - buf[2] = c; - const want = (a & 0x7f) | ((b & 0x7f) << 7) | (c << 14); - expect(codec.read(buf, 0)).toEqual([want, 3]); - } - } - } - }); - - it('rejects every terminated encoding above the u16 domain', () => { - expect.hasAssertions(); - const codec = shortU16(); - const buf = new Uint8Array(3); - // Full first-overflow row: third byte 0x04, values 65536..131071. - for (let a = 0x80; a < 0x100; a += 1) { - for (let b = 0x80; b < 0x100; b += 1) { - buf[0] = a; - buf[1] = b; - buf[2] = 0x04; - const want = (a & 0x7f) | ((b & 0x7f) << 7) | (0x04 << 14); - expect(() => codec.read(buf, 0)).toThrow( - new SolanaError(SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE, { - codecDescription: 'shortU16', - max: MAX, - min: MIN, - value: want, - }), - ); - } - } - }); - - it('never returns out-of-range values or past-end offsets for arbitrary bytes', () => { - expect.hasAssertions(); - const codec = shortU16(); - // Deterministic PRNG so failures reproduce exactly. - let state = 0x9e3779b9; - const nextByte = () => { - state = (state + 0x6d2b79f5) | 0; - let z = state; - z = Math.imul(z ^ (z >>> 15), z | 1); - z ^= z + Math.imul(z ^ (z >>> 7), z | 61); - return (z ^ (z >>> 14)) & 0xff; - }; - const successes: Array<{ bytes: Uint8Array; nextOffset: number; value: number }> = []; - const failures: unknown[] = []; - for (let i = 0; i < 5000; i += 1) { - const len = nextByte() % 7; - const bytes = new Uint8Array(len); - for (let j = 0; j < len; j += 1) { - bytes[j] = nextByte(); - } - try { - const [value, nextOffset] = codec.read(bytes, 0); - successes.push({ bytes, nextOffset, value }); - } catch (e) { - failures.push(e); - } - } - // Every failed read must fail with a SolanaError. - failures.forEach(failure => { - expect(failure).toBeInstanceOf(SolanaError); - }); - // Every successful read must be in-range, in-bounds, and round-trip. - successes.forEach(({ bytes, nextOffset, value }) => { - expect(value).toBeGreaterThanOrEqual(MIN); - expect(value).toBeLessThanOrEqual(MAX); - expect(nextOffset).toBeGreaterThanOrEqual(1); - expect(nextOffset).toBeLessThanOrEqual(3); - expect(nextOffset).toBeLessThanOrEqual(bytes.length); - // Successful decodes round-trip through the encoder. - expect(codec.decode(codec.encode(value))).toBe(value); - }); - }); }); From fa6e525ac69aa95fe35c17dd1a4af25c3b9636ae Mon Sep 17 00:00:00 2001 From: latent-9 <296084221+latent-9@users.noreply.github.com> Date: Tue, 22 Sep 2026 23:57:36 +1200 Subject: [PATCH 7/7] fix(codecs-numbers): throw INVALID_BYTE_LENGTH for overlong shortU16 chains The decoder only reaches the throw when all three bytes have their continuation bit set, so the input demands a fourth byte. That is a byte-length problem rather than a range problem, and the accumulated value in the error context was misleading anyway. Replaces the magic number with a local constant and trims the explanatory comments. --- .changeset/quiet-shorts-refuse.md | 2 +- .../src/__tests__/short-u16-test.ts | 16 ++++++++++++---- packages/codecs-numbers/src/short-u16.ts | 19 +++++++------------ 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/.changeset/quiet-shorts-refuse.md b/.changeset/quiet-shorts-refuse.md index 820503740..09224e812 100644 --- a/.changeset/quiet-shorts-refuse.md +++ b/.changeset/quiet-shorts-refuse.md @@ -2,4 +2,4 @@ '@solana/codecs-numbers': patch --- -Fixed `getShortU16Decoder()` silently accepting malformed input: a truncated buffer decoded to garbage with an offset past the end of the byte array, and continuation chains longer than the documented three-byte encoding produced values outside the u16 domain. Truncated input now throws `SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH` and overlong chains throw `SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE`, matching the guards every other number decoder already uses. +Fixed `getShortU16Decoder()` silently accepting malformed input: a truncated buffer decoded to garbage with an offset past the end of the byte array, and continuation chains longer than the documented three-byte encoding decoded to values outside the u16 domain. Malformed input now throws `SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH`, and decoded values above 65,535 now throw `SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE`, matching the guards every other number decoder already uses. diff --git a/packages/codecs-numbers/src/__tests__/short-u16-test.ts b/packages/codecs-numbers/src/__tests__/short-u16-test.ts index c0da094da..90b98d374 100644 --- a/packages/codecs-numbers/src/__tests__/short-u16-test.ts +++ b/packages/codecs-numbers/src/__tests__/short-u16-test.ts @@ -81,12 +81,20 @@ describe('getShortU16Codec', () => { it('rejects continuation chains that exceed the three-byte encoding', () => { expect.hasAssertions(); + // A 4-byte chain needs a fourth byte, which violates the 3-byte encoding cap. expect(() => shortU16().decode(new Uint8Array([0xff, 0xff, 0xff, 0x00]))).toThrow( - new SolanaError(SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE, { + new SolanaError(SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH, { + bytesLength: 4, codecDescription: 'shortU16', - max: MAX, - min: MIN, - value: 0x7f | (0x7f << 7) | (0x7f << 14), + expected: 3, + }), + ); + // Same rejection when the buffer ends exactly at three continuation bytes. + expect(() => shortU16().decode(new Uint8Array([0xff, 0xff, 0xff]))).toThrow( + new SolanaError(SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH, { + bytesLength: 4, + codecDescription: 'shortU16', + expected: 3, }), ); }); diff --git a/packages/codecs-numbers/src/short-u16.ts b/packages/codecs-numbers/src/short-u16.ts index 1cfb5787a..7563fbbf8 100644 --- a/packages/codecs-numbers/src/short-u16.ts +++ b/packages/codecs-numbers/src/short-u16.ts @@ -9,10 +9,12 @@ import { VariableSizeDecoder, VariableSizeEncoder, } from '@solana/codecs-core'; -import { SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE, SolanaError } from '@solana/errors'; +import { SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH, SolanaError } from '@solana/errors'; import { assertNumberIsBetweenForCodec } from './assertions'; +const MAX_SHORT_U16_VALUE = 65535; + /** * Returns an encoder for `shortU16` values. * @@ -101,21 +103,14 @@ export const getShortU16Decoder = (): VariableSizeDecoder => value |= nextSevenBits << (byteIndex * 7); if ((currentByte & 0b10000000) === 0) { // This byte does not have its continuation bit set. We're done. - // Every byte only contributes 7 bits, so a terminated - // 3-byte encoding can still exceed the u16 domain - // (e.g. [0x80, 0x80, 0x04] is 65536). Reject it here so - // the decoder never returns a value the encoder refuses. - assertNumberIsBetweenForCodec('shortU16', 0, 65535, value); + assertNumberIsBetweenForCodec('shortU16', 0, MAX_SHORT_U16_VALUE, value); return [value, offset + byteCount]; } } - // Every encoded shortU16 fits in at most three bytes. A continuation bit on - // what would be the fourth byte implies a value outside the u16 domain. - throw new SolanaError(SOLANA_ERROR__CODECS__NUMBER_OUT_OF_RANGE, { + throw new SolanaError(SOLANA_ERROR__CODECS__INVALID_BYTE_LENGTH, { + bytesLength: 4, codecDescription: 'shortU16', - max: 65535, - min: 0, - value, + expected: 3, }); }, });