billvaglienti / ProtoGen

Communications protocol generation software
MIT License
30 stars 16 forks source link

Bitfields don't always respect default values #65

Closed jefffisher closed 6 years ago

jefffisher commented 7 years ago

I just realized that default values for bitfields don't always make sense if adding a new bitfield to the packet doesn't increase the size. Example:

<Packet name="test" ID="0">
    <Data name="Field0" inMemoryType="bitfield1" />
    <Data name="Field1" inMemoryType="bitfield1" default="1" />
</Packet>

In this case, if an older version of the protocol that only knows about Field0 sends a packet with Field0 = 1, the packet data will consist of one byte, 0x80. The problem is that the newer protocol version will infer from the one-byte size that it has a valid Field1 value and read it out as zero, overwriting the default of 1.

My suggestion would be to throw a warning for any bitfields with default values. One could argue that the default could be forced to zero, but I think there may be valid usecases for non-zero defaults.

In addition if you have multiple default bitfields in the same byte the size check is unnecessarily repeated.

billvaglienti commented 6 years ago

Fixed in 2.9.a