ProtoDef-io / node-protodef

Describe your protocol, and read it with ease.
MIT License
31 stars 18 forks source link

Array not enough elements or too much elements. #80

Open LiamKarlMitchell opened 7 years ago

LiamKarlMitchell commented 7 years ago

If you have an array with a fixed number for the count.

{
  "name": "numbers",
  "type": [
    "array",
    {
      "count": 3,
      "type": "li32"
    }
  ]
}

And you serialize less elements than the count.

  { "numbers": [1,2] }

Then the buffer should probably zero fill to look like this. <Buffer 01 00 00 00 02 00 00 00 00 00 00 00> Or have an error.

Currently it looks like this. <Buffer 01 00 00 00 02 00 00 00>

Yet if I go over 3 values like so.

  { "numbers": [1,2,3,4] }

It actually over-flows the space in which I would expect it to be allowed to write in. <Buffer 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00>

Then the parsing would be wrong, because the extra data would not be for the same packet.

I guess this is really two concerns. 1) Undefined to be 00 filled if count length is fixed number. (An option maybe to zero fill or error?) 2) Over-writing past count to throw an error? (Silent ignore could also be a thing maybe an option?)

roblabla commented 7 years ago

we should never silently zero-fill, and neither should we do whatever it is we do right now.

What should happen is, if we don't write sizeOf(type) bytes in the write function, we should blow up hard. This should probably be added in the general write logic, because it's not specific to array or anything.