Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions src/main/java/org/sofabuffers/sofab/OStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public final class OStream {
* Create an encoder over {@code buffer} with no flush sink. Writing past the
* end of the buffer raises {@link SofabError#BUFFER_FULL}.
*
* @param buffer caller-owned output buffer (length > 0)
* @param buffer caller-owned output buffer
*/
public OStream(byte[] buffer) {
this(buffer, 0, null);
Expand All @@ -223,13 +223,13 @@ public OStream(byte[] buffer, int offset) {
* usable bytes — {@code buffer.length - offset} — and is rejected here if it
* does not (CORELIB_PLAN §5.1).
*
* @param buffer caller-owned output buffer (length > 0)
* @param buffer caller-owned output buffer
* @param offset initial write position ({@code 0..buffer.length})
* @param sink flush sink, or {@code null} for none
* @throws IllegalArgumentException if the buffer is empty, the offset is out of
* range, or a sink is given and the buffer
* leaves less than {@link Sofab#MIN_OUTPUT_BUFFER}
* usable bytes
* @throws IllegalArgumentException if the buffer is {@code null}, the offset is
* out of range, or a sink is given and the
* buffer leaves less than
* {@link Sofab#MIN_OUTPUT_BUFFER} usable bytes
*/
public OStream(byte[] buffer, int offset, FlushSink sink) {
// The one moment an encoder allocates (CORELIB_PLAN §6.6): this object and
Expand All @@ -250,10 +250,18 @@ public OStream(byte[] buffer, int offset, FlushSink sink) {
* 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 {@code MAX_SIZE} keeps an exact fit.
*
* <p>"No minimum" includes <b>zero</b>, which is where the exact fit ends up:
* MESSAGE_SPEC §2 makes the all-default message the empty byte string, so a
* message with no fields has {@code MAX_SIZE == 0} and a caller sizing from it
* hands over an empty array. That is an installation with no room left rather
* than a broken one — writing into it is {@link SofabError#BUFFER_FULL} — and
* only the sink case below still rejects it, because a sink it could never
* flush through is the configuration §5.1 forbids.
*/
private static void checkHandover(byte[] buffer, int offset, FlushSink sink) {
if (buffer == null || buffer.length == 0) {
throw new IllegalArgumentException("buffer must be non-empty");
if (buffer == null) {
throw new IllegalArgumentException("buffer must not be null");
}
if (offset < 0 || offset > buffer.length) {
throw new IllegalArgumentException("offset out of range");
Expand Down Expand Up @@ -304,13 +312,14 @@ public int bytesUsed() {
* buffer plus a sink.
*
* @param size smallest buffer that will do, normally the generated
* {@code MAX_SIZE}
* {@code MAX_SIZE} — {@code 0} for a message with no fields, whose
* encoding is the empty byte string
* @return an encoder positioned at the start of this thread's scratch buffer
* @throws IllegalArgumentException if {@code size} is not positive
* @throws IllegalArgumentException if {@code size} is negative
*/
public static OStream overScratch(int size) {
if (size < 1) {
throw new IllegalArgumentException("scratch buffer size must be positive");
if (size < 0) {
throw new IllegalArgumentException("scratch buffer size must not be negative");
}
byte[] buf = SCRATCH.get();
if (buf == null || buf.length < size) {
Expand Down Expand Up @@ -384,11 +393,11 @@ public int flush() throws IOException {
* storage the encoder could not write a single byte into (CORELIB_PLAN §5.1).
* On a sink-less stream no minimum applies.
*
* @param buffer new caller-owned output buffer (length &gt; 0)
* @param buffer new caller-owned output buffer
* @param offset initial write position ({@code 0..buffer.length})
* @throws IllegalArgumentException if the buffer is empty, the offset is out of
* range, or this stream carries a sink and the
* buffer leaves less than
* @throws IllegalArgumentException if the buffer is {@code null}, the offset is
* out of range, or this stream carries a sink
* and the buffer leaves less than
* {@link Sofab#MIN_OUTPUT_BUFFER} usable bytes
*/
public void bufferSet(byte[] buffer, int offset) {
Expand Down Expand Up @@ -424,7 +433,7 @@ public void bufferSet(byte[] buffer, int offset) {
* {@link #nPending} is zero, which is cleared here. Everything else this class declares is restored, and
* {@code ResetCoversEveryFieldTest} holds that to every field added later.
*
* @param buffer caller-owned output buffer (length &gt; 0)
* @param buffer caller-owned output buffer
*/
public void reset(byte[] buffer) {
bufferSet(buffer, 0);
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/org/sofabuffers/sofab/EncoderOverloadsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,11 @@ void writeSequenceEndWithoutOpenSequenceIsNotRejected() throws Exception {

@Test
void constructorValidatesArguments() {
assertThrows(IllegalArgumentException.class, () -> new OStream(new byte[0]));
assertThrows(IllegalArgumentException.class, () -> new OStream(new byte[8], 9));
assertThrows(IllegalArgumentException.class, () -> new OStream(null));
assertThrows(IllegalArgumentException.class, () -> new OStream((byte[]) null));
// An EMPTY buffer is not an invalid argument: without a sink §5.1 imposes no
// minimum, and zero is the MAX_SIZE of a message with no fields.
new OStream(new byte[0]);
}

@Test
Expand All @@ -291,7 +293,7 @@ void bufferSetSwapsBufferAndValidates() throws IOException {
});
assertEquals(List.of(42L), got);

assertThrows(IllegalArgumentException.class, () -> os.bufferSet(new byte[0], 0));
assertThrows(IllegalArgumentException.class, () -> os.bufferSet((byte[]) null, 0));
assertThrows(IllegalArgumentException.class, () -> os.bufferSet(new byte[4], 99));
}
}
11 changes: 7 additions & 4 deletions src/test/java/org/sofabuffers/sofab/EncoderPathCoverageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ void constructorRejectsNullBuffer() {
}

@Test
void constructorRejectsEmptyBuffer() {
assertThrows(IllegalArgumentException.class, () -> new OStream(new byte[0]));
void constructorAcceptsEmptyBuffer() {
// Without a sink no minimum applies (§5.1), and zero is the MAX_SIZE of a
// message with no fields: an installation with no room, not a broken one.
assertEquals(0, new OStream(new byte[0]).bytesUsed());
}

@Test
Expand All @@ -77,9 +79,10 @@ void constructorAcceptsOffsetAtEnd() {
}

@Test
void bufferSetRejectsEmptyBuffer() {
void bufferSetAcceptsEmptyBufferAndRejectsNull() {
OStream os = new OStream(new byte[8]);
assertThrows(IllegalArgumentException.class, () -> os.bufferSet(new byte[0], 0));
os.bufferSet(new byte[0], 0);
assertEquals(0, os.bytesUsed());
assertThrows(IllegalArgumentException.class, () -> os.bufferSet(null, 0));
}

Expand Down
46 changes: 46 additions & 0 deletions src/test/java/org/sofabuffers/sofab/MinOutputBufferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,52 @@ void aSinklessBufferIsSubjectToNoMinimum() throws IOException {
assertArrayEquals(new byte[] {0x08, 0x01}, tight);
}

/**
* The degenerate end of the same rule, and the case the handover check used to
* refuse outright (sofa-buffers/generator#738). "No minimum" includes zero:
* MESSAGE_SPEC §2 makes the all-default message the empty byte string, so a
* message with no fields has {@code MAX_SIZE == 0} and a caller sizing from it
* hands over an empty array. That is a buffer with no room left, not a broken
* one — writing into it is BUFFER_FULL — and {@code overScratch(0)}, which is
* what the generated one-shot {@code encode()} calls, has to answer the same way.
*/
@Test
void aZeroLengthBufferIsAnInstallationWithNoRoom() throws IOException {
OStream empty = new OStream(new byte[0]);
assertEquals(0, empty.bytesUsed(), "nothing is written yet");
assertArrayEquals(new byte[0], empty.copyOfBytesUsed(),
"the message that encodes to nothing encodes to nothing");
SofabException e = assertThrows(SofabException.class, () -> empty.writeUnsigned(1, 1),
"a write into no room is refused");
assertEquals(SofabError.BUFFER_FULL, e.error(),
"and refused as capacity, not as a bad argument");

// overScratch(0) is the same ASK -- it is what the generated one-shot
// encode() of a field-less message makes -- but not the same buffer: the
// per-thread scratch only ever grows, so what comes back may be larger than
// what was asked for. What has to hold either way is that the ask is legal
// and that nothing is written.
OStream scratch = OStream.overScratch(0);
assertEquals(0, scratch.bytesUsed(), "nothing is written yet");
assertArrayEquals(new byte[0], scratch.copyOfBytesUsed(), "and nothing comes out");

// The same installation mid-stream, in both directions.
byte[] buf = new byte[4];
OStream os = new OStream(buf);
os.bufferSet(new byte[0], 0);
assertEquals(SofabError.BUFFER_FULL,
assertThrows(SofabException.class, () -> os.writeUnsigned(1, 1)).error());
os.reset(buf);
os.writeUnsigned(1, 1);
assertEquals(2, os.bytesUsed(), "a real buffer installed after it writes again");

// Only a null array is still refused, and a sink still cannot take one:
// a buffer it could never flush through is what §5.1 forbids.
assertThrows(IllegalArgumentException.class, () -> new OStream((byte[]) null));
assertThrows(IllegalArgumentException.class,
() -> new OStream(new byte[0], 0, (data, off, len) -> { }));
}

/**
* The repro from the report: a zero-usable-byte buffer with a sink used to be
* accepted, and the first flush emitted the untouched reservation as if it were
Expand Down
17 changes: 14 additions & 3 deletions src/test/java/org/sofabuffers/sofab/OStreamScratchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,22 @@ void anInstalledForeignBufferDoesNotSurviveTheNextCall() throws IOException {
"the next message landed in the foreign buffer, overwriting its first");
}

/** A size that could not hold anything is refused where it is asked for. */
/**
* A negative size is refused where it is asked for. Zero is not negative: it is
* the {@code MAX_SIZE} of a message with no fields, whose encoding is the empty
* byte string (MESSAGE_SPEC §2), and the generated one-shot {@code encode()}
* asks for exactly that (sofa-buffers/generator#738). What it gets back is a
* stream that has written nothing -- over whatever buffer this thread already
* holds, since the scratch only ever grows.
*/
@Test
void aNonPositiveSizeIsRefused() {
assertThrows(IllegalArgumentException.class, () -> OStream.overScratch(0));
void aNegativeSizeIsRefusedButZeroIsASize() {
assertThrows(IllegalArgumentException.class, () -> OStream.overScratch(-1));

OStream os = OStream.overScratch(0);
assertEquals(0, os.bytesUsed(), "nothing written");
assertArrayEquals(new byte[0], os.copyOfBytesUsed(),
"the message that encodes to nothing encodes to nothing");
}

// --- copyOfBytesUsed -----------------------------------------------------
Expand Down
Loading