summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
authorThomas Hurst <tom@hur.st>2018-09-04 21:40:18 +0100
committerKartikaya Gupta (kats) <staktrace@users.noreply.github.com>2018-09-04 21:06:35 -0400
commit80d7b5ed49cf56ba7713cfb9d091f140fa0e101f (patch)
tree500f3c99c0d9df8c2639d59b6520047bc244443c /src/lib.rs
parent03a4b80888a677a78bd0b3e0be19303a71e261f0 (diff)
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.
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs23
1 files changed, 13 insertions, 10 deletions
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<usize>) -> 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<usize> {
@@ -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)));
+ }
}