Skip to content

fix(object): compare a sized blob against its default image, not the empty blob - #192

Merged
Andste82 merged 3 commits into
mainfrom
fix/sized-blob-default-191
Oct 3, 2026
Merged

Andste82 merged 3 commits into
mainfrom
fix/sized-blob-default-191

Conversation

@Andste82

@Andste82 Andste82 commented Oct 3, 2026

Copy link
Copy Markdown
Member

Fixes #191

Problem

_field_is_default (src/object.c) hard-coded the default of a sized blob as "used length == 0" and ignored the default image. With a non-empty declared default (example: {2, "Hi"}):

  • an object left at its default encoded to 02 13 48 69 instead of nothing;
  • an explicit empty blob was treated as the default and omitted instead of being written as 02 03, so it did not round-trip.

MESSAGE_SPEC.md §2: "The encoder MUST emit a field iff its value ≠ its default"; and "absent → reconstructed as the declared default (which may be non-empty [...]); explicit empty → the empty collection, overriding that default."

Fix

  • The sized-blob branch is folded into the sized-array branch: a blob is an array of one-byte elements there. Used length first, then the used prefix against the default image.
  • Without a default image the logical default is still the empty blob, so blobs without a default (and with an empty default) behave exactly as before.
  • Bytes past the used length are still never compared.
  • No new heap, one branch instead of two, still guarded by FIXLEN || ARRAY support.

Tests

New in test/c/test_object.c:

  • test_object_blob_sized_default_nonempty: default omitted; same-length different bytes and a prefix are written; absent decodes to the default; written values round-trip.
  • test_object_blob_sized_default_explicit_empty: explicit empty written as 02 03, decodes back as empty.
  • test_object_blob_sized_no_default_image_unchanged: no image / empty image, empty is omitted.

Before the fix (main + tests): 83 Tests 2 Failures

test_object_blob_sized_default_nonempty:FAIL: Expected 0 Was 4. a sized blob equal to its non-empty default must be omitted
test_object_blob_sized_default_explicit_empty:FAIL: Expected 2 Was 0. an explicit empty blob over a non-empty default must be written

After: 83 Tests 0 Failures; full ctest (full config, C++ on) 5/5 passed; ASan+UBSan build (SOFAB_ENABLE_FUZZ=ON, GCC, Debug) 5/5 passed. The minimal configuration and builds with SOFAB_DISABLE_FIXLEN_SUPPORT / SOFAB_DISABLE_ARRAY_SUPPORT compile.

Footprint

object.c .text, full configuration, -Os: armv6-m 1678 -> 1656 B (-22), armv7e-m+fp.dp 1694 -> 1660 B (-34). .data/.bss unchanged (0).

Sibling kinds

Checked, no change needed: sized strings/char arrays compare with strncmp against the default image; sized arrays of every element kind (also inside structs, which recurse through the same function) already used the length-first branch; sized wrapper holders carry no default image by design (default is the empty array).

Generator verification

Using the generator's shared driver tests/conformance/lib/check_empty_vs_absent.py (generator#645 branch, C leg only): with an unpatched corelib it fails ({} writes 7a 13 48 69, explicit empty bl writes nothing). The generated default image currently sets .bl = {0x48, 0x69} but not .bl__len (arrays do get .x__len), so the image's blob length is 0. With only this fix the driver still reports {} as non-default; with .bl__len = 2 added to the generated image by hand, the driver passes on both decode and streamdecode with this fix and fails identically on main. The generator therefore needs a one-line follow-up (emit .bl__len in the blob default image); this PR does not regress the current output.

…empty blob

_field_is_default hard-coded a sized blob's default as used_len == 0 and
never read the default image. A blob with a non-empty default therefore
wrote the default when left untouched, and dropped an explicit empty blob
as if it were the default (MESSAGE_SPEC section 2).

