chore: Move unsafe operations in unsafe functions to unsafe blocks (#36017)

Signed-off-by: DK Liao <dklassic@gmail.com>
This commit is contained in:
DK Liao 2025-03-18 15:19:35 +09:00 committed by GitHub
parent eb3c48f9d3
commit bcdd34e2aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 59 additions and 57 deletions

View file

@ -23,6 +23,3 @@ windows-sys = { workspace = true, features = ["Win32_System_Memory"] }
[target.'cfg(target_env = "ohos")'.dependencies]
libc = { workspace = true }
[lints.rust]
unsafe_op_in_unsafe_fn = { level = "allow" }

View file

@ -45,10 +45,14 @@ mod platform {
/// Passing a non-heap allocated pointer to this function results in undefined behavior.
pub unsafe extern "C" fn usable_size(ptr: *const c_void) -> usize {
#[cfg(target_vendor = "apple")]
return libc::malloc_size(ptr);
unsafe {
return libc::malloc_size(ptr);
}
#[cfg(not(target_vendor = "apple"))]
return libc::malloc_usable_size(ptr as *mut _);
unsafe {
return libc::malloc_usable_size(ptr as *mut _);
}
}
pub mod libc_compat {
@ -70,12 +74,14 @@ mod platform {
///
/// Passing a non-heap allocated pointer to this function results in undefined behavior.
pub unsafe extern "C" fn usable_size(mut ptr: *const c_void) -> usize {
let heap = GetProcessHeap();
unsafe {
let heap = GetProcessHeap();
if HeapValidate(heap, 0, ptr) == FALSE {
ptr = *(ptr as *const *const c_void).offset(-1)
if HeapValidate(heap, 0, ptr) == FALSE {
ptr = *(ptr as *const *const c_void).offset(-1)
}
HeapSize(heap, 0, ptr) as usize
}
HeapSize(heap, 0, ptr) as usize
}
}