Repository navigation
feat(encode): writeString(id, text, maxlen) refuses past the caller's bound - #127
Merged
Merged
Conversation
… 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
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
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.
Refs sofa-buffers/generator#656
What
OStream.writeString(int id, String text, int maxlen): a string that encodes to more thanmaxlenUTF-8 bytes is refused withSofabException(ARGUMENT)before any byte of the field is written (no lazily held sequence header is committed either), exactly like an unpaired surrogate.maxlenof the field or array element; the library holds no bound of its own.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.maxlenis 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.Why
The generator refuses an over-bound value at encode (ARCHITECTURE section 9.6, generator#656). For a
Stringthe UTF-8 byte length is only known insidewriteString, 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:Measurement
Generator
tests/benchjava row (vehicle_telemetry, Callgrind, encode Ir/op,subtractmethod):writeStringcalls)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.