Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions rust/sbp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,36 @@ mod tests {
}
}

#[test]
fn parser_crc_error() {
let packet = vec![
// Start with a mostly valid message, with a single byte error
0x55, 0x0c, // This byte should be 0x0b, changed to intentionally cause a CRC error
0x02, 0xd3, 0x88, 0x14, 0x28, 0xf4, 0x7a, 0x13, 0x96,
0x62, 0xee, 0xff, 0xbe, 0x40, 0x14, 0x00, 0xf6, 0xa3, 0x09, 0x00, 0x00, 0x00, 0x0e,
0x00, 0xdb, 0xbf, 0xde, 0xad, 0xbe, 0xef,
// Include another valid message to properly parse
0x55u8, 0x0b, 0x02, 0xd3, 0x88, 0x14, 0x28, 0xf4, 0x7a, 0x13, 0x96,
0x62, 0xee, 0xff, 0xbe, 0x40, 0x14, 0x00, 0xf6, 0xa3, 0x09, 0x00, 0x00, 0x00, 0x0e,
0x00, 0xdb, 0xbf, 0xde, 0xad, 0xbe, 0xef,
];
let mut reader = std::io::Cursor::new(packet);
let mut parser = crate::parser::Parser::new();
// Iterate through the data until we hit something that is
// parsable
let sbp_result = parser.parse(&mut reader);
assert!(sbp_result.is_err());
match sbp_result.unwrap_err() {
crate::Error::CrcError => {},
e => {
assert!(false, "Unexpected error: {:?}", e);
}
};

let sbp_result = parser.parse(&mut reader);
assert!(sbp_result.is_ok());
}

#[test]
fn making_frame() {
use crate::messages::SBPMessage;
Expand Down
9 changes: 7 additions & 2 deletions rust/sbp/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,19 @@ impl Parser {
self.buffer = self.buffer[bytes_read..].to_vec();
break Ok(msg);
}
(Err(crate::Error::ParseError), bytes_read) => {
(Err(e), bytes_read) => {
if bytes_read >= self.buffer.len() {
self.buffer.clear()
} else {
self.buffer = self.buffer[bytes_read..].to_vec();
}

if let crate::Error::ParseError = e {
// Continue parsing
} else {
break Err(e)
}
}
(Err(e), _bytes_read) => break Err(e),
}

if self.buffer.is_empty() {
Expand Down