NordicSemiconductor / zcbor

Low footprint C/C++ CBOR library and Python tool providing code generation from CDDL descriptions.
Apache License 2.0
118 stars 37 forks source link

zcbor_encode.c: Fix a length bug in zcbor_bstr_end_decode() #434

Closed oyvindronningstad closed 5 months ago

oyvindronningstad commented 5 months ago

The bug caused CBOR-encoded bstrs to lose a byte.

When calculating the maximum length of the bstr, there are certain payload lengths which cannot be filled by a bstr (incl. header and payload). One of these lengths is 258, since a 255 byte payload gets a 2-byte header (total 257) and a 256 byte payload gets a 3-byte header (total 259), i.e. there is no combination that gives 258 bytes. The same applies to tota lengths of 25, 65539-65540, and 4294967300-4294967304

The previous code tried to find the start of the string by extrapolating from the end of the payload, but this gave the wrong answer for the cases described above.

Add a test that tests some of the cases.