parser: fix panic on invalid message id

memfd
Manos Pitsidianakis 2020-05-06 18:53:44 +03:00
parent f2ecb81612
commit 5981f98f17
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
2 changed files with 17 additions and 5 deletions

View File

@ -95,12 +95,18 @@ impl Address {
}
pub fn get_tags(&self, separator: char) -> Vec<String> {
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<u8>, 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 {

View File

@ -77,11 +77,17 @@ impl BytesExt for [u8] {
}
// https://stackoverflow.com/a/35907071
fn find(&self, needle: &[u8]) -> Option<usize> {
if needle.is_empty() {
return None;
}
self.windows(needle.len())
.position(|window| window == needle)
}
fn rfind(&self, needle: &[u8]) -> Option<usize> {
if needle.is_empty() {
return None;
}
self.windows(needle.len())
.rposition(|window| window == needle)
}