Add indefinite lenght support to cbor read functions.#111
Conversation
LCOV of commit
|
There was a problem hiding this comment.
Pull request overview
This PR extends the library’s CBOR support to handle RFC 8949 indefinite-length encodings by introducing synthetic CBOR “types” (via the 0x100 bit) and updating header read/write plus skip-item behavior accordingly.
Changes:
- Add synthetic
n20_cbor_type_*indefinite*_evariants and document their meaning (0x100bit). - Extend
n20_cbor_write_header,n20_cbor_read_header, andn20_cbor_read_skip_itemto support indefinite-length items and break (0xFF). - Expand CBOR unit tests and adjust message tests to avoid using
0xFFas an “invalid item” now that it’s recognized as a break code.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/service/test/messages.cpp |
Updates malformed message tests to use a reserved/invalid simple value (0xFE) instead of 0xFF (break). |
src/core/test/cbor.cpp |
Adds extensive coverage for indefinite-length header read/write and skip_item behavior; updates invalid-header parameterization. |
src/core/cbor.c |
Implements indefinite-length handling in header read/write and adds an internal-result-based skipper that can process break-terminated structures. |
include/nat20/cbor.h |
Introduces synthetic indefinite-length type variants and documents new behavior/contracts for read/write/skip. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| * of an indefinite length string) is treated as an error and the function | ||
| * returns false. The chunks of an indefinite length byte or text string must | ||
| * be definite length byte or text strings respectively; anything else is an | ||
| * error. |
There was a problem hiding this comment.
I think you can mention that it's possible to have nested indefinite length arrays/maps, and if they're there, they also need to have the break stop codes.
There was a problem hiding this comment.
I added tests accordingly.
| case n20_cbor_type_tag_e: | ||
| /* Skip the tag and the item it refers to. */ | ||
| return n20_cbor_read_skip_item(s); | ||
| return n20_cbor_read_skip_item(s) ? n20_cbor_read_skip_item_ok_e |
There was a problem hiding this comment.
Should this call the internal function like other cases?
| return n20_cbor_read_skip_item(s) ? n20_cbor_read_skip_item_ok_e | |
| return n20_cbor_read_skip_item_internal(s) ? n20_cbor_read_skip_item_ok_e |
There was a problem hiding this comment.
No this is correct. If this returns ..._break_e it needs to be an error because we cannot have a tagged break symbol. So in terms of the internal semantic it would be _internal(s) == _ok_e ? _ok_e : _error_e which is exactly what n20_cbor_read_skip_item does.
| if (result == n20_cbor_read_skip_item_error_e) { | ||
| return n20_cbor_read_skip_item_error_e; | ||
| } | ||
| } while (result != n20_cbor_read_skip_item_break_e); |
There was a problem hiding this comment.
If we have a malformed indef length array, wouldn't it be possible for the recursion to overflow?
There was a problem hiding this comment.
The read functions make sure not to read past the end of the buffer. Even a definite length byte string can be larger than the buffer. In this case the read functions return false and the internal skip function returns ..._error_e.
Add synthetic cbor type variant to the enum n20_cbor_types_e that indicate indefinite length encoding. And adds support for indefinite length encoding to the read and write header functions as well as to the cbor skip item function.