Handle the blob in the sized-array branch (a blob is an array of one-byte
elements there): length first, then the used prefix against the image;
no image keeps the empty blob as the default. One branch instead of two,
object.c is 22 B (armv6-m) / 34 B (armv7e-m) smaller.

Fixes #191

@Andste82 Andste82 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed against #191. The diagnosis and the direction are right -- folding the blob into the sized-array branch is the correct shape, the spec reading matches MESSAGE_SPEC §2, and I reproduced 83 Tests 0 Failures plus the 131-vector runner on this branch. One real finding on footprint, and three smaller ones; all inline, all addressed in a follow-up commit I pushed to this branch.

Footprint, the finding. The PR measures only the full configuration. With SOFAB_DISABLE_ARRAY_SUPPORT the whole width != 0 block used to be #if-ed out and the blob took the two-line branch; now the generic block compiles in, including a variable division that pulls __aeabi_uidiv on a core without one:

_field_is_default   208 -> 314 B   (+106)
sofab_object_init   268 -> 272 B     (+4)

That is the row README:778 publishes as the per-switch delta for SOFAB_DISABLE_ARRAY_SUPPORT, so the table needs regenerating either way.

Measured A/B, src/object.c .text, -Os -DNDEBUG (CMake Release -- what tools/footprint.sh measures), fixed being the follow-up commit:

Config Arch main this PR fixed
full ARMv6-m 1664 1640 (-24) 1640
full ARMv7e-m+fp.dp 1680 1644 (-36) 1644
DISABLE_ARRAY ARMv6-m 1268 1372 (+104) 1320 (+52)
DISABLE_ARRAY ARMv7e-m+fp.dp 1268 1356 (+88) 1328 (+60)
DISABLE_FIXLEN ARMv6-m 1368 1372 (+4) 1372
minimal ARMv6-m 352 352 352

.data/.bss stay 0 in every row. The guard is free in the full configuration and takes back 52 B / 28 B in the array-free one; the remaining ~+52 B is the irreducible price of reading the default image at all, and belongs in the PR description rather than being left out.

Out of scope here, but worth not losing: the generator follow-up this PR's description promises (blob default image emits .bl without .bl__len, while arrays do get .x__len) has no issue filed in the generator repo. Until it does, a non-empty blob default stays silently empty in generated code. Related and unverified: the C++ leg omits defaults through per-field comparisons in generated code, not through _field_is_default, so the same bug class may well still be live for FixedBytes with a non-empty default -- the PR's generator verification covered the C leg only.

Comment thread src/object.c Outdated
* logical default is the empty value. The buffer past the used length
* is indeterminate and never influences the decision. */
size_t esz = field->type == SOFAB_OBJECT_FIELDTYPE_BLOB
? 1 : field->element_size;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 1 is load-bearing in a way nothing here records, and the ternary leaves footprint on the table.

For a BLOB the descriptor macros put sizeof(buffer) & 0xF in element_size -- the capacity, not an element width (SOFAB_OBJECT_FIELD_BLOB_SIZED in object.h, same expression as SOFAB_OBJECT_FIELD). At a capacity of 16 that is 0, so field->size / esz on the next line would be a division by zero if a blob ever reached it with element_size. The ternary is what makes that unreachable, and a future "simplification" that drops it reintroduces UB silently. It deserves to say so.

Second, as a ternary this costs the array-free build: with SOFAB_DISABLE_ARRAY_SUPPORT the blob is the only kind that reaches this block, so an #if folds esz to a constant and GCC emits neither the division (__aeabi_uidiv, no hardware divide on Cortex-M0) nor the multiply below. 52 B on ARMv6-m, 28 B on ARMv7e-m, and provably 0 B in the full configuration. object.c:1069 already uses exactly this #if shape in the decode path for the same reason, so it is the house pattern:

size_t esz = 1;
#if !defined(SOFAB_DISABLE_ARRAY_SUPPORT)
if (field->type != SOFAB_OBJECT_FIELDTYPE_BLOB)
    esz = field->element_size;
