Skip to content

fix:add validation and comments#155

Closed
Dailiduzhou wants to merge 0 commit intotokio-rs:masterfrom
Dailiduzhou:master
Closed

fix:add validation and comments#155
Dailiduzhou wants to merge 0 commit intotokio-rs:masterfrom
Dailiduzhou:master

Conversation

@Dailiduzhou
Copy link
Copy Markdown
Contributor

Summary

Added detailed comments to the Frame::check function's bulk string (b'$') case in src/frame.rs to explain the RESP (Redis Serialization Protocol) format and validation logic.
Added validation for special cases, aligning with Frame::parse.

Changes

File: src/frame.rs
Added comments and validation:

               // Bulk strings in the RESP protocol.
               //
               // Format: $<length>\r\n<data>\r\n
               //
               // Special case: $-1\r\n represents a Null value.
               // Validates that the frame conforms to RESP protocol.
               if b'-' == peek_u8(src)? {
                    let line = get_line(src)?;
                    if line != b"-1" {
                        return Err("protocol error; invalid frame format".into());
                    }
                    Ok(())
                } else {
                    // Read the bulk string
                    let len: usize = get_decimal(src)?.try_into()?;

                    // skip that number of bytes + 2 (\r\n).
                    skip(src, len + 2)
                }

Rationale

  • The bulk string case previously had minimal documentation
  • Clarifies the expected RESP protocol format for new contributors
  • Documents the special Null value case ($-1\r\n)
  • Improves code maintainability by making the protocol validation logic explicit

@Darksonn
Copy link
Copy Markdown
Member

Can you add a test case where invalid data is sent here, and assert that it is rejected?

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