Skip to content

Documentation: Frame encoder silently truncates payloads > 4 GiB #174

@coderabbitai

Description

@coderabbitai

Description

The example code in docs/rust-binary-router-library-design.md for the LengthPrefixedCodec encoder has a potential overflow bug that could cause silent truncation for large payloads.

Problem

In the encode function, the line:

dst.put_u32(data.len() as u32);

Casts data.len() to u32 without validation. For payloads larger than 4 GiB (u32::MAX), this will wrap around and produce corrupt output with an incorrect length prefix.

Solution

Add validation before the cast:

if data.len() > u32::MAX as usize {
    return Err(io::Error::new(
        io::ErrorKind::InvalidInput,
        "payload exceeds 4 GiB limit",
    ));
}
dst.put_u32(data.len() as u32);

Context

While this is example code in documentation rather than production code, it's important to demonstrate best practices to avoid misleading developers who might copy this pattern.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions