Repository navigation
fix(encode): a zero-length output buffer is an installation, not an argument error - #128
Merged
Merged
Conversation
…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.
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.
The Java half of the corelib work for sofa-buffers/generator#738 — an empty message is valid and has to work.
The bug
checkHandover()refusedbuffer.length == 0on every path (both constructors,bufferSet(),reset()), andoverScratch()refused any size below 1. Its own javadoc, three lines up, says the opposite: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-shotencode()— which isOStream.overScratch(MAX_SIZE)— threw instead of returning an empty array: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;0is a size.MIN_OUTPUT_BUFFERcheck, because a buffer a sink could never flush through is the configuration §5.1 forbids.bufferSet,resetandoverScratchupdated — they promisedlength > 0and anIllegalArgumentException"if the buffer is empty".Tests
Four existing tests pinned the old contract and now state the new one:
EncoderPathCoverageTest.constructorRejectsEmptyBuffer/bufferSetRejectsEmptyBuffer,EncoderOverloadsTest.constructorValidatesArguments, andOStreamScratchTest.aNonPositiveSizeIsRefused(nowaNegativeSizeIsRefusedButZeroIsASize). New case inMinOutputBufferTest, besideaSinklessBufferIsSubjectToNoMinimum:new OStream(new byte[0])installs, reports 0 bytes used, copies out an empty array, and answers a write withBUFFER_FULL— capacity, not a bad argument;bufferSet, in both directions, and writing works again after a real buffer is installed;nullis 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 generatedencode()needs.Whole suite green: 1620 tests, 0 failures.