summaryrefslogtreecommitdiff
path: root/test_ffi/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'test_ffi/src/lib.rs')
-rw-r--r--test_ffi/src/lib.rs40
1 files changed, 27 insertions, 13 deletions
diff --git a/test_ffi/src/lib.rs b/test_ffi/src/lib.rs
index 5b813cd01..b4908d9cd 100644
--- a/test_ffi/src/lib.rs
+++ b/test_ffi/src/lib.rs
@@ -1,5 +1,7 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
+#![allow(clippy::undocumented_unsafe_blocks)]
+
use std::os::raw::c_void;
use std::thread::sleep;
use std::time::Duration;
@@ -11,23 +13,29 @@ pub extern "C" fn print_something() {
println!("something");
}
-#[allow(clippy::not_unsafe_ptr_arg_deref)]
+/// # Safety
+///
+/// The pointer to the buffer must be valid and initalized, and the length must
+/// not be longer than the buffer's allocation.
#[no_mangle]
-pub extern "C" fn print_buffer(ptr: *const u8, len: usize) {
- let buf = unsafe { std::slice::from_raw_parts(ptr, len) };
+pub unsafe extern "C" fn print_buffer(ptr: *const u8, len: usize) {
+ let buf = std::slice::from_raw_parts(ptr, len);
println!("{:?}", buf);
}
-#[allow(clippy::not_unsafe_ptr_arg_deref)]
+/// # Safety
+///
+/// The pointer to the buffer must be valid and initalized, and the length must
+/// not be longer than the buffer's allocation.
#[no_mangle]
-pub extern "C" fn print_buffer2(
+pub unsafe extern "C" fn print_buffer2(
ptr1: *const u8,
len1: usize,
ptr2: *const u8,
len2: usize,
) {
- let buf1 = unsafe { std::slice::from_raw_parts(ptr1, len1) };
- let buf2 = unsafe { std::slice::from_raw_parts(ptr2, len2) };
+ let buf1 = std::slice::from_raw_parts(ptr1, len1);
+ let buf2 = std::slice::from_raw_parts(ptr2, len2);
println!("{:?} {:?}", buf1, buf2);
}
@@ -87,19 +95,25 @@ pub extern "C" fn sleep_blocking(ms: u64) {
sleep(duration);
}
-#[allow(clippy::not_unsafe_ptr_arg_deref)]
+/// # Safety
+///
+/// The pointer to the buffer must be valid and initalized, and the length must
+/// not be longer than the buffer's allocation.
#[no_mangle]
-pub extern "C" fn fill_buffer(value: u8, buf: *mut u8, len: usize) {
- let buf = unsafe { std::slice::from_raw_parts_mut(buf, len) };
+pub unsafe extern "C" fn fill_buffer(value: u8, buf: *mut u8, len: usize) {
+ let buf = std::slice::from_raw_parts_mut(buf, len);
for itm in buf.iter_mut() {
*itm = value;
}
}
-#[allow(clippy::not_unsafe_ptr_arg_deref)]
+/// # Safety
+///
+/// The pointer to the buffer must be valid and initalized, and the length must
+/// not be longer than the buffer's allocation.
#[no_mangle]
-pub extern "C" fn nonblocking_buffer(ptr: *const u8, len: usize) {
- let buf = unsafe { std::slice::from_raw_parts(ptr, len) };
+pub unsafe extern "C" fn nonblocking_buffer(ptr: *const u8, len: usize) {
+ let buf = std::slice::from_raw_parts(ptr, len);
assert_eq!(buf, vec![1, 2, 3, 4, 5, 6, 7, 8]);
}