Make dc_array_new and dc_array_new_typed safe

Just like Box::into_raw, these functions are safe,
even though the caller is responsible for the allocated structure.
This commit is contained in:
Alexander Krotov
2019-07-22 14:00:00 +03:00
parent 1dfad65afd
commit 05aca2c529
5 changed files with 9 additions and 9 deletions

View File

@@ -521,7 +521,7 @@ 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 = unsafe { dc_array_new(128 as size_t) };
let ret = dc_array_new(128 as size_t);
for row in rows {
let id = row?;
@@ -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 = unsafe { dc_array_new(100 as size_t) };
let ret = dc_array_new(100 as size_t);
let success = context
.sql

View File

@@ -242,11 +242,11 @@ pub unsafe fn dc_array_get_raw(array: *const dc_array_t) -> *const uintptr_t {
(*array).array.as_ptr()
}
pub unsafe fn dc_array_new(initsize: size_t) -> *mut dc_array_t {
pub fn dc_array_new(initsize: size_t) -> *mut dc_array_t {
dc_array_new_typed(0, initsize)
}
pub unsafe fn dc_array_new_typed(type_0: libc::c_int, initsize: size_t) -> *mut dc_array_t {
pub fn dc_array_new_typed(type_0: libc::c_int, initsize: size_t) -> *mut dc_array_t {
let capacity = if initsize < 1 { 1 } else { initsize as usize };
let mut array = dc_array_t::new(capacity);
array.type_0 = type_0;

View File

@@ -1285,7 +1285,7 @@ pub fn dc_get_chat_media(
],
|row| row.get::<_, i32>(0),
|ids| {
let ret = unsafe { dc_array_new(100) };
let ret = dc_array_new(100);
for id in ids {
unsafe { dc_array_add_id(ret, id? as u32) };
}
@@ -1458,7 +1458,7 @@ 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 = unsafe { dc_array_new(100) };
let ret = dc_array_new(100);
for id in ids {
unsafe { dc_array_add_id(ret, id? as u32) };

View File

@@ -537,7 +537,7 @@ pub fn dc_get_contacts(
.unwrap_or_default();
let mut add_self = false;
let ret = unsafe { dc_array_new(100) };
let ret = dc_array_new(100);
if (listflags & DC_GCL_VERIFIED_ONLY) > 0 || !query.is_null() {
let s3strLikeCmd = format!("%{}%", if !query.is_null() { as_str(query) } else { "" });
@@ -629,7 +629,7 @@ pub fn dc_get_blocked_contacts(context: &Context) -> *mut dc_array_t {
params![9],
|row| row.get::<_, i32>(0),
|ids| {
let ret = unsafe { dc_array_new(100) };
let ret = dc_array_new(100);
for id in ids {
unsafe { dc_array_add_id(ret, id? as u32) };

View File

@@ -247,7 +247,7 @@ pub fn dc_get_locations(
Ok(loc)
},
|locations| {
let ret = unsafe { dc_array_new_typed(1, 500) };
let ret = dc_array_new_typed(1, 500);
for location in locations {
unsafe {