Fix vec from_raw_parts when ptr is null

This commit is contained in:
Tyler Hallada 2020-11-21 01:52:17 -05:00
parent 5e7ec54c6b
commit 82c7bdd92d
3 changed files with 19 additions and 32 deletions

View File

@ -269,17 +269,11 @@ pub extern "C" fn create_interior_ref_list(
info!("create_interior_ref_list api_url: {:?}, api_key: {:?}, shop_id: {:?}, raw_interior_ref_len: {:?}, raw_shelf_len: {:?}", api_url, api_key, shop_id, raw_interior_ref_len, raw_shelf_len);
let raw_interior_ref_slice = match raw_interior_ref_ptr.is_null() {
true => &[],
false => unsafe {
assert!(!raw_interior_ref_ptr.is_null());
slice::from_raw_parts(raw_interior_ref_ptr, raw_interior_ref_len)
},
false => unsafe { slice::from_raw_parts(raw_interior_ref_ptr, raw_interior_ref_len) },
};
let raw_shelf_slice = match raw_shelf_ptr.is_null() {
true => &[],
false => unsafe {
assert!(!raw_shelf_ptr.is_null());
slice::from_raw_parts(raw_shelf_ptr, raw_shelf_len)
},
false => unsafe { slice::from_raw_parts(raw_shelf_ptr, raw_shelf_len) },
};
fn inner(
@ -364,17 +358,11 @@ pub extern "C" fn update_interior_ref_list(
info!("update_interior_ref_list api_url: {:?}, api_key: {:?}, shop_id: {:?}, raw_interior_ref_len: {:?}, raw_shelf_len: {:?}", api_url, api_key, shop_id, raw_interior_ref_len, raw_shelf_len);
let raw_interior_ref_slice = match raw_interior_ref_ptr.is_null() {
true => &[],
false => unsafe {
assert!(!raw_interior_ref_ptr.is_null());
slice::from_raw_parts(raw_interior_ref_ptr, raw_interior_ref_len)
},
false => unsafe { slice::from_raw_parts(raw_interior_ref_ptr, raw_interior_ref_len) },
};
let raw_shelf_slice = match raw_shelf_ptr.is_null() {
true => &[],
false => unsafe {
assert!(!raw_shelf_ptr.is_null());
slice::from_raw_parts(raw_shelf_ptr, raw_shelf_len)
},
false => unsafe { slice::from_raw_parts(raw_shelf_ptr, raw_shelf_len) },
};
fn inner(
@ -1041,16 +1029,16 @@ mod tests {
FFIResult::Ok(raw_interior_ref_data) => {
assert_eq!(raw_interior_ref_data.interior_ref_vec.len, 1);
assert_eq!(raw_interior_ref_data.shelf_vec.len, 1);
assert!(!raw_interior_ref_data.interior_ref_vec.ptr.is_null());
let raw_interior_ref_slice = unsafe {
assert!(!raw_interior_ref_data.interior_ref_vec.ptr.is_null());
slice::from_raw_parts(
raw_interior_ref_data.interior_ref_vec.ptr,
raw_interior_ref_data.interior_ref_vec.len,
)
};
let raw_interior_ref = &raw_interior_ref_slice[0];
assert!(!raw_interior_ref_data.shelf_vec.ptr.is_null());
let raw_shelf_slice = unsafe {
assert!(!raw_interior_ref_data.shelf_vec.ptr.is_null());
slice::from_raw_parts(
raw_interior_ref_data.shelf_vec.ptr,
raw_interior_ref_data.shelf_vec.len,
@ -1175,16 +1163,16 @@ mod tests {
FFIResult::Ok(raw_interior_ref_data) => {
assert_eq!(raw_interior_ref_data.interior_ref_vec.len, 1);
assert_eq!(raw_interior_ref_data.shelf_vec.len, 1);
assert!(!raw_interior_ref_data.interior_ref_vec.ptr.is_null());
let raw_interior_ref_slice = unsafe {
assert!(!raw_interior_ref_data.interior_ref_vec.ptr.is_null());
slice::from_raw_parts(
raw_interior_ref_data.interior_ref_vec.ptr,
raw_interior_ref_data.interior_ref_vec.len,
)
};
let raw_interior_ref = &raw_interior_ref_slice[0];
assert!(!raw_interior_ref_data.shelf_vec.ptr.is_null());
let raw_shelf_slice = unsafe {
assert!(!raw_interior_ref_data.shelf_vec.ptr.is_null());
slice::from_raw_parts(
raw_interior_ref_data.shelf_vec.ptr,
raw_interior_ref_data.shelf_vec.len,

View File

@ -101,9 +101,9 @@ pub extern "C" fn create_merchandise_list(
let api_url = unsafe { CStr::from_ptr(api_url) }.to_string_lossy();
let api_key = unsafe { CStr::from_ptr(api_key) }.to_string_lossy();
info!("create_merchandise_list api_url: {:?}, api_key: {:?}, shop_id: {:?}, raw_merchandise_len: {:?}", api_url, api_key, shop_id, raw_merchandise_len);
let raw_merchandise_slice = unsafe {
assert!(!raw_merchandise_ptr.is_null());
slice::from_raw_parts(raw_merchandise_ptr, raw_merchandise_len)
let raw_merchandise_slice = match raw_merchandise_ptr.is_null() {
true => &[],
false => unsafe { slice::from_raw_parts(raw_merchandise_ptr, raw_merchandise_len) },
};
fn inner(
@ -176,9 +176,9 @@ pub extern "C" fn update_merchandise_list(
let api_url = unsafe { CStr::from_ptr(api_url) }.to_string_lossy();
let api_key = unsafe { CStr::from_ptr(api_key) }.to_string_lossy();
info!("create_merchandise_list api_url: {:?}, api_key: {:?}, shop_id: {:?}, raw_merchandise_len: {:?}", api_url, api_key, shop_id, raw_merchandise_len);
let raw_merchandise_slice = unsafe {
assert!(!raw_merchandise_ptr.is_null());
slice::from_raw_parts(raw_merchandise_ptr, raw_merchandise_len)
let raw_merchandise_slice = match raw_merchandise_ptr.is_null() {
true => &[],
false => unsafe { slice::from_raw_parts(raw_merchandise_ptr, raw_merchandise_len) },
};
fn inner(
@ -647,8 +647,8 @@ mod tests {
match result {
FFIResult::Ok(raw_merchandise_vec) => {
assert_eq!(raw_merchandise_vec.len, 1);
assert!(!raw_merchandise_vec.ptr.is_null());
let raw_merchandise_slice = unsafe {
assert!(!raw_merchandise_vec.ptr.is_null());
slice::from_raw_parts(raw_merchandise_vec.ptr, raw_merchandise_vec.len)
};
let raw_merchandise = &raw_merchandise_slice[0];
@ -732,8 +732,8 @@ mod tests {
match result {
FFIResult::Ok(raw_merchandise_vec) => {
assert_eq!(raw_merchandise_vec.len, 1);
assert!(!raw_merchandise_vec.ptr.is_null());
let raw_merchandise_slice = unsafe {
assert!(!raw_merchandise_vec.ptr.is_null());
slice::from_raw_parts(raw_merchandise_vec.ptr, raw_merchandise_vec.len)
};
let raw_merchandise = &raw_merchandise_slice[0];

View File

@ -574,10 +574,9 @@ mod tests {
match result {
FFIResult::Ok(raw_shops_vec) => {
assert_eq!(raw_shops_vec.len, 1);
let raw_shops_slice = unsafe {
assert!(!raw_shops_vec.ptr.is_null());
slice::from_raw_parts(raw_shops_vec.ptr, raw_shops_vec.len)
};
assert!(!raw_shops_vec.ptr.is_null());
let raw_shops_slice =
unsafe { slice::from_raw_parts(raw_shops_vec.ptr, raw_shops_vec.len) };
let raw_shop = &raw_shops_slice[0];
assert_eq!(raw_shop.id, 1);
assert_eq!(