Skip to content

Add indefinite lenght support to cbor read functions.#111

Merged
werwurm merged 4 commits into
mainfrom
werwurm/cbor_indefinite_length_encoding
Jun 17, 2026
Merged

Add indefinite lenght support to cbor read functions.#111
werwurm merged 4 commits into
mainfrom
werwurm/cbor_indefinite_length_encoding

Conversation

@werwurm

@werwurm werwurm commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

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.

@github-actions

github-actions Bot commented Jun 16, 2026

Copy link
Copy Markdown

LCOV of commit c9cfdfa during lcov-test-coverage-report #258

Summary coverage rate:
  lines......: 95.7% (3105 of 3245 lines)
  functions..: 99.2% (235 of 237 functions)
  branches...: 87.3% (1697 of 1943 branches)

Files changed coverage rate: n/a

@werwurm werwurm marked this pull request as ready for review June 16, 2026 17:14
@werwurm werwurm requested review from Copilot and mjain02 June 16, 2026 17:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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*_e variants and document their meaning (0x100 bit).
  • Extend n20_cbor_write_header, n20_cbor_read_header, and n20_cbor_read_skip_item to support indefinite-length items and break (0xFF).
  • Expand CBOR unit tests and adjust message tests to avoid using 0xFF as 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.

Comment thread src/core/cbor.c Outdated
Comment thread src/core/cbor.c Outdated
Comment thread src/core/cbor.c

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Comment thread include/nat20/cbor.h
* 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added tests accordingly.

@werwurm werwurm requested a review from smacdude June 16, 2026 21:31
Comment thread src/core/cbor.c
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this call the internal function like other cases?

Suggested change
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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/core/cbor.c
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have a malformed indef length array, wouldn't it be possible for the recursion to overflow?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@werwurm werwurm requested a review from mjain02 June 17, 2026 15:56
@werwurm werwurm merged commit a517e96 into main Jun 17, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants