zserio icon indicating copy to clipboard operation
zserio copied to clipboard

Offsets should not be allowed in expressions

Open mikir opened this issue 8 months ago • 11 comments

Consider the following schema:

struct WrongSchema
{
    uint32 myOffset;
    uint8 optionalValue if myOffset == 4;
myOffset:
   string stringValue;
};

Such schema is not possible to fill by data correctly. Consider how the myOffset will be calculated and what will happen during write and read operation.

  1. Let's say that myOffset will be initialized to 0.
  2. Let's say that optionalValue will be set to 0.
  3. Then myOffset will be set to 4 (4 bytes because of uint32 type + 0 because of optional uint8 which is not there 0 != 4).
  4. Writer will write myOffset == 4, optionalValue = 0, strigValue = "something".
  5. Reader will fail because myOffset == 4 but it should 5 because optional is suddenly there.

We should reconsider usage of offsets:

One easy solution could be just to disable offsets in any Zserio expression.

Another solution could be that expressions with offsets can be used only after offset label. This is quite complicated and it is a question if this is usefull at all. For example, the following schema is ok:

struct CorrectSchema
{
    uint32 myOffset;
myOffset:
   string stringValue;
    uint8 optionalValue if myOffset == 4;
};

mikir avatar Jun 20 '24 07:06 mikir