Skip to content

Commit

Permalink
fix fix Frame::is_parsable
Browse files Browse the repository at this point in the history
  • Loading branch information
matejfric committed Dec 1, 2024
1 parent 51c505e commit ede3811
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,21 @@ fn seek_newline(cursor: &mut Cursor<&[u8]>) -> anyhow::Result<usize, RedisProtoc
fn has_crlf_with_checks(cursor: &mut Cursor<&[u8]>) -> anyhow::Result<(), RedisProtocolError> {
while cursor.has_remaining() {
let byte = cursor.get_u8();
let has_remaining = cursor.has_remaining();
if byte == b'\r' {
if cursor.has_remaining() {
if has_remaining {
if cursor.get_u8() == b'\n' {
return Ok(());
}
} else {
// We might get the missing byte later
return Err(RedisProtocolError::NotEnoughData);
}
// Delimiter must be `\r\n`
return Err(RedisProtocolError::ExcessiveNewline);
}
if byte == b'\n' && cursor.has_remaining() && cursor.get_u8() != b'\r' {
if byte == b'\n' && has_remaining {
// Early return if there is an extra newline character (delimiter must be `\r\n`)
return Err(RedisProtocolError::ExcessiveNewline);
}
}
Expand Down
28 changes: 28 additions & 0 deletions tests/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,34 @@ mod tests {
assert!(Frame::is_parsable(&mut cursor).is_ok(), "Expected Ok");
}

#[test]
fn incorrect_delimiter_in_simple_string() {
let data = b"+Hello from LFCR\n\r";
let mut cursor = Cursor::new(&data[..]);

assert!(
matches!(
Frame::is_parsable(&mut cursor),
Err(RedisProtocolError::ExcessiveNewline)
),
"Expected ExcessiveNewline"
);
}

#[test]
fn incorrect_delimiter_in_bulk_string() {
let data = b"$3\nabc\n\r";
let mut cursor = Cursor::new(&data[..]);

assert!(
matches!(
Frame::is_parsable(&mut cursor),
Err(RedisProtocolError::NotEnoughData)
),
"Expected NotEnoughData"
);
}

#[test]
fn test_null_bulk_string() {
get_or_init_logger();
Expand Down

0 comments on commit ede3811

Please sign in to comment.