Skip to content

Using flatc generated python funciton results in bad reads in flatcc generated code #353

@NReddyBase

Description

@NReddyBase

I have a struct:

struct Struct1 {
  field1: uint8;        // Target mode (0=Disabled, 1=PLL, 2=Sine Table, 3=Fixed Duty)
  field2: uint16;    // Frequency x 100 (for Sine Table mode, e.g., 6005 = 60.05 Hz)
  field3: uint8;          // Amplitude 0-100% (for Sine Table mode)
  field4: uint8;         // Duty cycle 0-100% (for Fixed Duty mode)
  field5: uint32;      // Bit-packed GPIO states (1 = ON, 0 = OFF)
  field6: uint8; // Dump burst capture
  field7: float;
  field8: float;
  field9: uint8;
}

Which compiles into this python function:

def CreateStruct1(builder, field1, field2, field3, field4, field5, field6, field7, field8, field9):
    builder.Prep(4, 28)
    builder.Pad(3)
    builder.PrependUint8(field9)
    builder.PrependFloat32(field8)
    builder.PrependFloat32(field7)
    builder.Pad(3)
    builder.PrependUint8(field6)
    builder.PrependUint32(field5)
    builder.Pad(2)
    builder.PrependUint8(field4)
    builder.PrependUint8(field3)
    builder.PrependUint16(field2)
    builder.Pad(1)
    builder.PrependUint8(field1)
    return builder.Offset()

Which when called like

builder = flatbuffers.Builder(0)
        off = CreateInverterControl(
            builder,
            3,  # target_mode
            6005,  # frequency_x100
            100,  # amplitude
            50,  # duty_cycle
            0xDEADBEEF,  # gpio_setting
            1,  # trigger_burst_dump
            1.234,  # idref
            4.567,  # iqref
            1,  # closed_loop_en
        )
        builder.Finish(off)
        # Create inverer control pre appends 4 unncessary bytes that prevent the inverter from processing the message
        payload = builder.Output()

gives me this message payload (hex)

 04 00 00 00 01 00 00 00 3C 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

But when I use the flatcc generated C code to try and read it results in a bad read:

static inline Inverter_CAN_Struct1_t *Inverter_CAN_Struct1_assign_to_pe(Inverter_CAN_Struct1_t *p,
  uint8_t v0, uint16_t v1, uint8_t v2, uint8_t v3,
  uint32_t v4, uint8_t v5, float v6, float v7, uint8_t v8)
{ p->field1 = v0; flatbuffers_uint16_assign_to_pe(&p->field2, v1); p->field3 = v2; p->field4 = v3;
  flatbuffers_uint32_assign_to_pe(&p->field5, v4); p->field6 = v5; flatbuffers_float_assign_to_pe(&p->field7, v6); flatbuffers_float_assign_to_pe(&p->field8, v7);
  p->field9 = v8;
  return p; }

Any advice or context here?

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