Skip to content

feat(encode): writeString(id, text, maxlen) refuses past the caller's bound - #127

Merged
Andste82 merged 2 commits into
mainfrom
feat/encode-bounds-656
Oct 9, 2026
Merged

Andste82 merged 2 commits into
mainfrom
feat/encode-bounds-656

Conversation

@Andste82

@Andste82 Andste82 commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Refs sofa-buffers/generator#656

What

OStream.writeString(int id, String text, int maxlen): a string that encodes to more than maxlen UTF-8 bytes is refused with SofabException(ARGUMENT) before any byte of the field is written (no lazily held sequence header is committed either), exactly like an unpaired surrogate.

  • The bound is the caller's. Generated code passes the schema maxlen of the field or array element; the library holds no bound of its own.
  • The unbounded writeString(id, text) does not delegate: both public writes share two private tails (ASCII header + bulk copy, measured header + UTF-8 loop), and only the bounded one compares.
  • No second pass, no allocation: a string with more UTF-16 chars than maxlen is refused before the measuring pass (every char encodes to at least one byte); otherwise the comparison runs on the byte length the existing measuring pass already returns.
  • README: the encode section documents the overload.

Why

The generator refuses an over-bound value at encode (ARCHITECTURE section 9.6, generator#656). For a String the UTF-8 byte length is only known inside writeString, so the bound has to ride that call instead of a generated pre-pass that would measure the string twice.

Tests

New StringMaxlenTest (6 tests): at the bound the bytes equal the unbounded write (ASCII below/above the bulk-copy threshold, every UTF-8 width, empty); ASCII refused on the char count; multi-byte refused on the byte length (ééé, xxxé, a surrogate pair, 40 x U+00E9 at 79); an unpaired surrogate inside the bound is still invalid UTF-8; a refusal leaves the stream usable and commits no held sequence header; a refusal reaches no sink.

mvn -B verify: all tests pass (6 new). JaCoCo, main 50a84b3 -> this branch:

counter main branch
instruction 98.68% (6075/6156) 98.70% (6132/6213)
branch 98.03% (697/711) 98.05% (703/717)
line 99.42% (1361/1369) 99.42% (1377/1385)
method 100% (165/165) 100% (169/169)

Measurement

Generator tests/bench java row (vehicle_telemetry, Callgrind, encode Ir/op, subtract method):

generated code corelib encode Ir/op
generator main (unbounded writeString calls) main 50a84b3 17022
generator main first commit of this PR (2-arg delegating to 3-arg) 17066 (+0.26%)
generator main this PR (no delegation) 16985 (-0.2%)

So the unbounded call costs nothing extra. The bounded call's two comparisons cost about 3 Ir per string on that workload (11 bounded strings, 35 Ir in total), measured by dropping the bound argument from the generated code.

… bound

A string whose UTF-8 encoding is longer than the caller's maxlen is refused
with SofabError.ARGUMENT before any byte of the field is written, the same
way an unpaired surrogate is. The byte length is measured by the pass
writeString already runs, so the bounded write costs no second pass and no
allocation; a string with more UTF-16 chars than maxlen is refused before
that pass, since every char encodes to at least one byte.

The bound is the caller's: generated code passes the schema maxlen of the
field or array element. The library holds no bound of its own, and the
two-argument writeString is the bounded one with no bound.

Refs sofa-buffers/generator#656
The two-argument writeString delegated to the bounded one with
Integer.MAX_VALUE, which cost the generator's java bench row 44 Ir per
encode on schemas whose strings carry no maxlen (17022 -> 17066). Both
public writes now share the two private tails (ASCII header + bulk copy,
measured header + UTF-8 loop) and only the bounded one compares: 16985
Ir/op for the same unbounded workload.

Refs sofa-buffers/generator#656
@Andste82
Andste82 merged commit 6d783e3 into main Oct 9, 2026
4 checks passed
@Andste82
Andste82 deleted the feat/encode-bounds-656 branch October 9, 2026 19:33
Andste82 added a commit to sofa-buffers/generator that referenced this pull request Oct 10, 2026
serialize now refuses a value past its schema bound with the corelib's
argument category, SofabException(SofabError.ARGUMENT), before the field
is written; encode() wraps it as it wraps every IOException, and no bytes
are returned. Each guard is a per-field comparison against the schema
literal, emitted on the write it protects (an omitted default field costs
nothing):

- string / array<string> element over maxlen (UTF-8 bytes): the bound
  rides the corelib call, OStream.writeString(id, s, maxlen), which
  compares inside the measuring pass it already runs -- no second pass,
  no allocation;
- blob / array<blob> element over maxlen: a length guard;
- array over count, at every nesting level (row guards on nested rows,
  wrapper arrays on the list the loop runs over);
- u8/u16/u32/i8/i16/i32, enum and bitfield held in a long: the same
  width comparison the decode store emits (widthTest, shared). 64-bit
  kinds and primitive-array elements (held in their declared width's own
  primitive) need none.

Union options carry the same guards. serialize/encode/encodeTo document
the refusal. SofabError joins the qualifier list (a field of that name
is escaped). The conformance suite writes its over-maxlen decode input by
hand, since the encoder no longer produces it; check_encode_bounds:
KNOWN_GAP[java] is empty, 22/22 cases pass, nothing storage-blocked.

Needs sofa-buffers/corelib-java#127 (writeString(id, text, maxlen)).

Measured (tests/bench/run.sh --rows java, Ir/op, before = corelib main
50a84b3, after = corelib-java#127):
  encode 17022 -> 17363 (+2.00%), decode 31730 -> 31723 (-0.02%).
The encode increase is the guards that run: vehicle_telemetry fills ~50
bounded values per message (16 scalars: 86 Ir, 21 array/blob lengths:
124 Ir, 11 strings: 35 Ir, each measured by removing that class of
guard), about 5-6 Ir per guard. One-operation width tests ((x & ~0xFFL)
!= 0, (byte) x != x) measured -32 Ir and were not taken, to keep the
decode side's comparison shape; a shorter throw site measured no change.

The serialize of every struct and union class documents the refusal
(@throws SofabException with ARGUMENT) whenever it can refuse, including
through a nested struct, union or struct/union array element.

Refs #656
Andste82 added a commit to sofa-buffers/generator that referenced this pull request Oct 10, 2026
serialize now refuses a value past its schema bound with the corelib's
argument category, SofabException(SofabError.ARGUMENT), before the field
is written; encode() wraps it as it wraps every IOException, and no bytes
are returned. Each guard is a per-field comparison against the schema
literal, emitted on the write it protects (an omitted default field costs
nothing):

- string / array<string> element over maxlen (UTF-8 bytes): the bound
  rides the corelib call, OStream.writeString(id, s, maxlen), which
  compares inside the measuring pass it already runs -- no second pass,
  no allocation;
- blob / array<blob> element over maxlen: a length guard;
- array over count, at every nesting level (row guards on nested rows,
  wrapper arrays on the list the loop runs over);
- u8/u16/u32/i8/i16/i32, enum and bitfield held in a long: the same
  width comparison the decode store emits (widthTest, shared). 64-bit
  kinds and primitive-array elements (held in their declared width's own
  primitive) need none.

Union options carry the same guards. serialize/encode/encodeTo document
the refusal. SofabError joins the qualifier list (a field of that name
is escaped). The conformance suite writes its over-maxlen decode input by
hand, since the encoder no longer produces it; check_encode_bounds:
KNOWN_GAP[java] is empty, 22/22 cases pass, nothing storage-blocked.

Needs sofa-buffers/corelib-java#127 (writeString(id, text, maxlen)).

Measured (tests/bench/run.sh --rows java, Ir/op, before = corelib main
50a84b3, after = corelib-java#127):
  encode 17022 -> 17363 (+2.00%), decode 31730 -> 31723 (-0.02%).
The encode increase is the guards that run: vehicle_telemetry fills ~50
bounded values per message (16 scalars: 86 Ir, 21 array/blob lengths:
124 Ir, 11 strings: 35 Ir, each measured by removing that class of
guard), about 5-6 Ir per guard. One-operation width tests ((x & ~0xFFL)
!= 0, (byte) x != x) measured -32 Ir and were not taken, to keep the
decode side's comparison shape; a shorter throw site measured no change.

The serialize of every struct and union class documents the refusal
(@throws SofabException with ARGUMENT) whenever it can refuse, including
through a nested struct, union or struct/union array element.

Refs #656
Andste82 added a commit to sofa-buffers/generator that referenced this pull request Oct 10, 2026
serialize now refuses a value past its schema bound with the corelib's
argument category, SofabException(SofabError.ARGUMENT), before the field
is written; encode() wraps it as it wraps every IOException, and no bytes
are returned. Each guard is a per-field comparison against the schema
literal, emitted on the write it protects (an omitted default field costs
nothing):

- string / array<string> element over maxlen (UTF-8 bytes): the bound
  rides the corelib call, OStream.writeString(id, s, maxlen), which
  compares inside the measuring pass it already runs -- no second pass,
  no allocation;
- blob / array<blob> element over maxlen: a length guard;
- array over count, at every nesting level (row guards on nested rows,
  wrapper arrays on the list the loop runs over);
- u8/u16/u32/i8/i16/i32, enum and bitfield held in a long: the same
  width comparison the decode store emits (widthTest, shared). 64-bit
  kinds and primitive-array elements (held in their declared width's own
  primitive) need none.

Union options carry the same guards. serialize/encode/encodeTo document
the refusal. SofabError joins the qualifier list (a field of that name
is escaped). The conformance suite writes its over-maxlen decode input by
hand, since the encoder no longer produces it; check_encode_bounds:
KNOWN_GAP[java] is empty, 22/22 cases pass, nothing storage-blocked.

Needs sofa-buffers/corelib-java#127 (writeString(id, text, maxlen)).

Measured (tests/bench/run.sh --rows java, Ir/op, before = corelib main
50a84b3, after = corelib-java#127):
  encode 17022 -> 17363 (+2.00%), decode 31730 -> 31723 (-0.02%).
The encode increase is the guards that run: vehicle_telemetry fills ~50
bounded values per message (16 scalars: 86 Ir, 21 array/blob lengths:
124 Ir, 11 strings: 35 Ir, each measured by removing that class of
guard), about 5-6 Ir per guard. One-operation width tests ((x & ~0xFFL)
!= 0, (byte) x != x) measured -32 Ir and were not taken, to keep the
decode side's comparison shape; a shorter throw site measured no change.

The serialize of every struct and union class documents the refusal
(@throws SofabException with ARGUMENT) whenever it can refuse, including
through a nested struct, union or struct/union array element.

Refs #656
Andste82 added a commit to sofa-buffers/generator that referenced this pull request Oct 10, 2026
serialize now refuses a value past its schema bound with the corelib's
argument category, SofabException(SofabError.ARGUMENT), before the field
is written; encode() wraps it as it wraps every IOException, and no bytes
are returned. Each guard is a per-field comparison against the schema
literal, emitted on the write it protects (an omitted default field costs
nothing):

- string / array<string> element over maxlen (UTF-8 bytes): the bound
  rides the corelib call, OStream.writeString(id, s, maxlen), which
  compares inside the measuring pass it already runs -- no second pass,
  no allocation;
- blob / array<blob> element over maxlen: a length guard;
- array over count, at every nesting level (row guards on nested rows,
  wrapper arrays on the list the loop runs over);
- u8/u16/u32/i8/i16/i32, enum and bitfield held in a long: the same
  width comparison the decode store emits (widthTest, shared). 64-bit
  kinds and primitive-array elements (held in their declared width's own
  primitive) need none.

Union options carry the same guards. serialize/encode/encodeTo document
the refusal. SofabError joins the qualifier list (a field of that name
is escaped). The conformance suite writes its over-maxlen decode input by
hand, since the encoder no longer produces it; check_encode_bounds:
KNOWN_GAP[java] is empty, 22/22 cases pass, nothing storage-blocked.

Needs sofa-buffers/corelib-java#127 (writeString(id, text, maxlen)).

Measured (tests/bench/run.sh --rows java, Ir/op, before = corelib main
50a84b3, after = corelib-java#127):
  encode 17022 -> 17363 (+2.00%), decode 31730 -> 31723 (-0.02%).
The encode increase is the guards that run: vehicle_telemetry fills ~50
bounded values per message (16 scalars: 86 Ir, 21 array/blob lengths:
124 Ir, 11 strings: 35 Ir, each measured by removing that class of
guard), about 5-6 Ir per guard. One-operation width tests ((x & ~0xFFL)
!= 0, (byte) x != x) measured -32 Ir and were not taken, to keep the
decode side's comparison shape; a shorter throw site measured no change.

The serialize of every struct and union class documents the refusal
(@throws SofabException with ARGUMENT) whenever it can refuse, including
through a nested struct, union or struct/union array element.

Refs #656
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