From c655fd8a64785bd230f8f7ae2f101b021f8a1f55 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Fri, 19 Jun 2020 02:44:23 +0300 Subject: [PATCH] contact: forbid indexing and slicing --- src/contact.rs | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/contact.rs b/src/contact.rs index 361e72274..cfd221441 100644 --- a/src/contact.rs +++ b/src/contact.rs @@ -1,5 +1,7 @@ //! Contacts module +#![forbid(clippy::indexing_slicing)] + use async_std::path::PathBuf; use deltachat_derive::*; use itertools::Itertools; @@ -1029,10 +1031,10 @@ pub fn addr_normalize(addr: &str) -> &str { let norm = addr.trim(); if norm.starts_with("mailto:") { - return &norm[7..]; + norm.get(7..).unwrap_or(norm) + } else { + norm } - - norm } fn sanitize_name_and_addr(name: impl AsRef, addr: impl AsRef) -> (String, String) { @@ -1042,11 +1044,15 @@ fn sanitize_name_and_addr(name: impl AsRef, addr: impl AsRef) -> (Stri if let Some(captures) = ADDR_WITH_NAME_REGEX.captures(addr.as_ref()) { ( if name.as_ref().is_empty() { - normalize_name(&captures[1]) + captures + .get(1) + .map_or("".to_string(), |m| normalize_name(m.as_str())) } else { name.as_ref().to_string() }, - captures[2].to_string(), + captures + .get(2) + .map_or("".to_string(), |m| m.as_str().to_string()), ) } else { (name.as_ref().to_string(), addr.as_ref().to_string()) @@ -1117,24 +1123,17 @@ pub(crate) async fn set_profile_image( /// /// Typically, this function is not needed as it is called implicitly by `Contact::add_address_book`. pub fn normalize_name(full_name: impl AsRef) -> String { - let mut full_name = full_name.as_ref().trim(); + let full_name = full_name.as_ref().trim(); if full_name.is_empty() { return full_name.into(); } - let len = full_name.len(); - if len > 1 { - let firstchar = full_name.as_bytes()[0]; - let lastchar = full_name.as_bytes()[len - 1]; - if firstchar == b'\'' && lastchar == b'\'' - || firstchar == b'\"' && lastchar == b'\"' - || firstchar == b'<' && lastchar == b'>' - { - full_name = &full_name[1..len - 1]; - } + match full_name.as_bytes() { + [b'\'', .., b'\''] | [b'\"', .., b'\"'] | [b'<', .., b'>'] => full_name + .get(1..full_name.len() - 1) + .map_or("".to_string(), |s| s.trim().into()), + _ => full_name.to_string(), } - - full_name.trim().into() } fn cat_fingerprint(