Skip to content

Fixed size arrays of structs produce incorrect padding [C#, master] #5484

@develmi

Description

@develmi

padding_bug_prepro.zip

I did my best to narrow this problem down. It looks like flatc.exe produces wrong padding for fixed arrays of structs in c#.
Fixed size array of longs is generated correctly:

    builder.Prep(8, 80);
    builder.PutLong(Int2);
    for (int _idx0 = 8; _idx0 > 0; _idx0--) {
      builder.PutLong(IntStruct[_idx0-1]);
    }
    builder.Pad(4); // <- correctly placed padding
    builder.PutInt(Int1);

But in the case of array of structs:

    builder.Prep(8, 80);
    builder.PutLong(Int2);
    builder.Pad(4); // <- incorrectly placed padding, unnecessary padding of 4 bytes
    for (int _idx0 = 8; _idx0 > 0; _idx0--) {
      builder.Prep(8, 8); // same thing, results in one more alignment, producing extra 4 bytes
      builder.PutLong(int_struct_LongField[_idx0-1]);
    }
    builder.PutInt(Int1);

Both schemes should result in exactly the same buffer layout: there is no difference in laying out array of longs and array of structs that contain a single long.
As you can see in the attachment, incorrect padding leads to everything to be offset by a few bytes

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions