Skip to content

[Dart] Fix struct builder alignment#9119

Open
KirtiRamchandani wants to merge 1 commit into
google:masterfrom
KirtiRamchandani:fix/dart-struct-builder-alignment
Open

[Dart] Fix struct builder alignment#9119
KirtiRamchandani wants to merge 1 commit into
google:masterfrom
KirtiRamchandani:fix/dart-struct-builder-alignment

Conversation

@KirtiRamchandani
Copy link
Copy Markdown

Fixes #9099.

Problem
Dart generated fixed-struct builders write fields one at a time. When an inline struct with a larger alignment follows an unaligned table field, the builder can align only the first scalar write instead of the whole struct.

Root cause
The Dart runtime and generator did not have a struct-preparation step that accounts for the struct's minimum alignment and total byte size before writing its fields.

Solution

  • Add Builder.prepStruct(alignment, size) to pad for a whole inline struct before individual field writes.
  • Emit prepStruct(minalign, bytesize) for generated Dart fixed-struct builders and object builders.
  • Add a regression case covering a ushort table field followed by a Header struct containing a ulong.

Tests run

  • cmake -S . -B build -G Ninja -DFLATBUFFERS_BUILD_TESTS=OFF
  • cmake --build build --target flatc
  • build/flatc.exe --dart --gen-object-api -o dart/test dart/test/bool_structs.fbs
  • git diff --check
  • dart test from dart/

@github-actions github-actions Bot added c++ codegen Involving generating code from schema dart labels Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ codegen Involving generating code from schema dart

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Dart] Struct builders do not prepare full inline struct alignment/size before writing fields

1 participant