Skip to content

fix(encode): a zero-length output buffer is an installation, not an argument error - #128

Merged
Andste82 merged 1 commit into
mainfrom
fix/zero-length-output-buffer
Oct 12, 2026
Merged

Andste82 merged 1 commit into
mainfrom
fix/zero-length-output-buffer

Conversation

@Andste82

Copy link
Copy Markdown
Member

The Java half of the corelib work for sofa-buffers/generator#738 — an empty message is valid and has to work.

The bug

checkHandover() refused buffer.length == 0 on every path (both constructors, bufferSet(), reset()), and overScratch() refused any size below 1. Its own javadoc, three lines up, says the opposite:

{@link Sofab#MIN_OUTPUT_BUFFER} applies only when a sink is present. Without one no flush can occur, so §5.1 imposes no minimum: the buffer either holds the message or reports {@link SofabError#BUFFER_FULL}, and a caller sizing from the generated MAX_SIZE keeps an exact fit.

Zero is where that exact fit ends up. MESSAGE_SPEC §2 makes the all-default message the empty byte string, so a message with no fields has MAX_SIZE == 0, and the generated one-shot encode() — which is OStream.overScratch(MAX_SIZE) — threw instead of returning an empty array:

java.lang.IllegalArgumentException: scratch buffer size must be positive
  at org.sofabuffers.sofab.OStream.overScratch(OStream.java:313)
  at message.Empty.encode(Empty.java:21)

Decode was already fine (Empty.decode(new byte[0]) → COMPLETE), so this is the encode half only.

The fix

  • checkHandover() refuses a null array; an empty one is an installation with no room left.
  • overScratch() refuses a negative size; 0 is a size.
  • The sink path is untouched: an empty buffer with a sink is still rejected by the MIN_OUTPUT_BUFFER check, because a buffer a sink could never flush through is the configuration §5.1 forbids.
  • Javadoc on the constructors, bufferSet, reset and overScratch updated — they promised length > 0 and an IllegalArgumentException "if the buffer is empty".

Tests

Four existing tests pinned the old contract and now state the new one: EncoderPathCoverageTest.constructorRejectsEmptyBuffer / bufferSetRejectsEmptyBuffer, EncoderOverloadsTest.constructorValidatesArguments, and OStreamScratchTest.aNonPositiveSizeIsRefused (now aNegativeSizeIsRefusedButZeroIsASize). New case in MinOutputBufferTest, beside aSinklessBufferIsSubjectToNoMinimum:

  • new OStream(new byte[0]) installs, reports 0 bytes used, copies out an empty array, and answers a write with BUFFER_FULL — capacity, not a bad argument;
  • the same installation mid-stream through bufferSet, in both directions, and writing works again after a real buffer is installed;
  • null is still refused, and an empty buffer with a sink is still refused.

One thing worth naming, because it bit me while writing the test: overScratch(0) does not promise a zero-length buffer. The per-thread scratch only ever grows, so the stream handed back may sit on a much larger buffer and a write into it can succeed. What holds is that the ask is legal and that nothing is written — which is exactly what the generated encode() needs.

Whole suite green: 1620 tests, 0 failures.

…rgument error

checkHandover() refused `buffer.length == 0` on every path -- the constructors,
bufferSet() and reset() -- and overScratch() refused a size below 1. Its own
javadoc says the opposite three lines up: "Without one no flush can occur, so
§5.1 imposes no minimum: the buffer either holds the message or reports
BUFFER_FULL, and a caller sizing from the generated MAX_SIZE keeps an exact
fit."

Zero is where that exact fit ends up. MESSAGE_SPEC §2 makes the all-default
message the empty byte string, so a message with NO FIELDS has MAX_SIZE == 0,
and the generated one-shot encode() -- which calls overScratch(MAX_SIZE) --
threw IllegalArgumentException("scratch buffer size must be positive") instead
of returning an empty array (sofa-buffers/generator#738).

Now only a null array is refused, and the sink path still rejects an empty
buffer through the MIN_OUTPUT_BUFFER check: a buffer a sink could never flush
through is the configuration §5.1 forbids, and that is unchanged.

Four existing tests pinned the old contract and state the new one instead
(EncoderPathCoverageTest x2, EncoderOverloadsTest, OStreamScratchTest's
non-positive-size case). Note what overScratch(0) does NOT promise: the
per-thread scratch only ever grows, so the stream handed back may sit on a
larger buffer -- what holds is that the ask is legal and that nothing is
written, which is what the new test asserts, next to the exact-length
installation that does report BUFFER_FULL on a write.

Whole suite green: 1620 tests, 0 failures.
@Andste82
Andste82 merged commit 51d91ff into main Oct 12, 2026
4 checks passed
@Andste82
Andste82 deleted the fix/zero-length-output-buffer branch October 12, 2026 17:53
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 this pull request may close these issues.

1 participant