bergzand / NanoCBOR

CBOR library aimed at heavily constrained devices
Creative Commons Zero v1.0 Universal
47 stars 23 forks source link

decoder: fix double decrement for indefinite containers #53

Closed DanielLockau-MLPA closed 3 years ago

DanielLockau-MLPA commented 3 years ago

This fixes early detection of end of container in case of indefinite containers within definite containers. Previously, a double decrement of the remaining items happened:

Example:

In tests/fuzz, execute

make test; echo v2NzZXECY2NtZIe/Y2xlZPT/v2VkZWxheRkB9P+/ZGVjaG9pSSdtIGRvbmUh/79jbGVk9f+/ZWRlbGF5GQH0/79jbGVk9P+/ZGVjaG9tTm93IEknbSBkb25lIf9jY2Zn9v8= | base64 -d | ./bin/fuzztest

Result without the fix

Reading 98 bytes from stdin
advancing
parsing cbor
{
  "seq": 2,
  "cmd": [
    {
      "led": False,
    },
    {
      "delay": 500,
    },
    {
      "echo": "I'm done!",
    },
    {
      "led": True,
    },
  ],
  {
    "delay": 500,
  }: {
    "led": False,
  },
  {
    "echo": "Now I'm done!",
  }: "cfg",
  NULL: Unsupported type
Err
},
Done parsing cbor

Result with the fix

Reading 98 bytes from stdin
advancing
parsing cbor
{
  "seq": 2,
  "cmd": [
    {
      "led": False,
    },
    {
      "delay": 500,
    },
    {
      "echo": "I'm done!",
    },
    {
      "led": True,
    },
    {
      "delay": 500,
    },
    {
      "led": False,
    },
    {
      "echo": "Now I'm done!",
    },
  ],
  "cfg": NULL,
},
Done parsing cbor
DanielLockau-MLPA commented 3 years ago

I am aware that my changes are somewhat verbose. I wanted to avoid the hacky local change. If you have an idea of better formulating this, I'm happy to rework. /Daniel

@benpicco FYI

DanielLockau-MLPA commented 3 years ago

Closing as this is now already covered in #54.