From 5981f98f176b8d260f0ea6a019386e44e42df196 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Wed, 6 May 2020 18:53:44 +0300 Subject: [PATCH] parser: fix panic on invalid message id --- melib/src/email/address.rs | 16 +++++++++++----- melib/src/email/parser.rs | 6 ++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/melib/src/email/address.rs b/melib/src/email/address.rs index a00b08bd4..44ca48572 100644 --- a/melib/src/email/address.rs +++ b/melib/src/email/address.rs @@ -95,12 +95,18 @@ impl Address { } pub fn get_tags(&self, separator: char) -> Vec { - let mut ret = Vec::new(); let email = self.get_email(); - let at_pos = email.as_bytes().iter().position(|&b| b == b'@').unwrap(); + let at_pos = email + .as_bytes() + .iter() + .position(|&b| b == b'@') + .unwrap_or(0); let email: &str = email[..at_pos].into(); - ret.extend(email.split(separator).skip(1).map(str::to_string)); - ret + email + .split(separator) + .skip(1) + .map(str::to_string) + .collect::<_>() } } @@ -190,7 +196,7 @@ pub struct MessageID(pub Vec, pub StrBuilder); impl StrBuild for MessageID { fn new(string: &[u8], slice: &[u8]) -> Self { - let offset = string.find(slice).unwrap(); + let offset = string.find(slice).unwrap_or(0); MessageID( string.to_owned(), StrBuilder { diff --git a/melib/src/email/parser.rs b/melib/src/email/parser.rs index 5d5b2aca4..c5d4fc220 100644 --- a/melib/src/email/parser.rs +++ b/melib/src/email/parser.rs @@ -77,11 +77,17 @@ impl BytesExt for [u8] { } // https://stackoverflow.com/a/35907071 fn find(&self, needle: &[u8]) -> Option { + if needle.is_empty() { + return None; + } self.windows(needle.len()) .position(|window| window == needle) } fn rfind(&self, needle: &[u8]) -> Option { + if needle.is_empty() { + return None; + } self.windows(needle.len()) .rposition(|window| window == needle) }