From 8d43ad48097e10e1011a63e89a18e1381f69a183 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Tue, 23 Jul 2019 00:12:23 +0300 Subject: [PATCH] Construct dc_array_t id arrays using safe methods --- src/context.rs | 15 ++++++--------- src/dc_chat.rs | 28 ++++++++++++---------------- src/dc_contact.rs | 16 ++++++++-------- 3 files changed, 26 insertions(+), 33 deletions(-) diff --git a/src/context.rs b/src/context.rs index c61fee72c..1bbc8a986 100644 --- a/src/context.rs +++ b/src/context.rs @@ -521,13 +521,13 @@ pub fn dc_get_fresh_msgs(context: &Context) -> *mut dc_array_t { &[10, 9, if 0 != show_deaddrop { 2 } else { 0 }], |row| row.get(0), |rows| { - let ret = dc_array_new(128 as size_t); + let mut ret = dc_array_t::new(128); for row in rows { let id = row?; - unsafe { dc_array_add_id(ret, id) }; + ret.add_id(id); } - Ok(ret) + Ok(ret.as_ptr()) }, ) .unwrap() @@ -560,7 +560,7 @@ pub fn dc_search_msgs( AND ct.blocked=0 AND (m.txt LIKE ? OR ct.name LIKE ?) ORDER BY m.timestamp DESC,m.id DESC;" }; - let ret = dc_array_new(100 as size_t); + let mut ret = dc_array_t::new(100); let success = context .sql @@ -570,7 +570,7 @@ pub fn dc_search_msgs( |row| row.get::<_, i32>(0), |rows| { for id in rows { - unsafe { dc_array_add_id(ret, id? as u32) }; + ret.add_id(id? as u32); } Ok(()) }, @@ -578,12 +578,9 @@ pub fn dc_search_msgs( .is_ok(); if success { - return ret; + return ret.as_ptr(); } - if !ret.is_null() { - unsafe { dc_array_unref(ret) }; - } std::ptr::null_mut() } diff --git a/src/dc_chat.rs b/src/dc_chat.rs index 59c4b2310..2668b08a3 100644 --- a/src/dc_chat.rs +++ b/src/dc_chat.rs @@ -1094,14 +1094,13 @@ pub unsafe fn dc_get_draft(context: &Context, chat_id: uint32_t) -> *mut dc_msg_ draft_msg } -pub unsafe fn dc_get_chat_msgs( +pub fn dc_get_chat_msgs( context: &Context, chat_id: uint32_t, flags: uint32_t, marker1before: uint32_t, ) -> *mut dc_array_t { - let ret = dc_array_new(512); - assert!(!ret.is_null()); + let mut ret = dc_array_t::new(512); let mut last_day = 0; let cnv_to_local = dc_gm2local_offset(); @@ -1111,17 +1110,17 @@ pub unsafe fn dc_get_chat_msgs( for row in rows { let (curr_id, ts) = row?; if curr_id as u32 == marker1before { - dc_array_add_id(ret, 1); + ret.add_id(1); } if 0 != flags & 0x1 { let curr_local_timestamp = ts + cnv_to_local; let curr_day = (curr_local_timestamp / 86400) as libc::c_int; if curr_day != last_day { - dc_array_add_id(ret, 9); + ret.add_id(9); last_day = curr_day; } } - dc_array_add_id(ret, curr_id as u32); + ret.add_id(curr_id as u32); } Ok(()) }; @@ -1169,11 +1168,8 @@ pub unsafe fn dc_get_chat_msgs( }; if success.is_ok() { - ret + ret.as_ptr() } else { - if !ret.is_null() { - dc_array_unref(ret); - } 0 as *mut dc_array_t } } @@ -1285,11 +1281,11 @@ pub fn dc_get_chat_media( ], |row| row.get::<_, i32>(0), |ids| { - let ret = dc_array_new(100); + let mut ret = dc_array_t::new(100); for id in ids { - unsafe { dc_array_add_id(ret, id? as u32) }; + ret.add_id(id? as u32); } - Ok(ret) + Ok(ret.as_ptr()) } ).unwrap_or_else(|_| std::ptr::null_mut()) } @@ -1458,13 +1454,13 @@ pub fn dc_get_chat_contacts(context: &Context, chat_id: u32) -> *mut dc_array_t params![chat_id as i32], |row| row.get::<_, i32>(0), |ids| { - let ret = dc_array_new(100); + let mut ret = dc_array_t::new(100); for id in ids { - unsafe { dc_array_add_id(ret, id? as u32) }; + ret.add_id(id? as u32); } - Ok(ret) + Ok(ret.as_ptr()) }, ) .unwrap_or_else(|_| std::ptr::null_mut()) diff --git a/src/dc_contact.rs b/src/dc_contact.rs index 3bb956caa..a10a6bfba 100644 --- a/src/dc_contact.rs +++ b/src/dc_contact.rs @@ -537,7 +537,7 @@ pub fn dc_get_contacts( .unwrap_or_default(); let mut add_self = false; - let ret = dc_array_new(100); + let mut ret = dc_array_t::new(100); if (listflags & DC_GCL_VERIFIED_ONLY) > 0 || !query.is_null() { let s3strLikeCmd = format!("%{}%", if !query.is_null() { as_str(query) } else { "" }); @@ -565,7 +565,7 @@ pub fn dc_get_contacts( |row| row.get::<_, i32>(0), |ids| { for id in ids { - unsafe { dc_array_add_id(ret, id? as u32) }; + ret.add_id(id? as u32); } Ok(()) }, @@ -595,7 +595,7 @@ pub fn dc_get_contacts( |row| row.get::<_, i32>(0), |ids| { for id in ids { - unsafe { dc_array_add_id(ret, id? as u32) }; + ret.add_id(id? as u32); } Ok(()) } @@ -603,10 +603,10 @@ pub fn dc_get_contacts( } if 0 != listflags & 0x2 && add_self { - unsafe { dc_array_add_id(ret, 1) }; + ret.add_id(1); } - ret + ret.as_ptr() } pub fn dc_get_blocked_cnt(context: &Context) -> libc::c_int { @@ -629,13 +629,13 @@ pub fn dc_get_blocked_contacts(context: &Context) -> *mut dc_array_t { params![9], |row| row.get::<_, i32>(0), |ids| { - let ret = dc_array_new(100); + let mut ret = dc_array_t::new(100); for id in ids { - unsafe { dc_array_add_id(ret, id? as u32) }; + ret.add_id(id? as u32); } - Ok(ret) + Ok(ret.as_ptr()) }, ) .unwrap_or_else(|_| std::ptr::null_mut())