Skip to content

Round struct sizes up to their alignment - #2

Merged
agerveshi-unity merged 3 commits into
unity-mainfrom
struct-trailing-padding
Aug 11, 2026
Merged

agerveshi-unity merged 3 commits into
unity-mainfrom
struct-trailing-padding

Conversation

@agerveshi-unity

Copy link
Copy Markdown

The FlatBuffers spec requires a struct's size to be a multiple of its alignment, so consecutive struct vector elements stay aligned. flatc does this via PadLastField; FlatSharp pads between fields but never rounds up the total. A struct whose last field is narrower than its alignment serializes short. This is fine at the end of a table where the vtable absorbs it, but in an array of structs every element after the first lands at the wrong offset.

The fix is one line after the field loop in ValueStructTypeModel and StructTypeModel. inlineSize feeds PhysicalLayout, the serialize/parse slices, MaxInlineSize and the emitted [StructLayout] size, so it propagates from there.

Tests

12 struct instances across 5 schemas change size, updated the matching test expectations and schema doc comments. The two PoorlyAligned tests asserted a size (5) is not a multiple of the alignment (4) error; at 8 bytes that no longer throws, so both became round-trips.

All tests passed locally (excluding tests without a Mac arm64 target).

csproj

Modified for Unity compatibility, do not upstream.

@joejo-unity joejo-unity left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The fuzzing job may need to have a longer timeout? Otherwise, LGTM.

@agerveshi-unity
agerveshi-unity merged commit d818676 into unity-main Aug 11, 2026
4 of 5 checks passed
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.

2 participants