From 7e11def527f5f970c7c307243ed3be408fc37f59 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Mon, 9 Sep 2019 00:01:15 +0200 Subject: [PATCH 1/3] make code more readable --- src/dc_receive_imf.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/dc_receive_imf.rs b/src/dc_receive_imf.rs index 29bfdd2f9..091f8ba40 100644 --- a/src/dc_receive_imf.rs +++ b/src/dc_receive_imf.rs @@ -1701,7 +1701,13 @@ unsafe fn search_chat_ids_by_contact_ids( let contact_ids_str = join(contact_ids.iter().map(|x| x.to_string()), ","); context.sql.query_map( format!( - "SELECT DISTINCT cc.chat_id, cc.contact_id FROM chats_contacts cc LEFT JOIN chats c ON c.id=cc.chat_id WHERE cc.chat_id IN(SELECT chat_id FROM chats_contacts WHERE contact_id IN({})) AND c.type=120 AND cc.contact_id!=1 ORDER BY cc.chat_id, cc.contact_id;", + "SELECT DISTINCT cc.chat_id, cc.contact_id \ + FROM chats_contacts cc \ + LEFT JOIN chats c ON c.id=cc.chat_id \ + WHERE cc.chat_id IN(SELECT chat_id FROM chats_contacts WHERE contact_id IN({})) \ + AND c.type=120 \ + AND cc.contact_id!=1 \ + ORDER BY cc.chat_id, cc.contact_id;", contact_ids_str ), params![], @@ -1713,11 +1719,11 @@ unsafe fn search_chat_ids_by_contact_ids( for row in rows { let (chat_id, contact_id) = row?; - if chat_id as u32 != last_chat_id { + if chat_id != last_chat_id { if matches == contact_ids.len() && mismatches == 0 { chat_ids.push(last_chat_id); } - last_chat_id = chat_id as u32; + last_chat_id = chat_id; matches = 0; mismatches = 0; } From d933183e0a4c0c2b7b507c1ea609e349b4cd710d Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Mon, 9 Sep 2019 00:56:13 +0200 Subject: [PATCH 2/3] mark safe functions as such --- src/dc_receive_imf.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/dc_receive_imf.rs b/src/dc_receive_imf.rs index 091f8ba40..53f6d3e54 100644 --- a/src/dc_receive_imf.rs +++ b/src/dc_receive_imf.rs @@ -923,7 +923,7 @@ unsafe fn handle_reports( } } -unsafe fn save_locations( +fn save_locations( context: &Context, mime_parser: &dc_mimeparser_t, chat_id: u32, @@ -1681,10 +1681,7 @@ fn hex_hash(s: impl AsRef) -> String { } #[allow(non_snake_case)] -unsafe fn search_chat_ids_by_contact_ids( - context: &Context, - unsorted_contact_ids: &Vec, -) -> Vec { +fn search_chat_ids_by_contact_ids(context: &Context, unsorted_contact_ids: &Vec) -> Vec { /* searches chat_id's by the given contact IDs, may return zero, one or more chat_id's */ let mut contact_ids = Vec::with_capacity(23); let mut chat_ids = Vec::with_capacity(23); From 13678739494589d8bd055a0d38ed733d0abab07e Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Mon, 9 Sep 2019 01:47:31 +0200 Subject: [PATCH 3/3] check bounds before accessing Vec --- src/dc_receive_imf.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dc_receive_imf.rs b/src/dc_receive_imf.rs index 53f6d3e54..9a2e5a448 100644 --- a/src/dc_receive_imf.rs +++ b/src/dc_receive_imf.rs @@ -1724,7 +1724,7 @@ fn search_chat_ids_by_contact_ids(context: &Context, unsorted_contact_ids: &Vec< matches = 0; mismatches = 0; } - if contact_id == contact_ids[matches] { + if matches < contact_ids.len() && contact_id == contact_ids[matches] { matches += 1; } else { mismatches += 1;