Skip to content

feat(cobs): Fixed issue when exactly 255 bytes (non zero) are encoded. - #780

Merged
finger563 merged 2 commits into
esp-cpp:mainfrom
jschuld:feature/cobs_fix_encoding_255_bytes_with_no_zeros
Sep 7, 2026
Merged

feat(cobs): Fixed issue when exactly 255 bytes (non zero) are encoded.#780
finger563 merged 2 commits into
esp-cpp:mainfrom
jschuld:feature/cobs_fix_encoding_255_bytes_with_no_zeros

Conversation

@jschuld

@jschuld jschuld commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Description

When encoding data of exactly 255 bytes in length, where the data is all non-zero, the last byte encoded is missing. This results in the encoded data not being valid COBS and can't be decoded.

Motivation and Context

While this is an edge case, it is a valid case and breaks any COBS decoding. This issue was identified when diagnosing issues where COBS decoding would fail intermittedly. A different COBS library was de decoding party. Initial suspicion was actual corruption of data on the line. However, after digging into this the issue was identified to be the COBS encoding for very specific cases (the above case)

How has this been tested?

  • Created a unittest (included) which reproduced the issue.
  • Ran fix on actual hardware, resulting in zero problems after the fix.

Screenshots (if appropriate, e.g. schematic, board, console logs, lab pictures):

N/A

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update
  • Hardware (schematic, board, system design) change
  • Software change

Checklist:

  • My change requires a change to the documentation.
  • I have added / updated the documentation related to this change via either README or WIKI

Software

  • I have added tests to cover my changes.
  • [-] (N/A Test added to exising tests) I have updated the .github/workflows/build.yml file to add my new test to the automated cloud build github action.
  • All new and existing tests passed.
  • My code follows the code style of this project.

Copilot AI lite review requested due to automatic review settings September 7, 2026 01:56
@jschuld jschuld changed the title Fixed issue when exactly 255 bytes (non zero) are encoded. feat(cobs): Fixed issue when exactly 255 bytes (non zero) are encoded. Sep 7, 2026
@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

✅Static analysis result - no issues found! ✅

void test_edge_cases(espp::Logger &logger) {
logger.info("\n=== Edge Cases Test ===");

// Test 1: Maximum block size (254 non-zero bytes)

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.

Note: This tests seems to attempt to test the same as the test that is added. However, I am unsure if there is a specific reason that 254 is chosen, so I have opted to add an extra test.

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.

🟢 Approval recommended

The encoder fix addresses the described edge-case correctly and adds coverage for the regression, with only minor non-blocking nits noted.

Pull request overview

Fixes an edge-case bug in the COBS encoder where encoding an input of exactly 255 bytes (all non-zero) could drop the final byte, producing invalid COBS output.

Changes:

  • Corrected block rollover handling in Cobs::encode_packet to always reserve space for the next code byte when a block completes (code == 0xff).
  • Added an edge-case regression test in the COBS example to cover a 255-byte all-nonzero payload.
File summaries
File Description
components/cobs/src/cobs.cpp Fixes encoder pointer advancement on block boundary to prevent losing a byte for the 255-byte all-nonzero case.
components/cobs/example/main/cobs_example.cpp Adds a regression test case for 255-byte all-nonzero payloads (plus minor wording/formatting follow-ups noted in comments).
Review details

Suppressed comments (1)

components/cobs/example/main/cobs_example.cpp:701

  • The failure log message still says "block len", which is ambiguous; update it to the concrete 255-byte edge case so logs clearly match the scenario being tested.
      logger.error(
          "Test 5: FAIL - Packet of exactly block len with no zeros (decoded: {}, expected: {}, encoded: {})",
          decoded.size(), block_len_packet.size(), encoded.size());
    }
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread components/cobs/example/main/cobs_example.cpp Outdated
Comment thread components/cobs/example/main/cobs_example.cpp Outdated
codep = encode;
if (!byte)
++encode;
++encode;

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.

If the loop ends because of no more data, we still want to ready the next encoding position for the delimiter to be added.

@finger563

Copy link
Copy Markdown
Contributor

Thanks for the fix!

@finger563
finger563 merged commit 178a45c into esp-cpp:main Sep 7, 2026
151 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