From 80d7b5ed49cf56ba7713cfb9d091f140fa0e101f Mon Sep 17 00:00:00 2001 From: Thomas Hurst Date: Tue, 4 Sep 2018 21:40:18 +0100 Subject: Fix #26: Incorrect unwrap() guard in is_boundary() Guarding line.chars().nth(n) by enforcing n <= line.len() is incorrect, because len() operates in bytes, not Unicode characters. Drop the guard and make use of nth()'s Option to conditionally check the character if it's within bounds. Add a test to prevent regressions. --- src/lib.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index b8e8d65..a77e04d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -95,16 +95,11 @@ pub struct MailHeader<'a> { } fn is_boundary(line: &str, ix: Option) -> bool { - match ix { - None => true, - Some(v) => { - if v >= line.len() { - return true; - } - let c = line.chars().nth(v).unwrap(); - return c.is_whitespace() || c == '"' || c == '(' || c == ')' || c == '<' || c == '>'; - } - } + ix.map(|v| { + line.chars().nth(v).map_or(true, |c| { + c.is_whitespace() || c == '"' || c == '(' || c == ')' || c == '<' || c == '>' + }) + }).unwrap_or(true) } fn find_from(line: &str, ix_start: usize, key: &str) -> Option { @@ -1223,4 +1218,12 @@ mod tests { assert_eq!(mail.get_body_raw().unwrap(), b""); assert_eq!(mail.get_body().unwrap(), ""); } + + #[test] + fn test_is_boundary_multibyte() { + // Bug #26, Incorrect unwrap() guard in is_boundary() + // 6x'REPLACEMENT CHARACTER', but 18 bytes of data: + let test = "\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}"; + assert!(is_boundary(test, Some(8))); + } } -- cgit v1.2.3