#endif

Fixed in the follow-up commit, with the capacity-16 trap written into the comment and a test_object_blob_sized_default_capacity_16 regression test that would crash rather than quietly pass if the 1 ever goes away. (Verified by hand before the test existed: capacity 16, default {3, "abc"} -- at default omitted, explicit empty 02 03, full 16 bytes round-trip.)

Comment thread test/c/test_object.c
TEST_ASSERT_EQUAL_size_t(sizeof(exp_h), n);
TEST_ASSERT_EQUAL_UINT8_ARRAY(exp_h, buf, n);

/* 4. absent decodes to the default; a written value round-trips. */

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Step numbering jumps 2. -> 4.; the sibling test_object_blob_sized above does have a 3., so this reads as a dropped step rather than a style. The prefix case under 2. really is its own step (same bytes, shorter length -- it passes for a different reason than "same length, other bytes"), so the follow-up commit splits it into 2. and 3. instead of renumbering this line down.

Comment thread test/c/test_object.c Outdated
&_info_defaults_blobsized);

static size_t blobsized_def_encode (const sofab_object_descr_t *info,
const blobsized_t *in, uint8_t *buf, size_t n)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, and the follow-up commit changes it: blobsized_def_encode/_decode are typed to const blobsized_t *, but they are entirely descriptor-driven -- sofab_object_encode itself takes the object as const void *. Typing them void * lets a second blob layout (the capacity-16 case) share them instead of needing a duplicate pair.

Andste82 and others added 2 commits October 3, 2026 04:45
…6 trap

Review of #192. The sized-blob/array merge left two things on the table.

esz was a ternary, so an array-free build still emitted the capacity
division -- __aeabi_uidiv on a core without a divide instruction -- for the
one field kind that can reach the block there. Behind #if the constant
folds and neither the division nor the multiply is generated: object.c
.text goes 1372 -> 1320 B (armv6-m) and 1356 -> 1328 B (armv7e-m) with
SOFAB_DISABLE_ARRAY_SUPPORT, and is unchanged in the full configuration.
Same #if shape the decode path at object.c:1069 already uses.

The 1 is also load-bearing against a division by zero: for a BLOB the
descriptor macros record sizeof(buffer) & 0xF in element_size -- the
capacity, not an element width -- which is 0 at a capacity of 16. Nothing
said so and no test covered it, so a later "simplification" could reach
for element_size and reintroduce UB silently. Now the comment states it
and test_object_blob_sized_default_capacity_16 exercises it: at default
omitted, explicit empty written as 02 03, full 16 bytes round-tripped.

Also: split the prefix case in test_object_blob_sized_default_nonempty
into its own step (the numbering jumped 2. -> 4.), and type the two
blobsized_def_* helpers on void * -- they are descriptor-driven, like
sofab_object_encode itself -- so both blob layouts share them.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
object.c moved, so the published numbers did. Regenerated with
tools/footprint.sh against origin/main as the baseline, and reproduced
byte-identically by a second run.

Full configuration .text: ARMv6-m 3960 -> 3938 B, ARMv7e-m+fp.dp
3960 -> 3926, RV32IMC 5080 -> 5038, atmega8 7550 -> 7546. The two Minimal
rows do not move -- the branch this fix touches is already compiled out
there. .data/.bss stay 0 everywhere.

In the per-switch table SOFAB_DISABLE_ARRAY_SUPPORT goes the other way,
2918 -> 2972 B: the blob now reads its default image in that configuration
too, which is the point of the fix and is not free. SOFAB_OBJECT_DESCR_SMALL
(+16 B) now sorts below SOFAB_ENABLE_SKIP_COUNTER (+18 B), so the two rows
swap. The prose absolute for SOFAB_DISABLE_LAZY_SEQ_SUPPORT follows its row
to 3662 B; every delta the hold-back section quotes is a difference between
two builds that both carry this change, so those are unchanged.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@Andste82

Andste82 commented Oct 3, 2026

Copy link
Copy Markdown
Member Author

Pushed as 95b7e22 (code + test) and a7a4f0c (re-measured tables).

One correction to my review above. I first reported the #if as also saving 8 B in the full configuration. That was an artifact: with live assert()s, shifting __LINE__ values changes the encoded immediates, and I was reading a comment-length difference as a code-size difference. Under -DNDEBUG -- which is what CMake Release and tools/footprint.sh actually build -- the ternary and the #if are byte-identical in the full configuration. The guard's value is entirely in the array-free build. Everything else in the review stands; object.c is identical between origin/main and the stale local main I first measured against, and the ARMv6-m per-switch table is too.

Final library-wide A/B, tools/footprint.sh, baseline origin/main (2c99398), .text in bytes. Both legs reproduced byte-identically by a second independent run:

Configuration Arch base with fix delta
full ARMv6-m 3960 3938 −22
full ARMv7e-m+fp.dp 3960 3926 −34
full RV32IMC 5080 5038 −42
full atmega8 7550 7546 −4
full, strict UTF-8 ARMv6-m 4206 4184 −22
minimal (object API on) all four — — ±0
minimal, no object.c all four — — ±0

Per-switch, ARMv6-m: SOFAB_DISABLE_ARRAY_SUPPORT 2918 → 2972 B (+54), SOFAB_OBJECT_DESCR_SMALL +22 → +16 B and so swaps rows with SOFAB_ENABLE_SKIP_COUNTER; every other row moves with the −22 B baseline. .data/.bss stay 0 in every row. The remaining +54 B in the array-free build is the price of reading the default image there at all -- it is what the fix is for, and the README now states it rather than leaving it out.

Verified on the pushed head: 84 Tests 0 Failures (object), 97/114 for ostream/istream, 131 vectors / 1033 checks, ctest 5/5 with C++ on. The minimal configuration and library-only builds with SOFAB_DISABLE_FIXLEN_SUPPORT / SOFAB_DISABLE_ARRAY_SUPPORT compile for armv6-m, armv7e-m and rv32imc.

Still open, and not for this PR: the generator-side .bl__len follow-up has no issue filed, and the C++ leg's FixedBytes default omission is unverified.

@Andste82
Andste82 merged commit c72a64c into main Oct 3, 2026
26 checks passed
Andste82 added a commit that referenced this pull request Oct 3, 2026
…6 trap

Review of #192. The sized-blob/array merge left two things on the table.

esz was a ternary, so an array-free build still emitted the capacity
division -- __aeabi_uidiv on a core without a divide instruction -- for the
one field kind that can reach the block there. Behind #if the constant
folds and neither the division nor the multiply is generated: object.c
.text goes 1372 -> 1320 B (armv6-m) and 1356 -> 1328 B (armv7e-m) with
SOFAB_DISABLE_ARRAY_SUPPORT, and is unchanged in the full configuration.
Same #if shape the decode path at object.c:1069 already uses.

The 1 is also load-bearing against a division by zero: for a BLOB the
descriptor macros record sizeof(buffer) & 0xF in element_size -- the
capacity, not an element width -- which is 0 at a capacity of 16. Nothing
said so and no test covered it, so a later "simplification" could reach
for element_size and reintroduce UB silently. Now the comment states it
and test_object_blob_sized_default_capacity_16 exercises it: at default
omitted, explicit empty written as 02 03, full 16 bytes round-tripped.

Also: split the prefix case in test_object_blob_sized_default_nonempty
into its own step (the numbering jumped 2. -> 4.), and type the two
blobsized_def_* helpers on void * -- they are descriptor-driven, like
sofab_object_encode itself -- so both blob layouts share them.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@Andste82
Andste82 deleted the fix/sized-blob-default-191 branch October 3, 2026 04:56
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.

object: a sized blob with a non-empty default is compared against the empty blob, not its default

1 participant