Skip to content

[C++] Align serialized scalars by wire size#9184

Open
carrerasdarren-cell wants to merge 1 commit into
google:masterfrom
carrerasdarren-cell:fix-scalar-wire-alignment
Open

[C++] Align serialized scalars by wire size#9184
carrerasdarren-cell wants to merge 1 commit into
google:masterfrom
carrerasdarren-cell:fix-scalar-wire-alignment

Conversation

@carrerasdarren-cell

@carrerasdarren-cell carrerasdarren-cell commented Jul 22, 2026

Copy link
Copy Markdown

Fixes #9182.

FlatBuffers aligns serialized scalars to their size, but several templated
builder paths used the host compiler's ABI alignment instead. On targets such
as m68k, where uint32_t is 4 bytes with 2-byte ABI alignment, this can make
PushElement insert padding between a string's length prefix and payload.
Scalar and offset vectors have the same wire-alignment mismatch.

This change:

  • uses the scalar width when pre-aligning scalar length prefixes;
  • uses wire width for scalar, Offset, and Offset64 vector elements while
    retaining AlignOf<T>() for generated structs;
  • applies the same rule to uninitialized scalar vectors; and
  • adds architecture-independent regressions with deliberately under-aligned
    32-bit and 64-bit scalar types.

Validation:

  • CMake flattests: all tests passed.
  • Exact statically linked m68k reproducer under qemu-m68k: stock master
    inserts two zero bytes and fails; patched code emits the canonical bytes and
    passes.

Use scalar widths for vector and length-prefix wire alignment while preserving compiler alignment for generated structs. This prevents host ABI alignment rules from inserting padding into serialized buffers.

Add portable regression coverage with deliberately under-aligned scalar types.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C++] FlatBufferBuilder writes unreadable buffers on targets where alignof(scalar) < sizeof(scalar)

1 participant