codec: fix broken length_delimited builder doc examples by GuTS805 · Pull Request #8350 · tokio-rs/tokio · GitHub
Skip to content

codec: fix broken length_delimited builder doc examples - #8350

Merged
Darksonn merged 1 commit into
tokio-rs:masterfrom
GuTS805:codec/fix-length-delimited-builder-docs
Aug 9, 2026
Merged

codec: fix broken length_delimited builder doc examples#8350
Darksonn merged 1 commit into
tokio-rs:masterfrom
GuTS805:codec/fix-length-delimited-builder-docs

Conversation

@GuTS805

@GuTS805 GuTS805 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

The Builder::new, new_codec, and new_read doc examples combine length_adjustment(0) with num_skip(0). This leaves the length header bytes in the buffer without accounting for them, so decoding returns a frame that includes the raw header at the front and is short at the back, corrupting the following frame.

Motivation

Copying these examples as written produces a LengthDelimitedCodec that silently corrupts every frame after the first, since the length header is never skipped but the byte count returned by decode isn't adjusted to account for it.

Solution

Drop num_skip(0) so the default (skip the header) behavior applies, and turn all three examples into executable doctests that perform a real encode/decode round-trip and assert on the payload, so this class of bug is caught automatically going forward. new_write was already correct and is unchanged.

Fixes #8348

Note: aware of #8349 which addresses the same issue — opening this as an independent fix.

The Builder::new, new_codec, and new_read doc examples combined
length_adjustment(0) with num_skip(0). This leaves the length
header bytes in the buffer without accounting for them, so decoding
returns a frame that includes the raw header at the front and is
short at the back, corrupting the following frame.

Drop num_skip(0) so the default (skip the header) behavior applies,
and turn all three examples into executable doctests that perform a
real encode/decode round-trip and assert on the payload, so this
class of bug is caught automatically going forward.

Fixes: tokio-rs#8348
@github-actions github-actions Bot added the R-loom-util Run loom tokio-util tests on this PR label Aug 7, 2026
@ADD-SP ADD-SP added the A-tokio-util Area: The tokio-util crate label Aug 7, 2026
@Darksonn Darksonn added the M-codec Module: tokio-util/codec label Aug 9, 2026

@Darksonn Darksonn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks.

@Darksonn
Darksonn merged commit b6ed004 into tokio-rs:master Aug 9, 2026
102 checks passed
@GuTS805
GuTS805 deleted the codec/fix-length-delimited-builder-docs branch August 9, 2026 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-tokio-util Area: The tokio-util crate M-codec Module: tokio-util/codec R-loom-util Run loom tokio-util tests on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LengthDelimitedCodec Builder doc examples (new/new_codec/new_read/new_write) still use broken length_adjustment(0)+num_skip(0) combo, corrupts frames

3 participants