-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor LengthFormat helpers to remove duplication #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1ba221b
c3f8193
e193ff8
dcfe939
94567a4
c038a88
de0cb4a
1efe26a
84f9791
bfcd5de
afa4a5d
7179eb7
eb3edf5
940ca2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1123,50 +1123,57 @@ examples are invaluable. They make the abstract design tangible and showcase how | |||||||||||||||||||||||||||||||||||||
| 2. **Frame Processor Implementation** (Simple Length-Prefixed Framing using | ||||||||||||||||||||||||||||||||||||||
| `tokio-util`): | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| ```rust | ||||||||||||||||||||||||||||||||||||||
| // Crate: my_frame_processor.rs | ||||||||||||||||||||||||||||||||||||||
| use bytes::{BytesMut, Buf, BufMut}; | ||||||||||||||||||||||||||||||||||||||
| use tokio_util::codec::{Encoder, Decoder}; | ||||||||||||||||||||||||||||||||||||||
| use std::io; | ||||||||||||||||||||||||||||||||||||||
| ```rust | ||||||||||||||||||||||||||||||||||||||
| // Crate: my_frame_processor.rs | ||||||||||||||||||||||||||||||||||||||
| use bytes::{BytesMut, Buf, BufMut}; | ||||||||||||||||||||||||||||||||||||||
| use tokio_util::codec::{Decoder, Encoder}; | ||||||||||||||||||||||||||||||||||||||
| use byteorder::{BigEndian, ReadBytesExt}; | ||||||||||||||||||||||||||||||||||||||
| use std::io; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| pub struct LengthPrefixedCodec; | ||||||||||||||||||||||||||||||||||||||
| const MAX_FRAME_LEN: usize = 16 * 1024 * 1024; // 16 MiB upper limit | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| impl Decoder for LengthPrefixedCodec { | ||||||||||||||||||||||||||||||||||||||
| type Item = BytesMut; // Raw frame payload | ||||||||||||||||||||||||||||||||||||||
| type Error = io::Error; | ||||||||||||||||||||||||||||||||||||||
| pub struct LengthPrefixedCodec; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { | ||||||||||||||||||||||||||||||||||||||
| if src.len() < 4 { return Ok(None); } // Not enough data for length prefix | ||||||||||||||||||||||||||||||||||||||
| impl Decoder for LengthPrefixedCodec { | ||||||||||||||||||||||||||||||||||||||
| type Item = BytesMut; // Raw frame payload | ||||||||||||||||||||||||||||||||||||||
| type Error = io::Error; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| let mut length_bytes = [0u8; 4]; | ||||||||||||||||||||||||||||||||||||||
| length_bytes.copy_from_slice(&src[..4]); | ||||||||||||||||||||||||||||||||||||||
| let length = u32::from_be_bytes(length_bytes) as usize; | ||||||||||||||||||||||||||||||||||||||
| fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> { | ||||||||||||||||||||||||||||||||||||||
| if src.len() < 4 { return Ok(None); } // Not enough data for length prefix | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if src.len() < 4 + length { | ||||||||||||||||||||||||||||||||||||||
| src.reserve(4 + length - src.len()); | ||||||||||||||||||||||||||||||||||||||
| return Ok(None); // Not enough data for full frame | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| let length = (&src[..4]) | ||||||||||||||||||||||||||||||||||||||
| .read_u32::<BigEndian>() | ||||||||||||||||||||||||||||||||||||||
| .expect("slice length checked") as usize; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| src.advance(4); // Consume length prefix | ||||||||||||||||||||||||||||||||||||||
| Ok(Some(src.split_to(length))) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| if length > MAX_FRAME_LEN { | ||||||||||||||||||||||||||||||||||||||
| return Err(io::Error::new(io::ErrorKind::InvalidInput, "frame too large")); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| impl<T: AsRef<[u8]>> Encoder<T> for LengthPrefixedCodec { | ||||||||||||||||||||||||||||||||||||||
| type Error = io::Error; | ||||||||||||||||||||||||||||||||||||||
| if src.len() < 4 + length { | ||||||||||||||||||||||||||||||||||||||
| src.reserve(4 + length - src.len()); | ||||||||||||||||||||||||||||||||||||||
| return Ok(None); // Not enough data for full frame | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| fn encode(&mut self, item: T, dst: &mut BytesMut) -> Result<(), Self::Error> { | ||||||||||||||||||||||||||||||||||||||
| let data = item.as_ref(); | ||||||||||||||||||||||||||||||||||||||
| dst.reserve(4 + data.len()); | ||||||||||||||||||||||||||||||||||||||
| dst.put_u32(data.len() as u32); | ||||||||||||||||||||||||||||||||||||||
| dst.put_slice(data); | ||||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||
| src.advance(4); // Consume length prefix | ||||||||||||||||||||||||||||||||||||||
| Ok(Some(src.split_to(length))) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| impl<T: AsRef<[u8]>> Encoder<T> for LengthPrefixedCodec { | ||||||||||||||||||||||||||||||||||||||
| type Error = io::Error; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| fn encode(&mut self, item: T, dst: &mut BytesMut) -> Result<(), Self::Error> { | ||||||||||||||||||||||||||||||||||||||
| let data = item.as_ref(); | ||||||||||||||||||||||||||||||||||||||
| dst.reserve(4 + data.len()); | ||||||||||||||||||||||||||||||||||||||
| dst.put_u32(data.len() as u32); | ||||||||||||||||||||||||||||||||||||||
| dst.put_slice(data); | ||||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1165
to
+1170
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Encode path silently truncates frames > 4 GiB.
- dst.put_u32(data.len() as u32);
+ 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);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coderabbitai new GitHub issue for this, please
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainI'll create a GitHub issue for the documentation bug you've identified. @leynos I've created a GitHub issue to track the documentation bug regarding the potential overflow in the frame encoder example. The issue includes details about the problem, the suggested fix, and references back to this PR and comment. 🏁 Script executed: Length of output: 1241 Perfect! I've successfully created GitHub issue #174 for the documentation bug. You can view it here: #174 The issue includes:
This will help track the need to fix the documentation example to demonstrate proper bounds checking for large payload sizes. |
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| (Note: "wireframe" would abstract the direct use of `Encoder`/`Decoder` | ||||||||||||||||||||||||||||||||||||||
| behind its own `FrameProcessor` trait or provide helpers.) | ||||||||||||||||||||||||||||||||||||||
| (Note: "wireframe" would abstract the direct use of `Encoder`/`Decoder` behind | ||||||||||||||||||||||||||||||||||||||
| its own `FrameProcessor` trait or provide helpers.) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| 1. **Server Setup and Handler**: | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.