From 503e64c84b9f6d3da5bc0a7ca15039360cea01f2 Mon Sep 17 00:00:00 2001 From: user <40304161+fullylegit@users.noreply.github.com> Date: Wed, 19 Nov 2025 23:48:04 +1100 Subject: [PATCH] Fixed a panic when the checksum contained multi-byte characters - Check that the entire sentence is ASCII as per the standard --- CHANGELOG.md | 1 + src/lib.rs | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26e5807..3a3a3c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added ### Changed +- Fixed panic on messages with multi-byte characters in the checksum ## [0.11.0] - 2024-06-13 ### Added diff --git a/src/lib.rs b/src/lib.rs index f4342dc..be2972c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -286,6 +286,12 @@ impl NmeaParser { } }; + if !sentence.is_ascii() { + return Err(ParseError::InvalidSentence(String::from( + "Invalid characters in sentence", + ))); + } + // Calculate NMEA checksum and compare it to the given one. Also, remove the checksum part // from the sentence to simplify next processing steps. let mut checksum = 0; @@ -625,9 +631,9 @@ mod test { fn test_parse_invalid_sentence() { let mut p = NmeaParser::new(); assert_eq!( - p.parse_sentence("$޴GAGSV,,"), + p.parse_sentence("$GAGSV^,,"), Err(ParseError::InvalidSentence( - "Invalid characters in sentence type: $\u{7b4}GAGSV".to_string() + "Invalid characters in sentence type: $GAGSV^".to_string() )) ); assert_eq!( @@ -648,6 +654,12 @@ mod test { "Invalid NMEA sentence: !".to_string() )) ); + assert_eq!( + p.parse_sentence("$GAGSV,,*💖"), + Err(ParseError::InvalidSentence( + "Invalid characters in sentence".to_string() + )) + ); } #[test] fn test_parse_prefix_chars() {