Repository navigation
feat(encode): rejectArgument hook; static storage clamps at a UTF-8 boundary - #153
Merged
Merged
Conversation
…oundary Generated serialize() now refuses a value past a bound only the schema knows (a string or blob longer than its maxlen, an array with more elements than its count). The corelib holds no bound: the new OStreamImpl::rejectArgument() only latches InvalidArgument, so ok() and error() report the refusal like the corelib's own §6.2/§6.4 refusals and no byte of the message is returned as a success. It is the encode-side counterpart of IStreamImpl::invalidate(). The static storage keeps clamping at assignment, now as a documented contract, with two fixes: - FixedString::assign never cuts inside a UTF-8 character: the cut moves back to the start of the character that would not fit whole (detail::utf8Prefix, one copy for every capacity). Before, "xxxé" into FixedString<4> kept "xxx\xC3", which the strict-UTF-8 encoder then refused. - InlineVector::push_back past N drops the new value and keeps the held ones. It used to overwrite the last element (it went through emplace_back, whose last-slot reuse is the decode binder's OOB guard and stays as it is). Tests: encodeBoundRefusalAndAssignmentClamp in test_roundtrip. Refs sofa-buffers/generator#656
Andste82
force-pushed
the
feat/encode-bounds-656
branch
from
October 9, 2026 10:47
42ff32f to
e96b937
Compare
Andste82
added a commit
to sofa-buffers/generator
that referenced
this pull request
Oct 10, 2026
A string or blob longer than its maxlen, or an array with more elements than its count (nested rows, array<string>/array<blob> elements and struct-array counts included), is now refused by the generated serialize() wherever the member is a growable std::string / std::vector: a per-field guard returns through the corelib's OStreamImpl::rejectArgument(), which latches InvalidArgument, so encode() returns no bytes, encodeTo() returns 0 and a caller's stream reports !ok(). The corelib is handed no bound. - Static storage (FixedString / FixedBytes / InlineVector, the allow_dynamic: false default of corelib: c-cpp and the opt-in of corelib: cpp) carries the bound as its capacity and clamps on assignment: no guard, a documented contract (CLAMP_CONTRACT). A level that falls back to std::vector (a counted array of maxlen-less strings) keeps its guard. - Scalars, enums and bitfields are stored in their declared fixed-width type, so nothing is emitted for them; the driver's STORAGE_BLOCKED reason now says the type guarantees the width. - corelib: c-cpp (footprint) writes each check as `if (sofab::ENCODE_BOUNDS && ...)`; SOFAB_DISABLE_ENCODE_BOUNDS compiles them all out. corelib: cpp has no opt-out. - The harness encode verb exits non-zero on a refusal (an empty encode() from a non-default message), and appends string elements with push_back so an InlineVector drops, not overwrites, past its capacity. - Generated docs of encode()/encodeTo()/serialize() say what is refused and what clamps; docs/generator/cpp.md documents both behaviours and the opt-out. check_encode_bounds.py: KNOWN_GAP is empty for cpp, cpp-static, c-cpp and c-cpp-static; the static variants hold all 11 string/blob/array cells to the clamp contract. Needs sofa-buffers/corelib-cpp#153 (rejectArgument, UTF-8-safe FixedString clamp, InlineVector::push_back drops past N) and sofa-buffers/corelib-c-cpp#194 (the same plus sofab::ENCODE_BOUNDS). Measured (tests/bench/run.sh, 3f85fbf + corelib main vs this commit + the corelib branches): Ir/op encode / decode cpp-cpp 11904 -> 11546 (-3.0%) / 28116 -> 28254 (+0.5%) cpp-cpp-static 10588 -> 10588 / 21322 -> 21325 cpp-cpp-unbounded 8496 -> 8490 / 21663 -> 21663 cpp-c-cpp 32983 -> 32983 / 40862 -> 40862 cpp-c-cpp-dyn 33335 -> 33437 (+0.3%) / 52167 -> 52164 .text (checks on / -DSOFAB_DISABLE_ENCODE_BOUNDS), .data/.bss unchanged cpp-c-cpp ARMv6-m 8694 -> 8694 / 8694 cpp-c-cpp ARMv7-m 8296 -> 8296 / 8296 cpp-c-cpp-dyn ARMv6-m 15632 -> 15834 / 15632 (+202 B, 18 guards) cpp-c-cpp-dyn ARMv7-m 14370 -> 14602 / 14370 (+232 B) rejectArgument is cold and out of line in both corelibs: inlined at the 18 guard sites it cost cpp-cpp +4.4% encode (GCC stopped inlining writeIntArray<uint16_t> into serialize) and the -dyn row 300 B. Refs #656
Andste82
added a commit
to sofa-buffers/generator
that referenced
this pull request
Oct 10, 2026
A string or blob longer than its maxlen, or an array with more elements than its count (nested rows, array<string>/array<blob> elements and struct-array counts included), is now refused by the generated serialize() wherever the member is a growable std::string / std::vector: a per-field guard returns through the corelib's OStreamImpl::rejectArgument(), which latches InvalidArgument, so encode() returns no bytes, encodeTo() returns 0 and a caller's stream reports !ok(). The corelib is handed no bound. - Static storage (FixedString / FixedBytes / InlineVector, the allow_dynamic: false default of corelib: c-cpp and the opt-in of corelib: cpp) carries the bound as its capacity and clamps on assignment: no guard, a documented contract (CLAMP_CONTRACT). A level that falls back to std::vector (a counted array of maxlen-less strings) keeps its guard. - Scalars, enums and bitfields are stored in their declared fixed-width type, so nothing is emitted for them; the driver's STORAGE_BLOCKED reason now says the type guarantees the width. - corelib: c-cpp (footprint) writes each check as `if (sofab::ENCODE_BOUNDS && ...)`; SOFAB_DISABLE_ENCODE_BOUNDS compiles them all out. corelib: cpp has no opt-out. - The harness encode verb exits non-zero on a refusal (an empty encode() from a non-default message), and appends string elements with push_back so an InlineVector drops, not overwrites, past its capacity. - Generated docs of encode()/encodeTo()/serialize() say what is refused and what clamps; docs/generator/cpp.md documents both behaviours and the opt-out. check_encode_bounds.py: KNOWN_GAP is empty for cpp, cpp-static, c-cpp and c-cpp-static; the static variants hold all 11 string/blob/array cells to the clamp contract. Needs sofa-buffers/corelib-cpp#153 (rejectArgument, UTF-8-safe FixedString clamp, InlineVector::push_back drops past N) and sofa-buffers/corelib-c-cpp#194 (the same plus sofab::ENCODE_BOUNDS). Measured (tests/bench/run.sh, 3f85fbf + corelib main vs this commit + the corelib branches): Ir/op encode / decode cpp-cpp 11904 -> 11546 (-3.0%) / 28116 -> 28254 (+0.5%) cpp-cpp-static 10588 -> 10588 / 21322 -> 21325 cpp-cpp-unbounded 8496 -> 8490 / 21663 -> 21663 cpp-c-cpp 32983 -> 32983 / 40862 -> 40862 cpp-c-cpp-dyn 33335 -> 33437 (+0.3%) / 52167 -> 52164 .text (checks on / -DSOFAB_DISABLE_ENCODE_BOUNDS), .data/.bss unchanged cpp-c-cpp ARMv6-m 8694 -> 8694 / 8694 cpp-c-cpp ARMv7-m 8296 -> 8296 / 8296 cpp-c-cpp-dyn ARMv6-m 15632 -> 15834 / 15632 (+202 B, 18 guards) cpp-c-cpp-dyn ARMv7-m 14370 -> 14602 / 14370 (+232 B) rejectArgument is cold and out of line in both corelibs: inlined at the 18 guard sites it cost cpp-cpp +4.4% encode (GCC stopped inlining writeIntArray<uint16_t> into serialize) and the -dyn row 300 B. Refs #656
Andste82
added a commit
to sofa-buffers/generator
that referenced
this pull request
Oct 10, 2026
A string or blob longer than its maxlen, or an array with more elements than its count (nested rows, array<string>/array<blob> elements and struct-array counts included), is now refused by the generated serialize() wherever the member is a growable std::string / std::vector: a per-field guard returns through the corelib's OStreamImpl::rejectArgument(), which latches InvalidArgument, so encode() returns no bytes, encodeTo() returns 0 and a caller's stream reports !ok(). The corelib is handed no bound. - Static storage (FixedString / FixedBytes / InlineVector, the allow_dynamic: false default of corelib: c-cpp and the opt-in of corelib: cpp) carries the bound as its capacity and clamps on assignment: no guard, a documented contract (CLAMP_CONTRACT). A level that falls back to std::vector (a counted array of maxlen-less strings) keeps its guard. - Scalars, enums and bitfields are stored in their declared fixed-width type, so nothing is emitted for them; the driver's STORAGE_BLOCKED reason now says the type guarantees the width. - corelib: c-cpp (footprint) writes each check as `if (sofab::ENCODE_BOUNDS && ...)`; SOFAB_DISABLE_ENCODE_BOUNDS compiles them all out. corelib: cpp has no opt-out. - The harness encode verb exits non-zero on a refusal (an empty encode() from a non-default message), and appends string elements with push_back so an InlineVector drops, not overwrites, past its capacity. - Generated docs of encode()/encodeTo()/serialize() say what is refused and what clamps; docs/generator/cpp.md documents both behaviours and the opt-out. check_encode_bounds.py: KNOWN_GAP is empty for cpp, cpp-static, c-cpp and c-cpp-static; the static variants hold all 11 string/blob/array cells to the clamp contract. Needs sofa-buffers/corelib-cpp#153 (rejectArgument, UTF-8-safe FixedString clamp, InlineVector::push_back drops past N) and sofa-buffers/corelib-c-cpp#194 (the same plus sofab::ENCODE_BOUNDS). Measured (tests/bench/run.sh, 3f85fbf + corelib main vs this commit + the corelib branches): Ir/op encode / decode cpp-cpp 11904 -> 11546 (-3.0%) / 28116 -> 28254 (+0.5%) cpp-cpp-static 10588 -> 10588 / 21322 -> 21325 cpp-cpp-unbounded 8496 -> 8490 / 21663 -> 21663 cpp-c-cpp 32983 -> 32983 / 40862 -> 40862 cpp-c-cpp-dyn 33335 -> 33437 (+0.3%) / 52167 -> 52164 .text (checks on / -DSOFAB_DISABLE_ENCODE_BOUNDS), .data/.bss unchanged cpp-c-cpp ARMv6-m 8694 -> 8694 / 8694 cpp-c-cpp ARMv7-m 8296 -> 8296 / 8296 cpp-c-cpp-dyn ARMv6-m 15632 -> 15834 / 15632 (+202 B, 18 guards) cpp-c-cpp-dyn ARMv7-m 14370 -> 14602 / 14370 (+232 B) rejectArgument is cold and out of line in both corelibs: inlined at the 18 guard sites it cost cpp-cpp +4.4% encode (GCC stopped inlining writeIntArray<uint16_t> into serialize) and the -dyn row 300 B. Refs #656
Andste82
added a commit
to sofa-buffers/generator
that referenced
this pull request
Oct 10, 2026
A string or blob longer than its maxlen, or an array with more elements than its count (nested rows, array<string>/array<blob> elements and struct-array counts included), is now refused by the generated serialize() wherever the member is a growable std::string / std::vector: a per-field guard returns through the corelib's OStreamImpl::rejectArgument(), which latches InvalidArgument, so encode() returns no bytes, encodeTo() returns 0 and a caller's stream reports !ok(). The corelib is handed no bound. - Static storage (FixedString / FixedBytes / InlineVector, the allow_dynamic: false default of corelib: c-cpp and the opt-in of corelib: cpp) carries the bound as its capacity and clamps on assignment: no guard, a documented contract (CLAMP_CONTRACT). A level that falls back to std::vector (a counted array of maxlen-less strings) keeps its guard. - Scalars, enums and bitfields are stored in their declared fixed-width type, so nothing is emitted for them; the driver's STORAGE_BLOCKED reason now says the type guarantees the width. - corelib: c-cpp (footprint) writes each check as `if (sofab::ENCODE_BOUNDS && ...)`; SOFAB_DISABLE_ENCODE_BOUNDS compiles them all out. corelib: cpp has no opt-out. - The harness encode verb exits non-zero on a refusal (an empty encode() from a non-default message), and appends string elements with push_back so an InlineVector drops, not overwrites, past its capacity. - Generated docs of encode()/encodeTo()/serialize() say what is refused and what clamps; docs/generator/cpp.md documents both behaviours and the opt-out. check_encode_bounds.py: KNOWN_GAP is empty for cpp, cpp-static, c-cpp and c-cpp-static; the static variants hold all 11 string/blob/array cells to the clamp contract. Needs sofa-buffers/corelib-cpp#153 (rejectArgument, UTF-8-safe FixedString clamp, InlineVector::push_back drops past N) and sofa-buffers/corelib-c-cpp#194 (the same plus sofab::ENCODE_BOUNDS). Measured (tests/bench/run.sh, 3f85fbf + corelib main vs this commit + the corelib branches): Ir/op encode / decode cpp-cpp 11904 -> 11546 (-3.0%) / 28116 -> 28254 (+0.5%) cpp-cpp-static 10588 -> 10588 / 21322 -> 21325 cpp-cpp-unbounded 8496 -> 8490 / 21663 -> 21663 cpp-c-cpp 32983 -> 32983 / 40862 -> 40862 cpp-c-cpp-dyn 33335 -> 33437 (+0.3%) / 52167 -> 52164 .text (checks on / -DSOFAB_DISABLE_ENCODE_BOUNDS), .data/.bss unchanged cpp-c-cpp ARMv6-m 8694 -> 8694 / 8694 cpp-c-cpp ARMv7-m 8296 -> 8296 / 8296 cpp-c-cpp-dyn ARMv6-m 15632 -> 15834 / 15632 (+202 B, 18 guards) cpp-c-cpp-dyn ARMv7-m 14370 -> 14602 / 14370 (+232 B) rejectArgument is cold and out of line in both corelibs: inlined at the 18 guard sites it cost cpp-cpp +4.4% encode (GCC stopped inlining writeIntArray<uint16_t> into serialize) and the -dyn row 300 B. 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
OStreamImpl::rejectArgument()([[gnu::cold, gnu::noinline]]): latchesError::InvalidArgument(first failure wins) and returns aResultcarrying it. Generatedserialize()calls it when a value is past a bound only the schema knows (string/blobmaxlen, arraycount). The corelib is handed no bound; it only records the refusal, sook()/error()report it andencode()returns no bytes as a success. Encode-side counterpart ofIStreamImpl::invalidate().FixedString::assignclamps at a UTF-8 character boundary (detail::utf8Prefix, one non-template copy for everyN). Before:"xxxé"intoFixedString<4>kept"xxx\xC3".InlineVector::push_backpastNdrops the new value and keeps the held ones (it used to overwrite the last element viaemplace_back).emplace_backkeeps its last-slot reuse, which the decode binders rely on.The assignment-time clamp of the static storage (
allow_dynamic: false) is a documented contract (generator#656 decision of 2026-10-07); these two fixes make it a correct one.Tests
encodeBoundRefusalAndAssignmentClampintest/test_roundtrip.cpp: the Result and stream verdict, first-failure-wins againstBufferFull, a refusingserialize(), and the clamp ofFixedString(2-, 3-, 4-byte characters, nothing fits, cut exactly at a boundary),InlineVector(copy and movepush_back, initializer list) andFixedBytes.Coverage (gcovr,
include/sofab/*.hpp, Debug + coverage, as the CI job):Generator impact
Instructions per operation (
tests/bench/run.sh, callgrind, generator main 3f85fbf + corelib main 65755b0 vs. the #656 generator change + this branch):rejectArgumentis cold and out of line on purpose: inlined at the 18 guard sites of the bench schema it pushedserialize()past GCC's inlining budget forwriteIntArray<uint16_t>(12423 Ir/op encode, +4.4%); out of line the encode is cheaper than before the guards (11546). The decode +0.5% is outside the changed code (same TU, inlining noise).