Skip to content

Fix recursive destuffing in physical layer byte stuffing codec#7

Merged
gertvdijk merged 2 commits intodevelopfrom
fix-byte-stuffing-issue-6
Apr 21, 2026
Merged

Fix recursive destuffing in physical layer byte stuffing codec#7
gertvdijk merged 2 commits intodevelopfrom
fix-byte-stuffing-issue-6

Conversation

@gertvdijk
Copy link
Copy Markdown
Owner

@gertvdijk gertvdijk commented Apr 19, 2026

The previous PhysicalCodec.decode() implementation used repeated replace() calls, which could inadvertently destuff newly created byte sequences again. This caused some valid frames to be decoded incorrectly.

This changes the physical byte stuffing logic to a single-pass implementation for both decode() and encode().

Besides fixing an actual bug in decode(), this also removes the fragile ordering dependency from the encoding logic and adds validation for truncated or invalid stuffing sequences.


Original PR by @jktjkt in #6; this is a slightly reworked version using a generator and has been reworded.

Comment thread tests/test_codec.py
Copy link
Copy Markdown
Contributor

@jktjkt jktjkt left a comment

Choose a reason for hiding this comment

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

I was lazy and my test only included byte destuffing. Can you perhaps add another test for producing these byte-stuffed sequences?

Comment thread tests/test_codec.py
Comment thread src/pykmp/codec.py Outdated
@gertvdijk
Copy link
Copy Markdown
Owner Author

I was lazy and my test only included byte destuffing. Can you perhaps add another test for producing these byte-stuffed sequences?

Actually I believe it was silly to have a separate test_codec_physical_decode() and test_codec_physical_encode() to begin with; this should be symmetrical.

I'll add a commit under this one to merge these two tests -- that should cover that I guess then?

@gertvdijk gertvdijk force-pushed the fix-byte-stuffing-issue-6 branch from 4e75957 to 33d6299 Compare April 20, 2026 18:50
gertvdijk and others added 2 commits April 21, 2026 23:06
Merge the happy-path PhysicalCodec tests into a single parametrized test that
checks both decode() and encode() from the same examples.

This makes the physical-layer examples symmetrical, reduces duplicated test
data, and lowers the chance that an encoding or decoding fix is only covered on
one side.
The previous PhysicalCodec.decode() implementation used repeated replace()
calls, which could inadvertently destuff newly created byte sequences again.
This caused some valid frames to be decoded incorrectly.

This changes the physical byte stuffing logic to a single-pass
implementation for both decode() and encode().

Besides fixing an actual bug in decode(), this also removes the
fragile ordering dependency from the encoding logic and adds validation
for truncated or invalid stuffing sequences.

Co-Authored-By: Gert van Dijk <github@gertvandijk.nl>
@gertvdijk gertvdijk force-pushed the fix-byte-stuffing-issue-6 branch from 33d6299 to 0f9209a Compare April 21, 2026 21:08
@gertvdijk
Copy link
Copy Markdown
Owner Author

I couldn't find an actual bug in the encode() logic; it was just fragile I guess. I have reworded some things accordingly in the last force-push.

@gertvdijk gertvdijk changed the title Fix double-decode and double-encode in byte stuffing codec Fix recursive destuffing in physical layer byte stuffing codec Apr 21, 2026
@gertvdijk gertvdijk merged commit 920491d into develop Apr 21, 2026
@gertvdijk gertvdijk deleted the fix-byte-stuffing-issue-6 branch April 21, 2026 21:13
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.

2 participants