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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ is an argument, an element type is a type parameter.
| `Seq.reset` / `Seq.orEmpty` / `Seq.boolsToLongs` | re-arm a reused destination in place; absorb a null field on the encode side; the one boxed-to-primitive conversion `bool` still needs |
| `PayloadAcc` | reassemble a `string` / `blob` payload split across feeds — a payload that arrives whole never touches its buffer, and the value never depends on where the split fell; the receiver cap on the announced length arrives as a `Bound` and is compared before the first chunk is taken (§6.2.1) |
| `Utf8.decode` | validate a byte range and materialize it, in that order — the only order in which invalid UTF-8 can still be rejected (§6.4) |
| `Sofab.invalid` | the carrier a `Visitor` rejects malformed input through, since a callback declares no checked exception; `IStream.feed` latches it as terminal like its own rejections |
| `Sofab.invalid` | the carrier a `Visitor` rejects malformed input through, since a callback declares no checked exception; `IStream.feed` latches it as terminal like its own rejections and unwraps it, so the caller catches the checked `SofabException` |
| `Sofab.limitExceeded` / `Sofab.argument` | the twin carrier for a receiver-limit refusal, which is latched as terminal too but under its own code, **never** as `INVALID`; and the carrier for a defect in the call, such as a bound the caller never stated |
| `Bound.schema` / `Bound.receiver` / `Bound.SCHEMA_BOUNDED` | which of §6.2.1's two rules bounds one field, with its number and its verdict: the schema's own `count`/`maxlen` (`INVALID_MSG`), or the deployment's configured cap (`LIMIT_EXCEEDED`); `SCHEMA_BOUNDED` is the numberless statement for a payload whose `maxlen` the caller has already enforced |
| `OStream.overScratch` / `copyOfBytesUsed` | a per-thread buffer for a one-shot `encode()`, so the worst case is allocated once per thread rather than once per call. The **size** stays with the caller (CORELIB_PLAN §5.1): generated code passes its own `MAX_SIZE` |
Expand Down
13 changes: 12 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
is what Maven admits) plus artifactId corelib. The normative half is the
namespace, and every symbol is under org.sofabuffers.sofab. -->
<artifactId>corelib</artifactId>
<!-- 0.13.0: source-breaking, in three places. Sofab.API_VERSION is the WIRE
<!-- 0.13.0: source-breaking, in four places. Sofab.API_VERSION is the WIRE
contract and stays 1 through all of them: the bytes are unchanged.

(a) IStream.feed now RETURNS the decode outcome —
Expand Down Expand Up @@ -47,6 +47,17 @@
the one case with no second number to compare — a string/blob whose maxlen
the caller already enforced at the length word, where PayloadAcc has nothing
to hold it to.

(d) IStream.feed unwraps a rejection a Visitor raised through the
UncheckedIOException carrier (Sofab.invalid / limitExceeded / argument) and
throws the SofabException it declares, for every code (#129). Generated code
rejects schema bounds and receiver caps from inside the visitor, so a caller
catching what feed's signature promised never caught them; LIMIT_EXCEEDED in
particular was documented as arriving wrapped. Callers that caught
UncheckedIOException around feed and read getCause() catch SofabException
instead. A visitor's own I/O failure (a cause that is not a SofabException)
still passes through as thrown, and inside a callback the carriers are
unchanged.
(0.12.0: source-breaking. The three §6.2.1 receiver caps became arguments on
the calls generated code already makes — PayloadAcc.string/blob take the cap
on the announced length, Seq.reserveRow* the cap on the row index — so every
Expand Down
43 changes: 28 additions & 15 deletions src/main/java/org/sofabuffers/sofab/IStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,21 @@ public DecodeStatus feed(byte[] data, Visitor visitor) throws SofabException {
* nothing further and rethrows {@code INVALID_MSG} for every subsequent call
* until {@link #reset()} begins a new message.
* {@link SofabError#LIMIT_EXCEEDED} — a receiver-cap refusal of well-formed
* bytes (§6.2.1) — is terminal too (§6.3) and behaves the same way, rethrown in
* the carrier it arrived in and under its own code, never folded into
* {@code INVALID_MSG}. So is {@link SofabError#ARGUMENT}, raised where a
* {@link Visitor#arrayBulk} destination is too short for the count it was
* offered (§6.6.3). Running out of bytes mid-field is <em>not</em> any of that:
* bytes (§6.2.1) — is terminal too (§6.3) and behaves the same way, rethrown
* under its own code, never folded into {@code INVALID_MSG}. So is
* {@link SofabError#ARGUMENT}, raised where a {@link Visitor#arrayBulk}
* destination is too short for the count it was offered (§6.6.3). Running out of bytes mid-field is <em>not</em> any of that:
* it suspends and resumes on the next call, as before.
*
* <p><b>Every rejection arrives as the checked {@link SofabException}</b>,
* including one raised by generated code inside the {@link Visitor}. A visitor
* method cannot throw a checked exception, so that code reports it wrapped in
* an {@link UncheckedIOException} ({@link Sofab#invalid},
* {@link Sofab#limitExceeded}, {@link Sofab#argument}); this method is the
* public boundary and unwraps it.
* Only a visitor's own I/O failure — an {@code UncheckedIOException} whose
* cause is not a {@code SofabException} — passes through as thrown.
*
* @param data backing array
* @param off start offset
* @param len number of bytes to consume
Expand All @@ -389,18 +397,16 @@ public DecodeStatus feed(byte[] data, Visitor visitor) throws SofabException {
* {@link DecodeStatus#INCOMPLETE}
* @throws SofabException with {@link SofabError#INVALID_MSG} on malformed input,
* or on any call after malformed input was already rejected; with
* {@link SofabError#LIMIT_EXCEEDED} where a receiver limit refuses this
* message, and on every call after that; with
* {@link SofabError#ARGUMENT} where a bulk destination is too short for
* the count it was offered, and on every call after that
* @throws java.io.UncheckedIOException wrapping a {@code LIMIT_EXCEEDED}
* {@link SofabException} where a receiver limit refuses this message,
* and on every call after that
*/
public DecodeStatus feed(byte[] data, int off, int len, Visitor visitor) throws SofabException {
if (terminal == SofabError.LIMIT_EXCEEDED) {
// Repeated in the carrier it was raised through, so the caller's
// existing catch still sees it, and still under its own code: §6.3
// MUST NOT report a receiver-limit rejection as InvalidMessage.
throw Sofab.limitExceeded(
// Repeated under its own code: §6.3 MUST NOT report a receiver-limit
// rejection as InvalidMessage.
throw new SofabException(SofabError.LIMIT_EXCEEDED,
"decode already refused by a receiver limit; reset() to start a new message");
}
if (terminal == SofabError.ARGUMENT) {
Expand Down Expand Up @@ -432,9 +438,16 @@ public DecodeStatus feed(byte[] data, int off, int len, Visitor visitor) throws
// by wrapping a SofabException, and refuses a receiver cap (§6.2.1) the
// same way. Both end the decode, so both latch here, each keeping its
// own code: §6.3 makes the policy rejection terminal too and forbids
// reporting it as InvalidMessage.
if (e.getCause() instanceof SofabException cause && isTerminal(cause.error())) {
terminal = cause.error();
// reporting it as InvalidMessage. The wrapper is only how the
// rejection crossed the Visitor interface; past this boundary it
// leaves as the SofabException this method declares (#129). A cause
// that is not one — the visitor's own I/O failure — is not ours to
// reinterpret and passes through as thrown.
if (e.getCause() instanceof SofabException cause) {
if (isTerminal(cause.error())) {
terminal = cause.error();
}
throw cause;
}
throw e;
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/sofabuffers/sofab/Sofab.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ private Sofab() {
* well-formed bytes (§6.2.1) — is deliberately not this: it travels through
* {@link #limitExceeded(String)} instead, and is latched under its own code.
*
* <p>The wrapper only carries the rejection across the {@link Visitor}
* interface: {@link IStream#feed} unwraps it, so the decode's caller catches
* the {@link SofabException} {@code feed} declares, as it does for the
* decoder's own rejections. The same holds for {@link #limitExceeded} and
* {@link #argument}.
*
* @param detail human-readable context, naming the field and the bound it broke
* @return the exception to throw
*/
Expand Down
6 changes: 2 additions & 4 deletions src/test/java/org/sofabuffers/sofab/BooleanTolerantTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,9 @@ private static SofabError rejectionOf(IStream in, byte[] data, Visitor sink) {
DecodeStatus st = in.feed(data, sink);
return fail("decode survived and answered " + st + "; want a rejection");
} catch (SofabException e) {
// A refusal raised from a visitor callback too: feed unwraps its
// UncheckedIOException carrier (#129).
return e.error();
} catch (UncheckedIOException e) {
// How a refusal raised from a visitor callback, which declares no checked
// exception, reaches the caller.
return ((SofabException) e.getCause()).error();
}
}

Expand Down
12 changes: 4 additions & 8 deletions src/test/java/org/sofabuffers/sofab/HeaderLimitsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -599,19 +599,15 @@ private static Outcome rejectionFrom(IStream in, HeaderDest dest, List<byte[]> p
}

/**
* The rejection's category, whichever carrier it arrived on. A refusal raised
* inside the decoder is a bare {@link SofabException}; one raised from a visitor
* callback — which declares no checked exception — is wrapped in an
* {@link UncheckedIOException}, and the re-raise a terminal verdict produces may
* use either.
* The rejection's category. Every refusal reaches the caller as a bare
* {@link SofabException}, including one raised from a visitor callback, which
* {@code feed} unwraps from its {@link java.io.UncheckedIOException} carrier
* (#129); anything else is not a sofab rejection.
*/
private static SofabError categoryOf(Throwable t) {
if (t instanceof SofabException e) {
return e.error();
}
if (t instanceof UncheckedIOException u && u.getCause() instanceof SofabException e) {
return e.error();
}
throw new AssertionError("not a sofab rejection: " + t, t);
}

Expand Down
20 changes: 12 additions & 8 deletions src/test/java/org/sofabuffers/sofab/InvalidIsTerminalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ void truncationDoesNotLatch() throws Exception {
* is explicitly NOT the INVALID outcome, so it must not latch one. It is
* terminal all the same and latches under its own code — that half belongs to
* {@link LimitExceededIsTerminalTest}. Generated code raises it from a visitor
* callback, wrapped because {@link Visitor} declares no checked exception.
* callback, wrapped because {@link Visitor} declares no checked exception,
* and {@code feed} unwraps it back into the {@link SofabException} it declares.
*/
@Test
void aLimitExceededFromTheVisitorDoesNotLatchInvalid() {
Expand All @@ -250,14 +251,14 @@ public void unsigned(int id, long value) {
}
};

UncheckedIOException e = assertThrows(UncheckedIOException.class,
SofabException e = assertThrows(SofabException.class,
() -> is.feed(bytes(0x00, 0x2A), limiter));
assertEquals(SofabError.LIMIT_EXCEEDED, ((SofabException) e.getCause()).error());
assertEquals(SofabError.LIMIT_EXCEEDED, e.error());
// Latched, but under its own code: the repeat rejection is still
// LIMIT_EXCEEDED, never folded into INVALID_MSG (§6.3).
UncheckedIOException again = assertThrows(UncheckedIOException.class,
SofabException again = assertThrows(SofabException.class,
() -> is.feed(bytes(0x00, 0x2A), limiter));
assertEquals(SofabError.LIMIT_EXCEEDED, ((SofabException) again.getCause()).error(),
assertEquals(SofabError.LIMIT_EXCEEDED, again.error(),
"a policy limit is not INVALID");
}

Expand Down Expand Up @@ -309,9 +310,9 @@ public void unsigned(int id, long value) {
}
};

UncheckedIOException e = assertThrows(UncheckedIOException.class,
SofabException e = assertThrows(SofabException.class,
() -> is.feed(bytes(0x00, 0x2A), unbounded));
assertEquals(SofabError.ARGUMENT, ((SofabException) e.getCause()).error());
assertEquals(SofabError.ARGUMENT, e.error());

// Nothing latched: the next feed decodes and answers for itself.
RecordingVisitor v = new RecordingVisitor();
Expand Down Expand Up @@ -339,8 +340,11 @@ public void fixlenBegin(int id, FixlenType subtype, int total) {
};

// string field id 0, declared length 3 - over the visitor's schema bound.
assertThrows(UncheckedIOException.class,
// Reported as the SofabException feed declares, not as the carrier the
// visitor threw it through (#129).
SofabException e = assertThrows(SofabException.class,
() -> is.feed(bytes(0x02, 0x1A, 0x61, 0x62, 0x63), bound));
assertEquals(SofabError.INVALID_MSG, e.error());

assertLatchedInvalid(is);
assertRejects(is, bound, bytes(0x00, 0x2A));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import static org.sofabuffers.sofab.common.Wire.bytes;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -150,21 +149,16 @@ private static void feedIn(IStream in, byte[] data, Visitor v, int chunk) throws
}
}

/** The error a visitor-side rejection carries out of a callback. */
private static SofabError categoryOf(UncheckedIOException e) {
return ((SofabException) e.getCause()).error();
}

/**
* A fresh decoder fed the capped message in {@code chunk}-byte slices, up to
* and including the LIMIT_EXCEEDED refusal that §6.2.1 says the cap must
* raise. Returns the decoder, parked exactly where the refusal left it.
*/
private static IStream refused(Capping v, int chunk) throws IOException {
IStream in = new IStream();
UncheckedIOException e = assertThrows(UncheckedIOException.class,
SofabException e = assertThrows(SofabException.class,
() -> feedIn(in, message(), v, chunk), FINDING + ": the cap must refuse, chunk=" + chunk);
assertEquals(SofabError.LIMIT_EXCEEDED, categoryOf(e),
assertEquals(SofabError.LIMIT_EXCEEDED, e.error(),
FINDING + ": a receiver cap is LIMIT_EXCEEDED, never INVALID_MSG (§6.3), chunk=" + chunk);
assertEquals(List.of(THE_ONLY_FIELD_SENT), v.events,
FINDING + ": the refusal happens at the length header, chunk=" + chunk);
Expand All @@ -191,11 +185,11 @@ void theOutcomeIsTerminalAfterALimitRefusal_CORELIB_JAVA_10() throws IOException
// feed call. An empty feed adds no bytes and so puts exactly the old
// question - "where do I stand?" - and a refused decode answers it with
// the refusal rather than with COMPLETE or INCOMPLETE.
UncheckedIOException after = assertThrows(UncheckedIOException.class,
SofabException after = assertThrows(SofabException.class,
() -> in.feed(new byte[0], v),
FINDING + ": a refused message neither completed (§6.3, §5.2.1) nor may "
+ "invite more bytes (§6.3, §5.2.3), chunk=" + chunk);
assertEquals(SofabError.LIMIT_EXCEEDED, categoryOf(after),
assertEquals(SofabError.LIMIT_EXCEEDED, after.error(),
FINDING + ": the refusal keeps its own code (§6.3), chunk=" + chunk);
}
}
Expand All @@ -215,11 +209,11 @@ void aFurtherFeedDoesNotResumeAfterALimitRefusal_CORELIB_JAVA_10() throws IOExce
Capping v = new Capping();
IStream in = refused(v, chunk);

UncheckedIOException again = assertThrows(UncheckedIOException.class,
SofabException again = assertThrows(SofabException.class,
() -> in.feed(bytes(0x00, 0x2A), v),
FINDING + ": a feed after a policy refusal must be refused too (§6.3), chunk="
+ chunk);
assertEquals(SofabError.LIMIT_EXCEEDED, categoryOf(again),
assertEquals(SofabError.LIMIT_EXCEEDED, again.error(),
FINDING + ": the repeat must keep the LimitExceeded code and must not be "
+ "reported as InvalidMessage (§6.3), chunk=" + chunk);
assertEquals(List.of(THE_ONLY_FIELD_SENT), v.events,
Expand Down Expand Up @@ -247,7 +241,7 @@ void noFabricatedFieldReachesTheVisitorAfterALimitRefusal_CORELIB_JAVA_10() thro

try {
in.feed(PAYLOAD, v);
} catch (UncheckedIOException | SofabException expected) {
} catch (SofabException expected) {
// A terminal decoder refuses the continuation; that is the point
// of case (b). What this case asserts is what reached the visitor.
}
Expand All @@ -271,9 +265,9 @@ void wholeAndChunkedFeedsAgreeAfterALimitRefusal_CORELIB_JAVA_10() throws IOExce
for (int chunk : CHUNKS) {
Capping v = new Capping();
IStream in = refused(v, chunk);
UncheckedIOException after = assertThrows(UncheckedIOException.class,
SofabException after = assertThrows(SofabException.class,
() -> in.feed(new byte[0], v));
verdicts.add(chunk + " -> " + categoryOf(after));
verdicts.add(chunk + " -> " + after.error());
}
assertEquals(1, verdicts.stream().map(s -> s.substring(s.indexOf("-> "))).distinct().count(),
FINDING + ": the two decode surfaces disagree after the same refusal: " + verdicts);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/sofabuffers/sofab/ReceiverCapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -473,14 +473,14 @@ void anElementIsBoundedAtTheLengthWordBeforeItsPayloadIsTaken() {
void aCapBreachIsNotTheInvalidOutcome() throws IOException {
IStream in = new IStream();
byte[] msg = oneString(64);
UncheckedIOException e = assertThrows(UncheckedIOException.class,
SofabException e = assertThrows(SofabException.class,
() -> in.feed(msg, new CappedVisitor(CAP)));
assertEquals(SofabError.LIMIT_EXCEEDED, categoryOf(e));
assertEquals(SofabError.LIMIT_EXCEEDED, e.error());
// The latched verdict keeps that code too: asking again (an empty feed adds
// no bytes) repeats LIMIT_EXCEEDED and never INVALID_MSG.
UncheckedIOException again = assertThrows(UncheckedIOException.class,
SofabException again = assertThrows(SofabException.class,
() -> in.feed(new byte[0], new CappedVisitor(CAP)));
assertEquals(SofabError.LIMIT_EXCEEDED, categoryOf(again),
assertEquals(SofabError.LIMIT_EXCEEDED, again.error(),
"well-formed bytes this receiver declines are not malformed bytes");
}

Expand Down
Loading
Loading