fix(borsh): encode layouts larger than 1000 bytes without error - #4961
Open
latent-9 wants to merge 1 commit into
Open
fix(borsh): encode layouts larger than 1000 bytes without error#4961latent-9 wants to merge 1 commit into
latent-9 wants to merge 1 commit into
Conversation
The instruction, account, and type coders allocated a fixed 1000-byte
scratch buffer for every encode. Anything that serialized larger than
that (a Vec<PublicKey> with 32 entries, a multi-KB Vec<u8>, a large
account struct) threw a confusing RangeError from buffer-layout
("encoding overruns Buffer") or the borsh length guards, so valid data
could not be encoded at all.
Add encodeLayout() in @anchor-lang/borsh that allocates exactly
layout.span when the size is fixed and grows the scratch buffer on
overflow for variable-size layouts. Route all three coders through it.
latent-9
requested review from
chen-robert,
jamie-osec and
renato-osec
as code owners
August 23, 2026 09:24
|
@latent-9 is attempting to deploy a commit to the OtterSec Team on Vercel. A member of the Team first needs to authorize it. |
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.
Summary
The Borsh instruction, account, and type coders in the TS package allocate a
fixed
Buffer.alloc(1000)scratch buffer for every encode. Any payload thatserializes to more than 1000 bytes fails hard: buffer-layout throws
RangeError: encoding overruns Buffer(or the borsh length guards throwInvalid bytes: need N bytes, only M remaining), so the data can never beencoded at all.
This is not an exotic case. A single instruction taking
Vec<PublicKey>with32 entries serializes to 4 + 32*32 = 1028 bytes, and Solana transactions allow
instruction data well past that (the tx size cap is 1232 bytes, and account /
type payloads have no such limit). Multi-KB
Vec<u8>arguments, large accountstructs, and batch operations all hit this wall with a confusing error instead
of working.
Change
Add
encodeLayout()to@anchor-lang/borsh:layout.span > 0) allocate exactlylayout.spanbytes.buffer and grow on overflow, so values larger than the initial allocation
are never dropped. Genuine input errors are rethrown, not retried.
Route
BorshInstructionCoder.encode,BorshAccountsCoder.encode, andBorshTypesCoder.encodethrough it, removing the duplicatedBuffer.alloc(1000)/slicepattern.Tests
Add a regression test in
coder-instructions.spec.tsthat encodes aninstruction with 32 public keys (1028 bytes) and asserts the round-trip is
intact. Without the fix it throws
RangeError: encoding overruns Buffer;with the fix it passes. The full coder suite (instruction/account/type/malformed
lengths) passes: 33/33.